-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
tzf_default_finder.go
97 lines (84 loc) · 2.19 KB
/
tzf_default_finder.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
package tzf
import (
"fmt"
"runtime"
tzfrellite "github.com/ringsaturn/tzf-rel-lite"
"github.com/ringsaturn/tzf/pb"
"google.golang.org/protobuf/proto"
)
// DefaultFinder is a finder impl combine both [FuzzyFinder] and [Finder].
//
// It's designed for performance first and allow some not so correct return at some area.
type DefaultFinder struct {
fuzzyFinder F
finder F
}
func NewDefaultFinder() (F, error) {
fuzzyFinder, err := func() (F, error) {
input := &pb.PreindexTimezones{}
if err := proto.Unmarshal(tzfrellite.PreindexData, input); err != nil {
panic(err)
}
return NewFuzzyFinderFromPB(input)
}()
if err != nil {
return nil, err
}
finder, err := func() (F, error) {
input := &pb.CompressedTimezones{}
if err := proto.Unmarshal(tzfrellite.LiteCompressData, input); err != nil {
panic(err)
}
return NewFinderFromCompressed(input, SetDropPBTZ)
}()
if err != nil {
return nil, err
}
if finder.DataVersion() != fuzzyFinder.DataVersion() {
return nil, fmt.Errorf(
"tzf: DefaultFinder only support same data version for Finder(version=%v) and FuzzyFinder(version=%v)",
finder.DataVersion(),
fuzzyFinder.DataVersion(),
)
}
f := &DefaultFinder{}
f.fuzzyFinder = fuzzyFinder
f.finder = finder
// Force free mem by probuf, about 80MB
runtime.GC()
return f, nil
}
func (f *DefaultFinder) GetTimezoneName(lng float64, lat float64) string {
fuzzyRes := f.fuzzyFinder.GetTimezoneName(lng, lat)
if fuzzyRes != "" {
return fuzzyRes
}
name := f.finder.GetTimezoneName(lng, lat)
if name != "" {
return name
}
for _, dx := range []float64{-0.02, 0, 0.02} {
for _, dy := range []float64{-0.02, 0, 0.02} {
dlng := dx + lng
dlat := dy + lat
fuzzyRes := f.fuzzyFinder.GetTimezoneName(dlng, dlat)
if fuzzyRes != "" {
return fuzzyRes
}
name := f.finder.GetTimezoneName(dlng, dlat)
if name != "" {
return name
}
}
}
return ""
}
func (f *DefaultFinder) GetTimezoneNames(lng float64, lat float64) ([]string, error) {
return f.finder.GetTimezoneNames(lng, lat)
}
func (f *DefaultFinder) TimezoneNames() []string {
return f.finder.TimezoneNames()
}
func (f *DefaultFinder) DataVersion() string {
return f.finder.DataVersion()
}