mirror of
https://github.com/imjasonh/staticmaps
synced 2026-07-08 10:55:03 +00:00
add TimeZone API
This commit is contained in:
parent
d4ffbe16cc
commit
42ad906d08
3 changed files with 66 additions and 0 deletions
|
|
@ -27,6 +27,7 @@ const (
|
|||
StatusInvalidRequest = "INVALID_REQUEST"
|
||||
StatusRequestDenied = "REQUEST_DENIED"
|
||||
StatusUnknownError = "UNKNOWN_ERROR"
|
||||
StatusOverQueryLimit = "OVER_QUERY_LIMIT"
|
||||
)
|
||||
|
||||
// TODO: via_waypoint not documented
|
||||
|
|
|
|||
12
maps_test.go
12
maps_test.go
|
|
@ -90,3 +90,15 @@ func TestStreetView(t *testing.T) {
|
|||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimeZone(t *testing.T) {
|
||||
c := NewClient("")
|
||||
ll := LatLng{40.7142700, -74.0059700}
|
||||
tm := time.Now()
|
||||
t.Logf("%s", baseURL+timezone(ll, tm, nil))
|
||||
r, err := c.TimeZone(ll, tm, nil)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
t.Logf("%v", r)
|
||||
}
|
||||
|
|
|
|||
53
timezone.go
Normal file
53
timezone.go
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
package maps
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (c Client) TimeZone(ll LatLng, t time.Time, opts *TimeZoneOpts) (*TimeZoneResponse, error) {
|
||||
var r TimeZoneResponse
|
||||
if err := c.doDecode(baseURL+timezone(ll, t, opts), &r); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
func timezone(ll LatLng, t time.Time, opts *TimeZoneOpts) string {
|
||||
p := url.Values{}
|
||||
p.Set("location", ll.String())
|
||||
p.Set("timestamp", fmt.Sprintf("%d", t.Unix()))
|
||||
opts.update(p)
|
||||
return "timezone/json?" + p.Encode()
|
||||
}
|
||||
|
||||
type TimeZoneOpts struct {
|
||||
Language string
|
||||
}
|
||||
|
||||
func (t *TimeZoneOpts) update(p url.Values) {
|
||||
if t == nil {
|
||||
return
|
||||
}
|
||||
if t.Language != "" {
|
||||
p.Set("language", t.Language)
|
||||
}
|
||||
}
|
||||
|
||||
type TimeZoneResponse struct {
|
||||
Status string `json:"status"`
|
||||
ErrorMessage string `json:"error_message"`
|
||||
DSTOffset int64 `json:"dstOffset"` // secs offset for DST
|
||||
RawOffset int64 `json:"rawOffset"`
|
||||
TimeZoneID string `json:"timeZoneId"`
|
||||
TimeZoneName string `json:"timeZoneName"`
|
||||
}
|
||||
|
||||
func (r TimeZoneResponse) DSTOffsetDuration() time.Duration {
|
||||
return time.Duration(r.DSTOffset) * time.Second
|
||||
}
|
||||
|
||||
func (r TimeZoneResponse) RawOffsetDuration() time.Duration {
|
||||
return time.Duration(r.RawOffset) * time.Second
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue