From e6cdd37bc0dcce6f94d9b9810dfd65f7c27331eb Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Sat, 21 Sep 2013 22:46:31 +0200 Subject: Searching for time locations in other directories. --- timelocs.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/timelocs.go b/timelocs.go index bb04070..10b0f33 100644 --- a/timelocs.go +++ b/timelocs.go @@ -3,16 +3,59 @@ package main import ( "archive/zip" "log" + "os" "path" "runtime" "sort" "sync" + "time" ) var timeLocs []string var tlOnce sync.Once +func findfiles(p, prefix string, files []string) []string { + d, err := os.Open(p) + if err != nil { + return files + } + defer d.Close() + + infos, err := d.Readdir(-1) + if err != nil { + return files + } + + for _, info := range infos { + if info.Mode().IsRegular() { + files = append(files, prefix+info.Name()) + } else if info.IsDir() { + files = findfiles(path.Join(p, info.Name()), info.Name()+"/", files) + } + } + + return files +} + func listTimeLocations() ([]string, error) { + for _, p := range []string{"/usr/share/zoneinfo", "/usr/share/lib/zoneinfo", "/usr/lib/locale/TZ"} { + files := findfiles(p, "", nil) + duprem := make(map[string]bool) + for _, loc := range files { + if _, err := time.LoadLocation(loc); err == nil { + duprem[loc] = true + } + } + var locs []string + for loc := range duprem { + locs = append(locs, loc) + } + if len(locs) > 0 { + sort.Strings(locs) + return locs, nil + } + } + zoneinfoZip := path.Join(runtime.GOROOT(), "lib", "time", "zoneinfo.zip") z, err := zip.OpenReader(zoneinfoZip) if err != nil { -- cgit v1.2.3-54-g00ecf