-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocaltimezone.go
292 lines (264 loc) · 6.96 KB
/
localtimezone.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Package localtimezone provides timezone lookup for a given location
//
// # Features
//
// * The timezone shapefile is embedded in the build binary using go-bindata
//
// * Supports overlapping zones
//
// * You can load your own geojson shapefile if you want
//
// * Sub millisecond lookup even on old hardware
//
// # Problems
//
// * The shapefile is simplified using a lossy method so it may be innacurate along the borders
//
// * This is purely in-memory. Uses ~50MB of ram
package localtimezone
import (
"bytes"
"compress/gzip"
_ "embed"
"errors"
"fmt"
"io"
"math"
"sort"
"sync"
json "github.com/json-iterator/go"
"github.com/paulmach/orb"
"github.com/paulmach/orb/geojson"
"github.com/paulmach/orb/planar"
)
// TZShapeFile is the data containing geographic shapes for timezone borders.
// This data is a large json blob compressed with gzip.
//
//go:embed data.json.gz
var TZShapeFile []byte
// MockTZShapeFile is similar to TZShapeFile but maps the entire world to the timezone America/Los_Angeles.
// This data is a small json blob compressed with gzip.
// It is meant for testing.
//
//go:embed data_mock.json.gz
var MockTZShapeFile []byte
// MockTimeZone is the timezone that is always returned from the NewMockLocalTimeZone client
const MockTimeZone = "America/Los_Angeles"
// ErrOutOfRange is returned when latitude exceeds 90 degrees or longitude exceeds 180 degrees
var ErrOutOfRange = errors.New("point's coordinates out of range")
// ErrNoTimeZone is returned when no matching timezone is found
// This error should never be returned because the client will attempt to return the nearest zone
var ErrNoTimeZone = errors.New("no timezone found")
// Point describes a location by Latitude and Longitude
type Point struct {
Lon float64
Lat float64
}
// pointFromOrb converts an orb Point into an internal Point
func pointFromOrb(p orb.Point) Point {
return Point{Lon: p[0], Lat: p[1]}
}
// pointToOrb converts an internal Point to an orb Point
func pointToOrb(p Point) orb.Point {
return orb.Point{p.Lon, p.Lat}
}
func init() {
// Set a faster json unmarshaller
geojson.CustomJSONUnmarshaler = json.ConfigFastest
}
// LocalTimeZone is a client for looking up time zones by Points
type LocalTimeZone interface {
GetZone(p Point) (tzids []string, err error)
GetOneZone(p Point) (tzid string, err error)
LoadGeoJSON(io.Reader) error
}
type tzData struct {
polygon *orb.Polygon
multiPolygon *orb.MultiPolygon
bound *orb.Bound
centers []orb.Point
}
type localTimeZone struct {
tzids []string
tzData map[string]tzData
mu sync.RWMutex
}
var _ LocalTimeZone = &localTimeZone{}
// NewLocalTimeZone creates a new LocalTimeZone with real timezone data
// The client is threadsafe
func NewLocalTimeZone() (LocalTimeZone, error) {
z := localTimeZone{}
err := z.load(TZShapeFile)
return &z, err
}
// NewMockLocalTimeZone creates a new LocalTimeZone that always returns
// America/Los_Angeles as the timezone
// The client is threadsafe
func NewMockLocalTimeZone() LocalTimeZone {
z := localTimeZone{}
err := z.load(MockTZShapeFile)
if err != nil {
panic(err)
}
return &z
}
func (z *localTimeZone) load(shapeFile []byte) error {
g, err := gzip.NewReader(bytes.NewBuffer(shapeFile))
if err != nil {
return err
}
defer g.Close()
err = z.LoadGeoJSON(g)
if err != nil {
return err
}
return nil
}
// GetZone returns a slice of strings containing time zone id's for a given Point
func (z *localTimeZone) GetZone(point Point) (tzids []string, err error) {
return z.getZone(point, false)
}
// GetOneZone returns a single zone id for a given Point
func (z *localTimeZone) GetOneZone(point Point) (tzid string, err error) {
tzids, err := z.getZone(point, true)
if err != nil {
return "", err
}
if len(tzids) == 0 {
return "", ErrNoTimeZone
}
return tzids[0], err
}
func (z *localTimeZone) getZone(point Point, single bool) (tzids []string, err error) {
p := pointToOrb(point)
if p[0] > 180 || p[0] < -180 || p[1] > 90 || p[1] < -90 {
return nil, ErrOutOfRange
}
z.mu.RLock()
defer z.mu.RUnlock()
for _, id := range z.tzids {
d := z.tzData[id]
if !d.bound.Contains(p) {
continue
}
if d.polygon != nil {
if planar.PolygonContains(*d.polygon, p) {
tzids = append(tzids, id)
if single {
return
}
}
continue
}
if d.multiPolygon != nil {
if planar.MultiPolygonContains(*d.multiPolygon, p) {
tzids = append(tzids, id)
if single {
return
}
}
}
}
if len(tzids) > 0 {
return tzids, nil
}
return z.getClosestZone(p)
}
func (z *localTimeZone) getClosestZone(point orb.Point) (tzids []string, err error) {
mindist := math.Inf(1)
var winner string
for id, d := range z.tzData {
for _, p := range d.centers {
tmp := planar.Distance(p, point)
if tmp < mindist {
mindist = tmp
winner = id
}
}
}
// Limit search radius
if mindist > 2.0 {
return getNauticalZone(point)
}
return append(tzids, winner), nil
}
func getNauticalZone(point orb.Point) (tzids []string, err error) {
z := point[0] / 7.5
z = (math.Abs(z) + 1) / 2
z = math.Floor(z)
if z == 0 {
return append(tzids, "Etc/GMT"), nil
}
if point[0] < 0 {
return append(tzids, fmt.Sprintf("Etc/GMT+%.f", z)), nil
}
return append(tzids, fmt.Sprintf("Etc/GMT-%.f", z)), nil
}
// buildCache builds centers for polygons
func (z *localTimeZone) buildCache(features []*geojson.Feature) {
var wg sync.WaitGroup
var m sync.Mutex
m.Lock()
for _, f := range features {
wg.Add(1)
go func(f *geojson.Feature) {
defer wg.Done()
id := f.Properties.MustString("tzid")
var multiPolygon orb.MultiPolygon
d := tzData{}
polygon, ok := f.Geometry.(orb.Polygon)
if ok {
d.polygon = &polygon
multiPolygon = []orb.Polygon{polygon}
} else {
multiPolygon, _ = f.Geometry.(orb.MultiPolygon)
d.multiPolygon = &multiPolygon
}
var tzCenters []orb.Point
for _, polygon := range multiPolygon {
for _, ring := range polygon {
point, _ := planar.CentroidArea(ring)
tzCenters = append(tzCenters, point)
}
}
bound := f.Geometry.Bound()
d.bound = &bound
d.centers = tzCenters
m.Lock()
z.tzData[id] = d
m.Unlock()
}(f)
}
m.Unlock()
wg.Wait()
z.tzids = make([]string, len(z.tzData))
i := 0
for tzid := range z.tzData {
z.tzids[i] = tzid
i++
}
sort.Strings(z.tzids)
}
// LoadGeoJSON loads a custom GeoJSON shapefile from a Reader
func (z *localTimeZone) LoadGeoJSON(r io.Reader) error {
z.mu.Lock()
var buf bytes.Buffer
_, err := buf.ReadFrom(r)
if err != nil {
return err
}
orbData, err := geojson.UnmarshalFeatureCollection(buf.Bytes())
if err != nil {
z.tzData = make(map[string]tzData)
z.tzids = []string{}
z.mu.Unlock()
return err
}
z.tzData = make(map[string]tzData, TZCount) // Possibly the incorrect length in case of Mock or custom data
z.tzids = []string{} // Cannot set a length or else array will be full of empty strings
go func(features []*geojson.Feature) {
defer z.mu.Unlock()
z.buildCache(features)
}(orbData.Features)
return nil
}