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