-
Notifications
You must be signed in to change notification settings - Fork 0
/
values.go
78 lines (67 loc) · 2.01 KB
/
values.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
package offtz
import (
"fmt"
"io/ioutil"
"regexp/syntax"
"runtime"
"strings"
"time"
"unicode/utf8"
// "golang.org/x/sys/windows/registry"
)
// zoneDirs adapted from https://golang.org/src/time/zoneinfo_unix.go
// https://golang.org/doc/install/source#environment
// list of available GOOS as of 10th Feb 2017
// android, darwin, dragonfly, freebsd, linux, netbsd, openbsd, plan9, solaris,windows
var zoneDirs = map[string]string{
"android": "/system/usr/share/zoneinfo/",
"darwin": "/usr/share/zoneinfo/",
"dragonfly": "/usr/share/zoneinfo/",
"freebsd": "/usr/share/zoneinfo/",
"linux": "/usr/share/zoneinfo/",
"netbsd": "/usr/share/zoneinfo/",
"openbsd": "/usr/share/zoneinfo/",
// "plan9":"/adm/timezone/", -- no way to test this platform
"solaris": "/usr/share/lib/zoneinfo/",
"windows": `SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\`,
}
var timeZones = make(map[int][]string)
func init() {
if runtime.GOOS == "nacl" || runtime.GOOS == "" || runtime.GOOS == "windows" {
panic("unsupported platform")
}
readTZFile("")
}
// readTZFile ... read timezone file and append into timeZones slice
func readTZFile(path string) {
files, _ := ioutil.ReadDir(zoneDirs[runtime.GOOS] + path)
for _, f := range files {
if f.Name() != strings.ToUpper(f.Name()[:1])+f.Name()[1:] {
continue
}
if f.IsDir() {
readTZFile(path + "/" + f.Name())
} else {
tz := (path + "/" + f.Name())[1:]
// convert string to rune
tzRune, _ := utf8.DecodeRuneInString(tz[:1])
if syntax.IsWordChar(tzRune) { // filter out entry that does not start with A-Za-z such as +VERSION
loc, err := time.LoadLocation(tz)
if err != nil {
fmt.Println(err)
}
_, offset := time.Now().In(loc).Zone()
timeZones[offset] = appendIfMissing(timeZones[offset], tz)
}
}
}
}
// appendIfMissing ... appends an element if not available in a slice
func appendIfMissing(list []string, str string) []string {
for _, v := range list {
if v == str {
return list
}
}
return append(list, str)
}