Skip to content

Commit 048df0d

Browse files
committed
Timezone
1 parent 844d620 commit 048df0d

3 files changed

Lines changed: 72 additions & 1 deletion

File tree

‎googlemaps/__init__.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from googlemaps.directions import directions
33
from googlemaps.geocoding import geocode
44
from googlemaps.geocoding import reverse_geocode
5+
from googlemaps.timezone import timezone
56

67
# Allow sphinx to pick up these symbols for the documentation.
7-
__all__ = ['Context', 'geocode', 'reverse_geocode', 'directions']
8+
__all__ = ['Context', 'geocode', 'reverse_geocode', 'directions', 'timezone']

‎googlemaps/timezone.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""Performs requests to the Google Maps Directions API."""
2+
3+
from googlemaps import common
4+
from googlemaps import convert
5+
6+
7+
def timezone(ctx, location, timestamp, language=None):
8+
"""Get time zone for a location on the earth, as well as that location's time offset from UTC.
9+
10+
:param ctx: Shared googlemaps.Context
11+
:type ctx: googlemaps.Context
12+
13+
:param location: The latitude/longitude value representing the location to look up.
14+
:type location: dict or tuple
15+
16+
:param timestamp: Timestamp specifies the desired time as seconds since midnight, January 1,
17+
1970 UTC. The Time Zone API uses the timestamp to determine whether or not Daylight Savings
18+
should be applied. Times before 1970 can be expressed as negative values.
19+
:type timestamp: int or datetime.datetime
20+
21+
:param language: The language in which to return results.
22+
:type language: basestring
23+
24+
:rtype: dict
25+
"""
26+
27+
if not isinstance(location, basestring):
28+
location = convert.latlng(location)
29+
30+
timestamp = convert.time(timestamp)
31+
32+
params = {
33+
"location": location,
34+
"timestamp": timestamp
35+
}
36+
37+
if language:
38+
params["language"] = language
39+
40+
return common._get(ctx, "/maps/api/timezone/json", params)

‎test/test_timezone.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This Python file uses the following encoding: utf-8
2+
3+
"""Tests for the timezone module."""
4+
5+
import unittest
6+
import datetime
7+
8+
import googlemaps
9+
10+
class TimezoneTest(unittest.TestCase):
11+
12+
def setUp(self):
13+
self.c = googlemaps.Context(
14+
key="AIzaSyDyZdCabN8GKh786tdj16gq80xalbbfqDM",
15+
timeout=5)
16+
17+
def test_los_angeles(self):
18+
timezone = googlemaps.timezone(self.c, (39.6034810,-119.6822510), 1331766000)
19+
self.assertIsNotNone(timezone)
20+
self.assertEquals(3600.0, timezone['dstOffset'])
21+
self.assertEquals('America/Los_Angeles', timezone['timeZoneId'])
22+
self.assertEquals('Pacific Daylight Time', timezone['timeZoneName'])
23+
24+
def test_los_angeles_es(self):
25+
timezone = googlemaps.timezone(self.c, (39.6034810,-119.6822510), 1331766000, language='es')
26+
self.assertIsNotNone(timezone)
27+
self.assertEquals(3600.0, timezone['dstOffset'])
28+
self.assertEquals('America/Los_Angeles', timezone['timeZoneId'])
29+
self.assertEquals(u'Hora de verano del Pacífico', timezone['timeZoneName'])
30+

0 commit comments

Comments
 (0)