11. Dates and Times

Unsurprisingly, given its antecedents, Go inherits the UNIX notion of time. Time is represented at the low level by the number of seconds since the UNIX Epoch: the start of 1970 UTC. Local time is calculated from this by applying a time zone offset, and can then be converted into something suitable for display, such as a year, month, and day.

Most Go APIs use nanoseconds for time intervals. It’s important to differentiate between time and time intervals. A time is a fixed point relative to some epoc date and depends on things like the current time zone. A time interval, in contrast, is a quantity that makes sense in isolation.

The time package includes two types for representing these two concepts. A Duration represents a ...

Get The Go Programming Language Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.