Documentation
¶
Overview ¶
Package time provides context-aware time utilities extending the standard time package.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ParseDuration ¶ added in v0.14.0
ParseDuration parses a duration string. A duration string is a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h", "d", "w".
Unlike the standard library's time.ParseDuration, this also accepts the "d" (day, 24h) and "w" (week, 168h) units.
Example ¶
package main
import (
"fmt"
gotime "github.com/foomo/go/time"
)
func main() {
for _, s := range []string{"2w", "5d", "1w2d3h"} {
d, _ := gotime.ParseDuration(s)
fmt.Printf("%s = %s\n", s, d)
}
}
Output: 2w = 336h0m0s 5d = 120h0m0s 1w2d3h = 219h0m0s
func Sleep ¶
Sleep waits for the specified delay duration or until the context is canceled, whichever occurs first. Returns an error if the context is canceled before the delay elapses.
Example ¶
package main
import (
"context"
"fmt"
"time"
gotime "github.com/foomo/go/time"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
// Wait for 1 second (completes successfully)
err := gotime.Sleep(ctx, 1*time.Second)
if err != nil {
fmt.Println("Sleep failed:", err)
return
}
fmt.Println("Sleep completed successfully")
// Sleep for 3 seconds with a 2-second timeout (context cancels first)
ctx2, cancel2 := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel2()
err = gotime.Sleep(ctx2, 3*time.Second)
if err != nil {
fmt.Println("Sleep cancelled:", err)
}
}
Output: Sleep completed successfully Sleep cancelled: context deadline exceeded
Types ¶
This section is empty.