-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (71 loc) · 1.4 KB
/
main.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
//go:generate go run util/embd.go
package tzdata
import (
"bytes"
"compress/gzip"
"fmt"
"io"
"time"
)
var (
Built = ""
// LocationNames contains all available timezone names
LocationNames = allLocations()
loaded = make(map[string]*time.Location)
)
// Uncompress and load all timezone data into memory for quicker access
func Preload() {
for k, _ := range tzdata {
loc, err := Load(k)
if err != nil {
panic(err)
}
loaded[k] = loc
}
}
// Load a timezone Location by name from the embedded tz database
func Load(name string) (*time.Location, error) {
name = resolveAlias(name)
if _, ok := tzdata[name]; !ok {
return nil, fmt.Errorf("unknown timezone: %s", name)
}
rbuf := bytes.NewBuffer(tzdata[name])
gz, err := gzip.NewReader(rbuf)
if err != nil {
return nil, err
}
defer gz.Close()
chunk := [20]byte{}
var wbuf bytes.Buffer
gzloop:
for {
n, err := gz.Read(chunk[:])
switch err {
case io.ErrUnexpectedEOF:
wbuf.Write(chunk[:n])
fallthrough
case io.EOF:
break gzloop
case nil:
wbuf.Write(chunk[:])
default:
return nil, err
}
}
return time.LoadLocationFromTZData(name, wbuf.Bytes())
}
func allLocations() (a []string) {
for k, _ := range tzdata {
a = append(a, k)
}
for k, _ := range aliases {
a = append(a, k)
}
return a
}
func resolveAlias(name string) string {
if aname, ok := aliases[name]; ok {
return resolveAlias(aname)
}
return name
}