diff options
Diffstat (limited to 'timelocs.go')
-rw-r--r-- | timelocs.go | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/timelocs.go b/timelocs.go new file mode 100644 index 0000000..bb04070 --- /dev/null +++ b/timelocs.go @@ -0,0 +1,42 @@ +package main + +import ( + "archive/zip" + "log" + "path" + "runtime" + "sort" + "sync" +) + +var timeLocs []string +var tlOnce sync.Once + +func listTimeLocations() ([]string, error) { + zoneinfoZip := path.Join(runtime.GOROOT(), "lib", "time", "zoneinfo.zip") + z, err := zip.OpenReader(zoneinfoZip) + if err != nil { + return nil, err + } + defer z.Close() + + locs := []string{} + for _, f := range z.File { + if f.Name[len(f.Name)-1] == '/' { + continue + } + locs = append(locs, f.Name) + } + + sort.Strings(locs) + return locs, nil +} + +func loadTimeLocs() { + tlOnce.Do(func() { + var err error + if timeLocs, err = listTimeLocations(); err != nil { + log.Fatalf("Could not load time locations: %s", err) + } + }) +} |