Skip to content

Commit 2fbb50b

Browse files
committed
datetime: add TZ support
The patch adds timezone support [1] for datetime. For the purpose of compatibility we got constants for timezones from Tarantool [2]. The patch adds a script to generate the data according to the instructions [3]. 1. https://github.com/tarantool/tarantool/wiki/Datetime-Internals#timezone-support 2. https://github.com/tarantool/tarantool/blob/9ee45289e01232b8df1413efea11db170ae3b3b4/src/lib/tzcode/timezones.h 3. https://github.com/tarantool/tarantool/blob/9ee45289e01232b8df1413efea11db170ae3b3b4/src/lib/tzcode/gen-zone-abbrevs.pl#L35-L39 Closes #163
1 parent 65a6de4 commit 2fbb50b

8 files changed

+1838
-68
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
1111
### Added
1212

1313
- Optional msgpack.v5 usage (#124)
14+
- TZ support for datetime (#163)
1415

1516
### Changed
1617

Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ clean:
2424
deps: clean
2525
( cd ./queue; tarantoolctl rocks install queue 1.1.0 )
2626

27+
.PHONY: datetime-timezones
28+
datetime-timezones:
29+
(cd ./datetime; ./gen-timezones.sh)
30+
2731
.PHONY: format
2832
format:
2933
goimports -l -w .

datetime/datetime.go

+55-12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ package datetime
1212
import (
1313
"encoding/binary"
1414
"fmt"
15+
"math"
1516
"time"
1617
)
1718

@@ -47,13 +48,11 @@ type datetime struct {
4748
// Nanoseconds, fractional part of seconds. Tarantool uses int32_t, see
4849
// a definition in src/lib/core/datetime.h.
4950
nsec int32
50-
// Timezone offset in minutes from UTC (not implemented in Tarantool,
51-
// see gh-163). Tarantool uses a int16_t type, see a structure
52-
// definition in src/lib/core/datetime.h.
51+
// Timezone offset in minutes from UTC. Tarantool uses a int16_t type,
52+
// see a structure definition in src/lib/core/datetime.h.
5353
tzOffset int16
54-
// Olson timezone id (not implemented in Tarantool, see gh-163).
55-
// Tarantool uses a int16_t type, see a structure definition in
56-
// src/lib/core/datetime.h.
54+
// Olson timezone id. Tarantool uses a int16_t type, see a structure
55+
// definition in src/lib/core/datetime.h.
5756
tzIndex int16
5857
}
5958

@@ -79,16 +78,40 @@ type Datetime struct {
7978
time time.Time
8079
}
8180

81+
const (
82+
noTimezoneZone = ""
83+
noTimezoneOffset = 0
84+
)
85+
86+
// NoTimezone allows to create a datetime without UTC timezone for
87+
// Tarantool. The problem is that Golang by default creates a time value with
88+
// UTC timezone. So it is a way to create a datetime without timezone.
89+
var NoTimezone = time.FixedZone(noTimezoneZone, noTimezoneOffset)
90+
8291
// NewDatetime returns a pointer to a new datetime.Datetime that contains a
83-
// specified time.Time. It may returns an error if the Time value is out of
84-
// supported range: [-5879610-06-22T00:00Z .. 5879611-07-11T00:00Z]
92+
// specified time.Time. It may return an error if the Time value is out of
93+
// supported range: [-5879610-06-22T00:00Z .. 5879611-07-11T00:00Z] or
94+
// an invalid timezone or offset value is out of supported range:
95+
// [math.MinInt16, math.MaxInt16]
8596
func NewDatetime(t time.Time) (*Datetime, error) {
8697
seconds := t.Unix()
8798

8899
if seconds < minSeconds || seconds > maxSeconds {
89100
return nil, fmt.Errorf("Time %s is out of supported range.", t)
90101
}
91102

103+
zone, offset := t.Zone()
104+
if zone != noTimezoneZone {
105+
if _, ok := timezoneToIndex[zone]; !ok {
106+
return nil, fmt.Errorf("Unknown timezone %s with offset %d",
107+
zone, offset)
108+
}
109+
}
110+
offset /= 60
111+
if offset < math.MinInt16 || offset > math.MaxInt16 {
112+
return nil, fmt.Errorf("Offset must be between %d and %d", math.MinInt16, math.MaxInt16)
113+
}
114+
92115
dt := new(Datetime)
93116
dt.time = t
94117
return dt, nil
@@ -105,8 +128,12 @@ func (dtime *Datetime) MarshalMsgpack() ([]byte, error) {
105128
var dt datetime
106129
dt.seconds = tm.Unix()
107130
dt.nsec = int32(tm.Nanosecond())
108-
dt.tzIndex = 0 // It is not implemented, see gh-163.
109-
dt.tzOffset = 0 // It is not implemented, see gh-163.
131+
132+
zone, offset := tm.Zone()
133+
if zone != noTimezoneZone {
134+
dt.tzIndex = int16(timezoneToIndex[zone])
135+
}
136+
dt.tzOffset = int16(offset / 60)
110137

111138
var bytesSize = secondsSize
112139
if dt.nsec != 0 || dt.tzOffset != 0 || dt.tzIndex != 0 {
@@ -127,7 +154,7 @@ func (dtime *Datetime) MarshalMsgpack() ([]byte, error) {
127154
func (tm *Datetime) UnmarshalMsgpack(b []byte) error {
128155
l := len(b)
129156
if l != maxSize && l != secondsSize {
130-
return fmt.Errorf("invalid data length: got %d, wanted %d or %d", len(b), secondsSize, maxSize)
157+
return fmt.Errorf("Invalid data length: got %d, wanted %d or %d", len(b), secondsSize, maxSize)
131158
}
132159

133160
var dt datetime
@@ -140,7 +167,23 @@ func (tm *Datetime) UnmarshalMsgpack(b []byte) error {
140167
dt.tzIndex = int16(binary.LittleEndian.Uint16(b[secondsSize+nsecSize+tzOffsetSize:]))
141168
}
142169

143-
tt := time.Unix(dt.seconds, int64(dt.nsec)).UTC()
170+
tt := time.Unix(dt.seconds, int64(dt.nsec))
171+
172+
loc := NoTimezone
173+
if dt.tzIndex != 0 || dt.tzOffset != 0 {
174+
zone := noTimezoneZone
175+
offset := int(dt.tzOffset) * 60
176+
177+
if dt.tzIndex != 0 {
178+
if _, ok := indexToTimezone[int(dt.tzIndex)]; !ok {
179+
return fmt.Errorf("Unknown timezone index %d", dt.tzIndex)
180+
}
181+
zone = indexToTimezone[int(dt.tzIndex)]
182+
}
183+
loc = time.FixedZone(zone, offset)
184+
}
185+
tt = tt.In(loc)
186+
144187
dtp, err := NewDatetime(tt)
145188
if dtp != nil {
146189
*tm = *dtp

0 commit comments

Comments
 (0)