Documentation
¶
Overview ¶
Package tzf is a package convert (lng,lat) to timezone.
Inspired by timezonefinder https://github.com/jannikmi/timezonefinder, fast python package for finding the timezone of any point on earth (coordinates) offline.
Index ¶
- Variables
- func SetDropPBTZ(opt *Option)
- type DefaultFinder
- type Finder
- func (f *Finder) GetTimezone(lng float64, lat float64) (*pb.Timezone, error)
- func (f *Finder) GetTimezoneLoc(lng float64, lat float64) (*time.Location, error)
- func (f *Finder) GetTimezoneName(lng float64, lat float64) string
- func (f *Finder) GetTimezoneNames(lng float64, lat float64) ([]string, error)
- func (f *Finder) GetTimezoneShapeByName(name string) (*pb.Timezone, error)
- func (f *Finder) GetTimezoneShapeByShift(shift int) ([]*pb.Timezone, error)
- func (f *Finder) TimezoneNames() []string
- type FuzzyFinder
- type Option
- type OptionFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNoTimezoneFound = errors.New("tzf: no timezone found")
Functions ¶
func SetDropPBTZ ¶
func SetDropPBTZ(opt *Option)
SetDropPBTZ will make Finder not save github.com/deslittle/tzf/pb.Timezone in memory
Types ¶
type DefaultFinder ¶
type DefaultFinder struct {
// contains filtered or unexported fields
}
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.
func NewDefaultFinder ¶
func NewDefaultFinder() (*DefaultFinder, error)
func (*DefaultFinder) GetTimezoneName ¶
func (f *DefaultFinder) GetTimezoneName(lng float64, lat float64) string
Example ¶
package main
import (
"fmt"
"github.com/deslittle/tzf"
)
func main() {
finder, err := tzf.NewDefaultFinder()
if err != nil {
panic(err)
}
fmt.Println(finder.GetTimezoneName(116.6386, 40.0786))
}
Output: Asia/Shanghai
func (*DefaultFinder) GetTimezoneNames ¶
func (f *DefaultFinder) GetTimezoneNames(lng float64, lat float64) ([]string, error)
func (*DefaultFinder) TimezoneNames ¶
func (f *DefaultFinder) TimezoneNames() []string
type Finder ¶
type Finder struct {
// contains filtered or unexported fields
}
Finder is based on point-in-polygon search algo.
Memeory will use about 100MB if lite data and 1G if full data. Performance is very stable and very accuate.
func NewFinderFromCompressed ¶
func NewFinderFromCompressed(input *pb.CompressedTimezones, opts ...OptionFunc) (*Finder, error)
func NewFinderFromPB ¶
func NewFinderFromPB(input *pb.Timezones, opts ...OptionFunc) (*Finder, error)
func NewFinderFromRawJSON ¶
func NewFinderFromRawJSON(input *convert.BoundaryFile, opts ...OptionFunc) (*Finder, error)
func (*Finder) GetTimezone ¶
func (*Finder) GetTimezoneLoc ¶
Example ¶
package main
import (
"fmt"
"github.com/deslittle/tzf"
tzfrel "github.com/deslittle/tzf-rel"
"github.com/deslittle/tzf/pb"
"google.golang.org/protobuf/proto"
)
func main() {
input := &pb.Timezones{}
// Lite data, about 16.7MB
dataFile := tzfrel.LiteData
// Full data, about 83.5MB
// dataFile := tzfrel.FullData
if err := proto.Unmarshal(dataFile, input); err != nil {
panic(err)
}
finder, _ := tzf.NewFinderFromPB(input)
fmt.Println(finder.GetTimezoneLoc(116.6386, 40.0786))
}
Output: Asia/Shanghai <nil>
func (*Finder) GetTimezoneName ¶
GetTimezoneName will use alphabet order and return first matched result.
Example ¶
package main
import (
"fmt"
"github.com/deslittle/tzf"
tzfrel "github.com/deslittle/tzf-rel"
"github.com/deslittle/tzf/pb"
"google.golang.org/protobuf/proto"
)
func main() {
input := &pb.Timezones{}
// Lite data, about 16.7MB
dataFile := tzfrel.LiteData
// Full data, about 83.5MB
// dataFile := tzfrel.FullData
if err := proto.Unmarshal(dataFile, input); err != nil {
panic(err)
}
finder, _ := tzf.NewFinderFromPB(input)
fmt.Println(finder.GetTimezoneName(116.6386, 40.0786))
}
Output: Asia/Shanghai
func (*Finder) GetTimezoneNames ¶
func (*Finder) GetTimezoneShapeByName ¶
Example ¶
package main
import (
"fmt"
"github.com/deslittle/tzf"
tzfrel "github.com/deslittle/tzf-rel"
"github.com/deslittle/tzf/pb"
"google.golang.org/protobuf/proto"
)
func main() {
input := &pb.Timezones{}
// Lite data, about 16.7MB
dataFile := tzfrel.LiteData
if err := proto.Unmarshal(dataFile, input); err != nil {
panic(err)
}
finder, _ := tzf.NewFinderFromPB(input)
pbtz, err := finder.GetTimezoneShapeByName("Asia/Shanghai")
fmt.Printf("%v %v\n", pbtz.Name, err)
}
Output: Asia/Shanghai <nil>
func (*Finder) GetTimezoneShapeByShift ¶
Example ¶
package main
import (
"fmt"
"strings"
"sort"
"github.com/deslittle/tzf"
tzfrel "github.com/deslittle/tzf-rel"
"github.com/deslittle/tzf/pb"
"google.golang.org/protobuf/proto"
)
func main() {
input := &pb.Timezones{}
// Lite data, about 16.7MB
dataFile := tzfrel.LiteData
if err := proto.Unmarshal(dataFile, input); err != nil {
panic(err)
}
finder, _ := tzf.NewFinderFromPB(input)
pbtzs, _ := finder.GetTimezoneShapeByShift(28800)
pbnames := make([]string, 0)
for _, pbtz := range pbtzs {
pbnames = append(pbnames, pbtz.Name)
}
sort.Strings(pbnames)
fmt.Println(strings.Join(pbnames, ","))
}
Output: Asia/Brunei,Asia/Choibalsan,Asia/Hong_Kong,Asia/Irkutsk,Asia/Kuala_Lumpur,Asia/Kuching,Asia/Macau,Asia/Makassar,Asia/Manila,Asia/Shanghai,Asia/Singapore,Asia/Taipei,Asia/Ulaanbaatar,Australia/Perth,Etc/GMT-8
func (*Finder) TimezoneNames ¶
type FuzzyFinder ¶
type FuzzyFinder struct {
// contains filtered or unexported fields
}
FuzzyFinder use a tile map to store timezone name. Data are made by github.com/deslittle/tzf/cmd/preindextzpb which powerd by github.com/deslittle/tzf/preindex.PreIndexTimezones.
func NewFuzzyFinderFromPB ¶
func NewFuzzyFinderFromPB(input *pb.PreindexTimezones) (*FuzzyFinder, error)
func (*FuzzyFinder) GetTimezoneName ¶
func (f *FuzzyFinder) GetTimezoneName(lng float64, lat float64) string
Example ¶
package main
import (
"fmt"
"github.com/deslittle/tzf"
tzfrel "github.com/deslittle/tzf-rel"
"github.com/deslittle/tzf/pb"
"google.golang.org/protobuf/proto"
)
func main() {
input := &pb.PreindexTimezones{}
if err := proto.Unmarshal(tzfrel.PreindexData, input); err != nil {
panic(err)
}
finder, _ := tzf.NewFuzzyFinderFromPB(input)
fmt.Println(finder.GetTimezoneName(116.6386, 40.0786))
}
Output: Asia/Shanghai
func (*FuzzyFinder) GetTimezoneNames ¶
func (f *FuzzyFinder) GetTimezoneNames(lng float64, lat float64) ([]string, error)
type OptionFunc ¶
type OptionFunc = func(opt *Option)
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
comparetzpb
command
|
|
|
compresstzpb
command
CLI tool to reduce polygon filesize
|
CLI tool to reduce polygon filesize |
|
geojson2tzpb
command
CLI tool to convert GeoJSON based Timezone boundary to tzf's Probuf format.
|
CLI tool to convert GeoJSON based Timezone boundary to tzf's Probuf format. |
|
preindextzpb
command
CLI tool to preindex timezone shape.
|
CLI tool to preindex timezone shape. |
|
reducetzpb
command
CLI tool to reduce polygon filesize
|
CLI tool to reduce polygon filesize |
|
tzf
command
tzf-cli tool for local query.
|
tzf-cli tool for local query. |
|
Package preindex
|
Package preindex |
|
Package reduce could reduce Polygon size both polygon lines and float precise.
|
Package reduce could reduce Polygon size both polygon lines and float precise. |
