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