===== time package =====
==== What is it? ====
`time` is Go’s standard library package for working with:
* current time
* durations
* sleeping/timers
* parsing and formatting dates/times
* time zones and locations
==== What is it used for? ====
* Create timestamps for logs/events.
* Measure elapsed time and performance.
* Set timeouts and deadlines (often with `context`).
* Schedule work with timers/tickers.
* Parse/format time strings (RFC3339, custom layouts).
==== Core types ====
* `time.Time`: a point in time (date + time + location)
* `time.Duration`: a length of time (e.g., 200ms, 5s)
* `time.Location`: time zone info (e.g., UTC, Asia/Ho_Chi_Minh)
==== Core functions (most common) ====
* `time.Now()`: current local time
* `time.Since(t)`: elapsed duration since `t`
* `time.Until(t)`: duration until `t`
* `time.Sleep(d)`: sleep for duration `d`
* `time.Parse(layout, value)`: parse string into `time.Time`
* `t.Format(layout)`: format `time.Time` into string
* `time.NewTimer(d)`: one-shot timer
* `time.NewTicker(d)`: periodic ticker
==== Example: current time ====
now := time.Now()
fmt.Println(now)
==== Example: measure elapsed time ====
start := time.Now()
// do work...
elapsed := time.Since(start)
fmt.Println("elapsed:", elapsed)
==== Example: sleep ====
time.Sleep(500 * time.Millisecond)
==== Example: parse + format (RFC3339) ====
t, err := time.Parse(time.RFC3339, "2026-01-09T10:30:00+07:00")
if err != nil { return err }
s := t.Format(time.RFC3339)
fmt.Println(s)
==== Example: custom layout (Go layout rule) ====
Go uses a reference time to define layouts:
==== Child pages:====
* [[go:time:Go_time_layout_reference|Go time layout reference]]
`