Documentation
¶
Overview ¶
Package time provides context-aware time utilities extending the standard time package.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // Now returns the current time. It is a package-level variable so it can be // swapped out to control time, e.g., for time travel or deterministic unit // test outputs. Call timex.Now() instead of the standard library time.Now(), // then use Static or Incremental to take control of the clock. // // It defaults to the standard library time.Now. Now = time.Now // NowStaticNSec is the fixed instant, in Unix nanoseconds, returned by the // static provider installed via Static. It also seeds NowIncrementalNSec. // Defaults to 2021-01-01 12:00:00 (11:00:00 UTC). NowStaticNSec = int64(1609498800e9) // 2021-01-01 12:00:00 // NowIncrementalNSec is the current cursor, in Unix nanoseconds, used by the // incremental provider installed via Incremental. It advances by one // nanosecond on every call to Now and can be rewound with ResetIncremental. NowIncrementalNSec = NowStaticNSec )
Functions ¶
func Incremental ¶ added in v0.16.0
func Incremental()
Incremental points Now at an incremental time provider: each call to Now returns a strictly increasing instant, starting at time.Unix(0, NowIncrementalNSec) and advancing by one nanosecond per call.
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 ResetIncremental ¶ added in v0.16.0
func ResetIncremental()
ResetIncremental rewinds the incremental provider's cursor back to the static default (NowStaticNSec).
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
func Static ¶ added in v0.16.0
func Static()
Static points Now at a static time provider: every call to Now returns the same instant, time.Unix(0, NowStaticNSec).
Example ¶
package main
import (
"fmt"
"time"
timex "github.com/foomo/go/time"
)
func main() {
// Save and restore the default clock so the example is self-contained.
defer func() { timex.Now = time.Now }()
timex.Static()
fmt.Println(timex.Now().UTC().Format(time.RFC3339))
fmt.Println(timex.Now().UTC().Format(time.RFC3339))
}
Output: 2021-01-01T11:00:00Z 2021-01-01T11:00:00Z
Types ¶
This section is empty.