Time
time.Time, time.Now, Duration arithmetic, time.Since and time.Until, Add and Sub, and comparisons with Before, After, and Equal.
The time package represents instants with time.Time and elapsed lengths with time.Duration. Use Year, Month, and Day for calendar components. Durations use nanoseconds internally; named constants such as time.Second make them readable.
Use Add to move an instant by a duration, Sub to measure the duration between instants, and Before, After, or Equal to compare instants.
package main
import (
"fmt"
"time"
)
func main() {
start := time.Now()
deadline := start.Add(5 * time.Second)
fmt.Println(time.Until(deadline) > 0)
time.Sleep(10 * time.Millisecond)
fmt.Println("elapsed:", time.Since(start))
fmt.Println(start.Before(deadline))
}In production
time.Since and time.Until use the monotonic clock when available, which makes them suitable for measuring elapsed time despite wall-clock adjustments. Use wall-clock times for timestamps. Compare time.Time values with Equal, not ==, because location and monotonic-clock data can differ even for the same instant.