- Using durations & periods
- See also datetime doc
- Represents hours, minutes, seconds, millis, microseconds and nanoseconds
- Stored as
int64
(nanoseconds between two instants) - Max value is ~290 years
- Rust equivalent
- JVM equivalent
- ISO 8601
- Internally stored as nanos
- An amount of time meaningful to humans
- Generally imprecise because of timezones, daylight savings time, variable month lengths, ...
- Measured in Days, Weeks, Months, Years
- JVM equivalent
- TODO:
ISO-8601
parse & format - ISO 8601
Construct (from string)
TODO
Parse (from string)
- TODO: ISO 8601
- Example
TODO: more here
h
: hourm
: minutes
: secondms
: millisecondus
orµs
: microsecondns
: nanosecond
Format (to string)
- TODO: ISO 8601
- Produces a string which can be Parsed via time.ParseDuration
- Example
dur, err := time.ParseDuration("5h4m3s2ms")
...
fmt.Printf("dur: %v", dur.String())
start := time.Now()
// ... do something ...
elapsed := time.Now().Sub(start)
log.Info().
Str("elapsed", fmt.Sprintf("%v", elapsed)).
Msg("total time")
time.Sleep(100 * time.Millisecond)
time.Sleep(20 * time.Second)
time.Sleep(5 * time.Minute)
TODO
- TODO: more here
- Official doc
- rounds down (toward zero)
- Can truncate to any duration (eg. previous hour, minute, second, ... 5-minutes)
- Official doc
- Avoid
time.Since
because it hard codestime.Now()
(better to inject a time provider) - Avoid
time.Until
because it hard codestime.Now()
(better to inject a time provider)