-
Notifications
You must be signed in to change notification settings - Fork 0
/
ziptz.go
54 lines (45 loc) · 1.17 KB
/
ziptz.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
package goziptz
import (
"bufio"
"bytes"
"compress/zlib"
_ "embed"
"errors"
"regexp"
"time"
)
//go:embed data/tz.data
var tz []byte
var (
// ErrInvalidZip is returned when zip code is not in zip or zip+4 formats.
ErrInvalidZip = errors.New("invalid zip code")
// ErrLocationNotFound is returned when location for passed zip code is not found.
ErrLocationNotFound = errors.New("error location not found")
)
var zipRegexp = regexp.MustCompile(`^\d{5}(?:[-\s]\d{4})?$`)
var sep = []byte("|")
// ZipToLocation returns IANA timezone of the US zip code.
// The zip code can either be 5 digit format ("94606") or 5+4 digit format ("94606-1234").
func ZipToLocation(zip string) (*time.Location, error) {
if !zipRegexp.MatchString(zip) {
return nil, ErrInvalidZip
}
zb := []byte(zip[:5])
r, err := zlib.NewReader(bytes.NewBuffer(tz))
if err != nil {
return nil, err
}
defer r.Close()
scanner := bufio.NewScanner(r)
for scanner.Scan() {
b := scanner.Bytes()
if bytes.HasPrefix(b, zb) {
split := bytes.SplitN(b, sep, 3)
return time.LoadLocation(string(split[1]))
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return nil, ErrLocationNotFound
}