I've been programming in Go for years now, and I released figtree to solve this duration type of a problem for me, but sometimes, you don't need or can't use figtree. That's okay too. What I have noticed is that if you use flag.Duration you need to pass the nanosecond value as an int into the flag in order for it to actually work.
For example, if you have a flag.Duration called -timeout set to a default of 5 * time.Minute then its int value is 300000000000 which is 300_000_000_000 (300B). If you use -timeout 5 for the flag, you're going to be off by 299,999,999,995 ns.
This simple Go program prints these various nanosecond integers needed for common human readable durations.
func main() {
fmt.Println("| Human Time | `int` Value | Go Syntax |")
fmt.Println("|------------|-------------|---------------|")
for i := range over(1, 60, 5) {
fmt.Printf("| `%d%s` | `%d` | `%d * time.Second` |\n", int(i), "s", i*time.Second, i)
}
for i := range over(1, 60, 5) {
fmt.Printf("| `%d%s` | `%d` | `%d * time.Minute` |\n", int(i), "m", i*time.Minute, i)
}
for i := range over(1, 24, 1) {
fmt.Printf("| `%d%s` | `%d` | `%d * time.Hour` |\n", int(i), "h", i*time.Hour, i)
}
}
I want to point your attention to the over() func because it's traditionally not accessible in languages like Go. However, a custom interator was built for this stepping output.
func over(start, stop, step int) iter.Seq[time.Duration] {
return func(yield func(time.Duration) bool) {
if start <= stop {
if !yield(time.Duration(start)) {
return
}
}
firstMultiple := start + (step - start%step)
if start%step == 0 {
firstMultiple = start + step
}
for i := firstMultiple; i <= stop; i += step {
if !yield(time.Duration(i)) {
return
}
}
}
}
When executed this program generates the following markdown table.
| Human Time |
int Value |
Go Syntax |
|---|---|---|
1s |
1000000000 |
1 * time.Second |
5s |
5000000000 |
5 * time.Second |
10s |
10000000000 |
10 * time.Second |
15s |
15000000000 |
15 * time.Second |
20s |
20000000000 |
20 * time.Second |
25s |
25000000000 |
25 * time.Second |
30s |
30000000000 |
30 * time.Second |
35s |
35000000000 |
35 * time.Second |
40s |
40000000000 |
40 * time.Second |
45s |
45000000000 |
45 * time.Second |
50s |
50000000000 |
50 * time.Second |
55s |
55000000000 |
55 * time.Second |
60s |
60000000000 |
60 * time.Second |
1m |
60000000000 |
1 * time.Minute |
5m |
300000000000 |
5 * time.Minute |
10m |
600000000000 |
10 * time.Minute |
15m |
900000000000 |
15 * time.Minute |
20m |
1200000000000 |
20 * time.Minute |
25m |
1500000000000 |
25 * time.Minute |
30m |
1800000000000 |
30 * time.Minute |
35m |
2100000000000 |
35 * time.Minute |
40m |
2400000000000 |
40 * time.Minute |
45m |
2700000000000 |
45 * time.Minute |
50m |
3000000000000 |
50 * time.Minute |
55m |
3300000000000 |
55 * time.Minute |
60m |
3600000000000 |
60 * time.Minute |
1h |
3600000000000 |
1 * time.Hour |
2h |
7200000000000 |
2 * time.Hour |
3h |
10800000000000 |
3 * time.Hour |
4h |
14400000000000 |
4 * time.Hour |
5h |
18000000000000 |
5 * time.Hour |
6h |
21600000000000 |
6 * time.Hour |
7h |
25200000000000 |
7 * time.Hour |
8h |
28800000000000 |
8 * time.Hour |
9h |
32400000000000 |
9 * time.Hour |
10h |
36000000000000 |
10 * time.Hour |
11h |
39600000000000 |
11 * time.Hour |
12h |
43200000000000 |
12 * time.Hour |
13h |
46800000000000 |
13 * time.Hour |
14h |
50400000000000 |
14 * time.Hour |
15h |
54000000000000 |
15 * time.Hour |
16h |
57600000000000 |
16 * time.Hour |
17h |
61200000000000 |
17 * time.Hour |
18h |
64800000000000 |
18 * time.Hour |
19h |
68400000000000 |
19 * time.Hour |
20h |
72000000000000 |
20 * time.Hour |
21h |
75600000000000 |
21 * time.Hour |
22h |
79200000000000 |
22 * time.Hour |
23h |
82800000000000 |
23 * time.Hour |
24h |
86400000000000 |
24 * time.Hour |
๐ Bookmark this page. It'll come in handy later for you. It's just 60B nanoseconds per minute. So you have 60 and 000000000 after aka 000_000_000. So 9 zeros after your second count. 17 seconds plus 9 zeros is 17_000_000_000 is 17 * time.Second.
I suppose that after writing this blog post about this I'll always remember that time.Duration is 9 zeros after the quantity of seconds. If you miss those 9 zeros, your duration will appear instant.
Top comments (0)