diff --git a/directions.go b/directions.go index 7de8f52..ab884f5 100644 --- a/directions.go +++ b/directions.go @@ -27,6 +27,7 @@ const ( StatusInvalidRequest = "INVALID_REQUEST" StatusRequestDenied = "REQUEST_DENIED" StatusUnknownError = "UNKNOWN_ERROR" + StatusOverQueryLimit = "OVER_QUERY_LIMIT" ) // TODO: via_waypoint not documented diff --git a/maps_test.go b/maps_test.go index f4dd0a3..a37b056 100644 --- a/maps_test.go +++ b/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) +} diff --git a/timezone.go b/timezone.go new file mode 100644 index 0000000..6ccde42 --- /dev/null +++ b/timezone.go @@ -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 +}