|
| 1 | +Go Homo Sapiens Time is a Go package that provides utilities for converting human-readable time strings to milliseconds and vice-versa. It's a Go port of the popular JavaScript library homo-sapiens-time. |
| 2 | +--- |
| 3 | +Go Homo Sapiens Time is a Go package that provides utilities for converting human-readable time strings to milliseconds and vice-versa. It's a Go port of the popular JavaScript library [homo-sapiens-time](https://github.com/fezcode/homo-sapiens-time). |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +To install the package, use `go get`: |
| 8 | + |
| 9 | +```bash |
| 10 | +go get github.com/fezcode/go-homo-sapiens-time |
| 11 | +``` |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +Here's how to use the functions provided by the package: |
| 16 | + |
| 17 | +### Convert a time string to milliseconds |
| 18 | + |
| 19 | +```go |
| 20 | +package main |
| 21 | + |
| 22 | +import ( |
| 23 | + "fmt" |
| 24 | + "github.com/fezcode/go-homo-sapiens-time" |
| 25 | +) |
| 26 | + |
| 27 | +func main() { |
| 28 | + ms := homo_sapiens_time.TimeStringToMs("1h 30m") |
| 29 | + fmt.Println(ms) // Output: 5400000 |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +### Convert milliseconds to a time string |
| 34 | + |
| 35 | +```go |
| 36 | +package main |
| 37 | + |
| 38 | +import ( |
| 39 | + "fmt" |
| 40 | + "github.com/fezcode/go-homo-sapiens-time" |
| 41 | +) |
| 42 | + |
| 43 | +func main() { |
| 44 | + timeString, err := homo_sapiens_time.MsToTimeString(5400000, nil) |
| 45 | + if err != nil { |
| 46 | + fmt.Println("Error:", err) |
| 47 | + return |
| 48 | + } |
| 49 | + fmt.Println(timeString) // Output: 1h 30m |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +### Add a duration to the current time |
| 54 | + |
| 55 | +```go |
| 56 | +package main |
| 57 | + |
| 58 | +import ( |
| 59 | + "fmt" |
| 60 | + "time" |
| 61 | + "github.com/fezcode/go-homo-sapiens-time" |
| 62 | +) |
| 63 | + |
| 64 | +func main() { |
| 65 | + futureTime := homo_sapiens_time.ImpreciseDurationAddedToNow("1d 2h") |
| 66 | + fmt.Println("Current time:", time.Now()) |
| 67 | + fmt.Println("Future time:", futureTime) |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +## API |
| 72 | + |
| 73 | +### `TimeStringToMs(str string) int` |
| 74 | + |
| 75 | +Converts a human-readable time string (e.g., "1h 30m") into the equivalent number of milliseconds. |
| 76 | + |
| 77 | +### `MsToTimeString(ms int, opts *MsToTimeStringOptions) (string, error)` |
| 78 | + |
| 79 | +Converts a duration in milliseconds to a human-readable time string. The `opts` parameter allows for customization: |
| 80 | + |
| 81 | +- `Auto`: If `true`, the function will automatically determine the units to use. |
| 82 | +- `Units`: A slice of strings representing the units to use (e.g., `[]string{"h", "m", "s"}`). |
| 83 | +- `ShowEmpty`: If `true`, units with a value of 0 will be included in the output. |
| 84 | +- `SortUnits`: If `true`, the units in the output string will be sorted. |
| 85 | + |
| 86 | +### `ImpreciseDurationAddedToNow(str string) time.Time` |
| 87 | + |
| 88 | +Adds a duration specified in a human-readable time string to the current time and returns the resulting `time.Time`. |
| 89 | + |
| 90 | +### `GetMSEquivalent(unitType string, count int) int` |
| 91 | + |
| 92 | +A helper function that converts a given amount of a specific time unit to milliseconds. |
0 commit comments