Documentation
¶
Overview ¶
Package timez provides compact binary encoding of UTC time and time offset.
A timez value encodes the UTC time with microsecond precision and the local time offset in a 64 bit unsigned integer. Both values can be retrieved independently. This makes timez a convenient stamp value in binary messages crossing local time offset zones boundaries.
The most interseting property of timez values is that comparing the integer values is the same as comparing by the timez values by UTC time and, when equal, by time offset. Timez values are then convenient and efficient to use as key in a sorted table, or as indexed value stored in a database.
Timez encoding ¶
A Timez encodes the number of micro seconds elapsed since 1970-01-01T00:00:00.000000Z in the 53 most significant bits of a 64 bit unsigned integer.
The time offset is encoded in the 11 less significant bits as a number of minutes relative to 1024. Thus the offset value 1024 is the time offset 00:00, the value 984 is the time offset -01:00, and the value 1084 it the time offset +01:00.
64 11 0 bits |__________________ ______________|________| |_________________//_______________|________| | number of microseconds elapsed | time | | since 1970-01-01T00:00:00.000000 | offset |
The epoch is picked so that the unix time period is covered and beyond. The smallest representable time value is Jan 1 1970 00:00:00 minus 2^32-1 seconds as for the unix time. Since we have 33 bits to encode the number of seconds since that time, the timez epoch is 2^33 - 2^31. The biggest time value that can be represented with a timez value is in the year 2174.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Time ¶
type Time uint64
Time is a timez values.
const Invalid Time = 0
Invalid is an invalid time value.
func FromTime ¶
FromTime converts a time.Time value to a timez.Time value. Return timez.Invalid if the time.Time value can't be converted to a timez.Time value.
func FromUint64 ¶
FromUint64 return the uint64 value as a timez value. Return Invalid if the uint64 value is not a valid timez value.
func (Time) ToTime ¶
ToTime converts a timez value to a time.Time value. The Location is set to a time.FixedZone. The time offset will remain constant since the location is unknown and the daytime saving time change rule evolution is unknown. The returned time will be a zero time.Time value if the timez is invalid. This can be determined by calling the IsZero() method.