Last active
March 15, 2019 01:44
-
-
Save shabbyrobe/b9a7a56e25bd8e441b7b3fe39dbb04fa to your computer and use it in GitHub Desktop.
Quickie junk code used to get around golang issue #21881
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package zonetools | |
import ( | |
"archive/zip" | |
"bytes" | |
"encoding/base64" | |
"fmt" | |
"io/ioutil" | |
"regexp" | |
"strings" | |
"sync" | |
"time" | |
) | |
var ( | |
lock sync.RWMutex | |
locs = map[string]*time.Location{} | |
prepared bool | |
prepareErr error | |
) | |
func prepare() { | |
lock.Lock() | |
defer lock.Unlock() | |
prepared = true | |
ptn := regexp.MustCompile(`\s+`) | |
data := tzFiles["tz/zoneinfo.zip"] | |
data = ptn.ReplaceAllString(data, "") | |
decoder := base64.NewDecoder(base64.StdEncoding, strings.NewReader(data)) | |
bts, err := ioutil.ReadAll(decoder) | |
if err != nil { | |
prepareErr = err | |
return | |
} | |
r, err := zip.NewReader(bytes.NewReader(bts), int64(len(bts))) | |
if err != nil { | |
prepareErr = err | |
return | |
} | |
for _, f := range r.File { | |
if f.FileInfo().IsDir() { | |
continue | |
} | |
rc, err := f.Open() | |
if err != nil { | |
prepareErr = err | |
return | |
} | |
fbts, err := ioutil.ReadAll(rc) | |
if err != nil { | |
rc.Close() | |
prepareErr = err | |
return | |
} | |
tz, err := time.LoadLocationFromTZData(f.Name, fbts) | |
if err != nil { | |
rc.Close() | |
prepareErr = err | |
return | |
} | |
rc.Close() | |
locs[f.Name] = tz | |
} | |
} | |
func LoadLocation(name string) (*time.Location, error) { | |
lock.RLock() | |
if !prepared { | |
lock.RUnlock() | |
prepare() | |
lock.RLock() | |
} | |
defer lock.RUnlock() | |
z := locs[name] | |
if z == nil { | |
return time.UTC, fmt.Errorf("zonetools: cannot find %q", name) | |
} | |
oz := *z | |
return &oz, nil | |
} | |
// This code is dedicated to the public domain if such a concept | |
// exists in your location, or licensed under the MIT license if not. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 28:
undefined: tzFiles