|
| 1 | +// Package with support of Tarantool's datetime data type. |
| 2 | +// |
| 3 | +// Datetime data type supported in Tarantool since 2.10. |
| 4 | +// |
| 5 | +// Since: 1.7.0 |
| 6 | +// |
| 7 | +// See also: |
| 8 | +// |
| 9 | +// * Datetime Internals https://github.com/tarantool/tarantool/wiki/Datetime-Internals |
| 10 | +package datetime |
| 11 | + |
| 12 | +import ( |
| 13 | + "encoding/binary" |
| 14 | + "fmt" |
| 15 | + "time" |
| 16 | + |
| 17 | + "gopkg.in/vmihailenco/msgpack.v2" |
| 18 | +) |
| 19 | + |
| 20 | +// Datetime MessagePack serialization schema is an MP_EXT extension, which |
| 21 | +// creates container of 8 or 16 bytes long payload. |
| 22 | +// |
| 23 | +// +---------+--------+===============+-------------------------------+ |
| 24 | +// |0xd7/0xd8|type (4)| seconds (8b) | nsec; tzoffset; tzindex; (8b) | |
| 25 | +// +---------+--------+===============+-------------------------------+ |
| 26 | +// |
| 27 | +// MessagePack data encoded using fixext8 (0xd7) or fixext16 (0xd8), and may |
| 28 | +// contain: |
| 29 | +// |
| 30 | +// * [required] seconds parts as full, unencoded, signed 64-bit integer, |
| 31 | +// stored in little-endian order; |
| 32 | +// |
| 33 | +// * [optional] all the other fields (nsec, tzoffset, tzindex) if any of them |
| 34 | +// were having not 0 value. They are packed naturally in little-endian order; |
| 35 | + |
| 36 | +// Datetime external type. Supported since Tarantool 2.10. See more details in |
| 37 | +// issue https://github.com/tarantool/tarantool/issues/5946. |
| 38 | +const datetime_extId = 4 |
| 39 | + |
| 40 | +// datetime structure keeps a number of seconds and nanoseconds since Unix Epoch. |
| 41 | +// Time is normalized by UTC, so time-zone offset is informative only. |
| 42 | +type datetime struct { |
| 43 | + // Seconds since Epoch, where the epoch is the point where the time |
| 44 | + // starts, and is platform dependent. For Unix, the epoch is January 1, |
| 45 | + // 1970, 00:00:00 (UTC). Tarantool uses a double type, see a structure |
| 46 | + // definition in src/lib/core/datetime.h and reasons in |
| 47 | + // https://github.com/tarantool/tarantool/wiki/Datetime-internals#intervals-in-c |
| 48 | + seconds int64 |
| 49 | + // Nanoseconds, fractional part of seconds. Tarantool uses int32_t, see |
| 50 | + // a definition in src/lib/core/datetime.h. |
| 51 | + nsec int32 |
| 52 | + // Timezone offset in minutes from UTC (not implemented in Tarantool, |
| 53 | + // see gh-163). Tarantool uses a int16_t type, see a structure |
| 54 | + // definition in src/lib/core/datetime.h. |
| 55 | + tzOffset int16 |
| 56 | + // Olson timezone id (not implemented in Tarantool, see gh-163). |
| 57 | + // Tarantool uses a int16_t type, see a structure definition in |
| 58 | + // src/lib/core/datetime.h. |
| 59 | + tzIndex int16 |
| 60 | +} |
| 61 | + |
| 62 | +// Size of datetime fields in a MessagePack value. |
| 63 | +const ( |
| 64 | + secondsSize = 8 |
| 65 | + nsecSize = 4 |
| 66 | + tzIndexSize = 2 |
| 67 | + tzOffsetSize = 2 |
| 68 | +) |
| 69 | + |
| 70 | +const maxSize = secondsSize + nsecSize + tzIndexSize + tzOffsetSize |
| 71 | + |
| 72 | +type Datetime struct { |
| 73 | + time time.Time |
| 74 | +} |
| 75 | + |
| 76 | +// NewDatetime returns a pointer to a new datetime.Datetime that contains a |
| 77 | +// specified time.Time. |
| 78 | +func NewDatetime(t time.Time) *Datetime { |
| 79 | + dt := new(Datetime) |
| 80 | + dt.time = t |
| 81 | + return dt |
| 82 | +} |
| 83 | + |
| 84 | +// ToTime returns a time.Time that Datetime contains. |
| 85 | +func (dtime *Datetime) ToTime() time.Time { |
| 86 | + return dtime.time |
| 87 | +} |
| 88 | + |
| 89 | +var _ msgpack.Marshaler = (*Datetime)(nil) |
| 90 | +var _ msgpack.Unmarshaler = (*Datetime)(nil) |
| 91 | + |
| 92 | +func (dtime *Datetime) MarshalMsgpack() ([]byte, error) { |
| 93 | + tm := dtime.ToTime() |
| 94 | + |
| 95 | + var dt datetime |
| 96 | + dt.seconds = tm.Unix() |
| 97 | + dt.nsec = int32(tm.Nanosecond()) |
| 98 | + dt.tzIndex = 0 // It is not implemented, see gh-163. |
| 99 | + dt.tzOffset = 0 // It is not implemented, see gh-163. |
| 100 | + |
| 101 | + var bytesSize = secondsSize |
| 102 | + if dt.nsec != 0 || dt.tzOffset != 0 || dt.tzIndex != 0 { |
| 103 | + bytesSize += nsecSize + tzIndexSize + tzOffsetSize |
| 104 | + } |
| 105 | + |
| 106 | + buf := make([]byte, bytesSize) |
| 107 | + binary.LittleEndian.PutUint64(buf, uint64(dt.seconds)) |
| 108 | + if bytesSize == maxSize { |
| 109 | + binary.LittleEndian.PutUint32(buf[secondsSize:], uint32(dt.nsec)) |
| 110 | + binary.LittleEndian.PutUint16(buf[secondsSize+nsecSize:], uint16(dt.tzOffset)) |
| 111 | + binary.LittleEndian.PutUint16(buf[secondsSize+nsecSize+tzOffsetSize:], uint16(dt.tzIndex)) |
| 112 | + } |
| 113 | + |
| 114 | + return buf, nil |
| 115 | +} |
| 116 | + |
| 117 | +func (tm *Datetime) UnmarshalMsgpack(b []byte) error { |
| 118 | + l := len(b) |
| 119 | + if l != maxSize && l != secondsSize { |
| 120 | + return fmt.Errorf("invalid data length: got %d, wanted %d or %d", len(b), secondsSize, maxSize) |
| 121 | + } |
| 122 | + |
| 123 | + var dt datetime |
| 124 | + sec := binary.LittleEndian.Uint64(b) |
| 125 | + dt.seconds = int64(sec) |
| 126 | + dt.nsec = 0 |
| 127 | + if l == maxSize { |
| 128 | + dt.nsec = int32(binary.LittleEndian.Uint32(b[secondsSize:])) |
| 129 | + dt.tzOffset = int16(binary.LittleEndian.Uint16(b[secondsSize+nsecSize:])) |
| 130 | + dt.tzIndex = int16(binary.LittleEndian.Uint16(b[secondsSize+nsecSize+tzOffsetSize:])) |
| 131 | + } |
| 132 | + tt := time.Unix(dt.seconds, int64(dt.nsec)).UTC() |
| 133 | + *tm = *NewDatetime(tt) |
| 134 | + |
| 135 | + return nil |
| 136 | +} |
| 137 | + |
| 138 | +func init() { |
| 139 | + msgpack.RegisterExt(datetime_extId, &Datetime{}) |
| 140 | +} |
0 commit comments