-
Notifications
You must be signed in to change notification settings - Fork 1
/
timezones.go
472 lines (430 loc) · 13.8 KB
/
timezones.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// Package timezones builds new time.Locations.
package timezones
import (
"encoding/binary"
"errors"
"fmt"
"math"
"strings"
"time"
)
// Zone describes a single zone.
type Zone struct {
// Name of the zone.
Name string
// Offset to add to UTC time to get local time.
// Zones with positive offsets are east of UTC.
Offset time.Duration
// IsDST reports whether Daylight Savings Time is in effect.
IsDST bool
}
// Change describes a change from one Zone to another.
type Change struct {
// Start is time when the previous zone changes to ZoneIndex.
Start time.Time
// ZoneIndex which takes effect since Start.
// Must be in range 0 to len(Zones)-1.
ZoneIndex int
}
// Template describes how to build a time.Location.
type Template struct {
// Name of the new location.
Name string
// Zones lists local zones.
// At the beginning of time, Zone at index 0 applies.
// When that zone changes to another zone is specified in Changes.
// Maximum of 254 zones can be present.
Zones []Zone
// Changes specifies zone transitions.
// Changes Start times must be in strictly increasing order.
// If Extend is non-empty, the ZoneIndex of the last Change is ignored, Extend is used instead.
// Changes might be empty, in that case Extend must be non-empty.
Changes []Change
// If Extend is non-empty, it replaces the definition of zones since the last change.
// If there is at most one zone specified by Zones and Changes, Extend applies since the beginning of time.
// Extend is a TZ string conforming to RFC 8536, section 3.3.
Extend string
}
// NewLocation creates a new time.Location from the template.
func NewLocation(template Template) (*time.Location, error) {
tzData, err := buildTZData(&template)
if err != nil {
return nil, err
}
return time.LoadLocationFromTZData(template.Name, tzData)
}
// TZData converts the template to TZif data.
// The returned data will be compatible with Go's time package.
// Compatilibity with other TZif readers is not guaranteed, in particular readers that support only version 1
// of TZif will not work as TZData does not emit any V1 data.
func TZData(template Template) ([]byte, error) {
return buildTZData(&template)
}
const headerSize = 4 + 1 + 15 + 6*4 // magic + ver + unused + 6x count
// maxUserZones is how many zones a user can specify.
// There are max 255 possible local time type records in a TZif file.
// We reserve zone 0 for the first zone, which must be unused by transitions
// because Go's time package does not follow the RFC 8536 exactly and chooses nonzero record in
// some cases if zone 0 is used in transitions, see time.Location.lookupFirstZone.
const maxUserZones = 254
// buildTZData builds TZIF description from location template.
// See https://datatracker.ietf.org/doc/html/rfc8536
//
// If V2+ data is present in TZIF stream, readers should use V2 data.
// Go ignores the V1 data completely, in that case, so buildTZData uses empty V1 data block.
func buildTZData(template *Template) ([]byte, error) {
if len(template.Zones) > maxUserZones {
return nil, fmt.Errorf("too many zones (%d), max is %d", len(template.Zones), maxUserZones)
}
if len(template.Zones) == 0 && template.Extend == "" {
return nil, fmt.Errorf("either zones or extend string need to be present")
}
if len(template.Changes) > math.MaxUint32 {
return nil, fmt.Errorf("too many changes (%d), max is %d", len(template.Changes), math.MaxUint32)
}
size := headerSize + // v1 header + empty v1 data block
headerSize // v2 header
// We only write transition times, transition types, local time type records, time zone designations.
// Go seems to ignore standard/wall indicators and UT/local indicators, which seems like a bug in Go, so
// we include them.
// Go does not read leap seconds, so we don't include any.
timecnt := len(template.Changes)
isutcnt := timecnt
isstdcnt := timecnt
typecnt := len(template.Zones) + 1 // first zone is special
var firstZone Zone
if len(template.Zones) > 0 {
firstZone = template.Zones[0]
}
zd := zoneDesignations{
names: make([]string, 0, typecnt),
offsets: make([]int, 0, typecnt),
}
// Build time zone designations.
// We need to deduplicate them because the index into time zone designations is only a single byte.
zd.add(firstZone.Name)
for i := range template.Zones {
zd.add(template.Zones[i].Name)
}
if zd.charcnt > math.MaxUint8 {
return nil, fmt.Errorf("time zone designators don't fit into limit, charcnt=%d", zd.charcnt)
}
// Add the size of the V2 data block.
dataBlockSize := timecnt*8 + timecnt + typecnt*6 + zd.charcnt + isstdcnt + isutcnt
size += dataBlockSize
// Add the size of footer.
size += 2 + len(template.Extend)
data := make([]byte, size)
// V1 header
v1Header, rest := data[:headerSize], data[headerSize:]
v1Header[0] = 'T'
v1Header[1] = 'Z'
v1Header[2] = 'i'
v1Header[3] = 'f'
v1Header[4] = '3' // version
// V2 header
v2Header, rest := rest[:headerSize], rest[headerSize:]
v2Header[0] = 'T'
v2Header[1] = 'Z'
v2Header[2] = 'i'
v2Header[3] = 'f'
v2Header[4] = '3' // version
binary.BigEndian.PutUint32(v2Header[20:24], uint32(isutcnt))
binary.BigEndian.PutUint32(v2Header[24:28], uint32(isstdcnt))
binary.BigEndian.PutUint32(v2Header[32:36], uint32(timecnt))
binary.BigEndian.PutUint32(v2Header[36:40], uint32(typecnt))
binary.BigEndian.PutUint32(v2Header[40:44], uint32(zd.charcnt))
// V2 data block
// transition times
transitionTimes, rest := rest[:timecnt*8], rest[timecnt*8:]
for i := range template.Changes {
if i > 0 && !template.Changes[i].Start.After(template.Changes[i-1].Start) {
return nil, fmt.Errorf("zone changes must be in strictly ascending order")
}
binary.BigEndian.PutUint64(transitionTimes[:8], uint64(template.Changes[i].Start.Unix()))
transitionTimes = transitionTimes[8:]
}
// transition types
transitionTypes, rest := rest[:timecnt], rest[timecnt:]
for i := range template.Changes {
// We add 1 to ZoneIndex because local time type record 0 is used by firstZone.
transitionTypes[0] = byte(template.Changes[i].ZoneIndex + 1)
transitionTypes = transitionTypes[1:]
}
// local time type records
localTimeType, rest := rest[:typecnt*6], rest[typecnt*6:]
localTimeType = putLocalTimeTypeRecord(localTimeType, firstZone.Offset, firstZone.IsDST, zd.offsets[0])
for i := range template.Zones {
localTimeType = putLocalTimeTypeRecord(localTimeType, template.Zones[i].Offset, template.Zones[i].IsDST, zd.offsets[i+1])
}
// time zone designations
for i := range zd.names {
n := copy(rest, zd.names[i])
rest = rest[n+1:]
}
// no leap second records
// standard/wall indicators and UT/local indicators
// We are always using UT, so all indicators are 1.
fill(rest[:isstdcnt+isutcnt], 1)
rest = rest[isstdcnt+isutcnt:]
// footer
rest[0], rest = '\n', rest[1:]
copy(rest, template.Extend)
rest = rest[len(template.Extend):]
rest[0], rest = '\n', rest[1:]
// everything written, do a sanity check
if len(rest) != 0 {
panic("some data was not written")
}
return data, nil
}
// zoneDesignations builds the buffer that holds zone names.
type zoneDesignations struct {
charcnt int
names []string
offsets []int
}
func (zd *zoneDesignations) add(name string) {
for i := 0; i < len(zd.names); i++ {
if strings.HasSuffix(zd.names[i], name) {
// Reuse existing record.
zd.offsets = append(zd.offsets, zd.offsets[i]+len(zd.names[i])-len(name))
return
}
}
// Add new record.
zd.names = append(zd.names, name)
zd.offsets = append(zd.offsets, zd.charcnt)
zd.charcnt += len(name) + 1
}
func putLocalTimeTypeRecord(buf []byte, offset time.Duration, isDST bool, nameOffset int) []byte {
record, rest := buf[:6], buf[6:]
binary.BigEndian.PutUint32(record[0:4], uint32(offset/time.Second))
if isDST {
record[4] = 1
}
record[5] = byte(nameOffset)
return rest
}
// fill the buffer with a constant value.
func fill(buffer []byte, value byte) {
l := len(buffer)
if l == 0 {
return
}
buffer[0] = value
for i := 1; i < l; i *= 2 {
copy(buffer[i:], buffer[:i])
}
}
var (
errInvalid = errors.New("timezones: invalid tzdata")
errUnsupportedVersion = errors.New("timezones: unsupported tzdata version")
errTooManyZones = errors.New("timezones: too many zones")
errStdUT = errors.New("timezones: unsupported isstd/isut indicator values")
)
// LoadTZData into a template.
func LoadTZData(tzdata []byte) (*Template, error) {
if len(tzdata) < headerSize {
return nil, errInvalid
}
header := tzdata[:headerSize]
if header[0] != 'T' || header[1] != 'Z' || header[2] != 'i' || header[3] != 'f' {
return nil, errInvalid
}
var version int
switch header[4] {
case 0:
version = 1
case '2':
version = 2
case '3':
version = 3
default:
return nil, errUnsupportedVersion
}
var isutcnt, isstdcnt, leapcnt, timecnt, typecnt, charcnt uint32
isutcnt = binary.BigEndian.Uint32(header[20:24])
isstdcnt = binary.BigEndian.Uint32(header[24:28])
leapcnt = binary.BigEndian.Uint32(header[28:32])
timecnt = binary.BigEndian.Uint32(header[32:36])
typecnt = binary.BigEndian.Uint32(header[36:40])
charcnt = binary.BigEndian.Uint32(header[40:44])
rest := tzdata[headerSize:]
tsize := 4 // v1 times are 32 bit
size := uint64(timecnt)*uint64(tsize+1) +
uint64(typecnt)*6 +
uint64(charcnt) +
uint64(leapcnt)*uint64(tsize+4) +
uint64(isstdcnt) +
uint64(isutcnt)
if uint64(len(rest)) < size {
return nil, errInvalid
}
if size > math.MaxInt {
return nil, errInvalid
}
if version > 1 {
// skip V1 data block
rest = rest[size:]
// read V2 header
if len(rest) < headerSize {
return nil, errInvalid
}
header, rest = rest[:headerSize], rest[headerSize:]
if header[0] != 'T' || header[1] != 'Z' || header[2] != 'i' || header[3] != 'f' {
return nil, errInvalid
}
if header[4] != tzdata[4] {
return nil, errInvalid
}
tsize = 8
isutcnt = binary.BigEndian.Uint32(header[20:24])
isstdcnt = binary.BigEndian.Uint32(header[24:28])
leapcnt = binary.BigEndian.Uint32(header[28:32])
timecnt = binary.BigEndian.Uint32(header[32:36])
typecnt = binary.BigEndian.Uint32(header[36:40])
charcnt = binary.BigEndian.Uint32(header[40:44])
size = uint64(timecnt)*uint64(tsize+1) +
uint64(typecnt)*6 +
uint64(charcnt) +
uint64(leapcnt)*uint64(tsize+4) +
uint64(isstdcnt) +
uint64(isutcnt)
if uint64(len(rest)) < size {
return nil, errInvalid
}
if size > math.MaxInt {
return nil, errInvalid
}
}
timesLen := int(timecnt) * tsize
times, rest := rest[:timesLen], rest[timesLen:]
typesLen := int(timecnt)
types, rest := rest[:typesLen], rest[typesLen:]
lttLen := int(typecnt) * 6
ltt, rest := rest[:lttLen], rest[lttLen:]
charLen := int(charcnt)
chars, rest := string(rest[:charLen]), rest[charLen:]
leapLen := int(leapcnt) * (tsize + 4)
leap, rest := rest[:leapLen], rest[leapLen:]
_ = leap
isstdLen := int(isstdcnt)
isstd, rest := rest[:isstdLen], rest[isstdLen:]
isutLen := int(isutcnt)
isut, rest := rest[:isutLen], rest[isutLen:]
for i := range isstd {
if isstd[i] != 1 {
return nil, errStdUT
}
}
for i := range isut {
if isut[i] != 1 {
return nil, errStdUT
}
}
changes := make([]Change, int(timecnt))
if version == 1 {
for i := 0; i < int(timecnt); i++ {
changes[i].Start = time.Unix(int64(int32(binary.BigEndian.Uint32(times))), 0)
times = times[4:]
}
} else {
for i := 0; i < int(timecnt); i++ {
changes[i].Start = time.Unix(int64(binary.BigEndian.Uint64(times)), 0)
times = times[8:]
}
}
zeroIsUsed := false
for i := 0; i < typesLen; i++ {
changes[i].ZoneIndex = int(types[i])
if changes[i].ZoneIndex == 0 {
zeroIsUsed = true
}
}
zones := make([]Zone, int(typecnt))
for i := 0; i < len(zones); i++ {
zones[i].Offset = time.Duration(int32(binary.BigEndian.Uint32(ltt[0:4]))) * time.Second
switch ltt[4] {
case 0:
zones[i].IsDST = false
case 1:
zones[i].IsDST = true
default:
return nil, errInvalid
}
idx := int(ltt[5])
if idx >= len(chars) {
return nil, errInvalid
}
zones[i].Name = zeroTerminated(chars[idx:])
ltt = ltt[6:]
}
// TZif says type at index 0 is always used for times before the first transition.
// Go implements a different algorithm.
// We do what Go does, so that we are compatible with the time package.
fz := firstZone(zones, changes, zeroIsUsed)
if fz != 0 {
// Swap the zones and update indexes so that first zone is always at index 0.
zeroIsUsed = false
zones[0], zones[fz] = zones[fz], zones[0]
for i := range changes {
switch changes[i].ZoneIndex {
case 0:
changes[i].ZoneIndex = fz
case fz:
changes[i].ZoneIndex = 0
zeroIsUsed = true
}
}
}
var extend string
if len(rest) >= 2 && rest[0] == '\n' && rest[len(rest)-1] == '\n' {
extend = string(rest[1 : len(rest)-1])
}
// buildTZData adds a special zone 0 (so that Go always uses it as first zone and because at least one zone
// is required in the tzif file).
// If we are reading output of buildTZData, remove the first zone, so that the round-tripped Template is the same.
if !zeroIsUsed && len(zones) >= 2 && zones[0] == zones[1] || len(changes) == 0 && extend != "" {
zones = zones[1:]
for i := range changes {
changes[i].ZoneIndex -= 1
}
}
if len(zones) > maxUserZones {
// Template.Zones can have only maxUserZones so that we can always create *time.Location unambiguously.
return nil, errTooManyZones
}
return &Template{
Zones: zones,
Changes: changes,
Extend: extend,
}, nil
}
func zeroTerminated(s string) string {
for i := 0; i < len(s); i++ {
if s[i] == 0 {
return s[:i]
}
}
return s
}
// firstZone selects the first zone the same way as Go does.
func firstZone(zones []Zone, changes []Change, zeroIsUsed bool) int {
if !zeroIsUsed {
return 0
}
if len(changes) > 0 && zones[changes[0].ZoneIndex].IsDST {
for i := changes[0].ZoneIndex - 1; i >= 0; i-- {
if !zones[i].IsDST {
return i
}
}
}
for i := range zones {
if !zones[i].IsDST {
return i
}
}
return 0
}