Skip to content

Commit

Permalink
Add an overriding timezone parameter to delorean.parse
Browse files Browse the repository at this point in the history
Fixes issue myusuf3#72
  • Loading branch information
mlew committed Aug 21, 2015
1 parent 0964d48 commit 922eb86
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
24 changes: 19 additions & 5 deletions delorean/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
from .dates import Delorean, is_datetime_naive, datetime_timezone


def parse(datetime_str, dayfirst=True, yearfirst=True):
def parse(datetime_str, timezone=None, dayfirst=True, yearfirst=True):
"""
Parses a datetime string and returns a `Delorean` object.
:param str datetime_str: The string to be interpreted into a `Delorean` object.
:param bool dayfirst: Whether to interpret the first value in an ambiguous 3-integer date (ex. 01/05/09) as the day
:param datetime_str: The string to be interpreted into a `Delorean` object.
:param timezone: Pass this parameter and the returned Delorean object will be normalized to this timezone. Any
offsets passed as part of datetime_str will be ignored.
:param dayfirst: Whether to interpret the first value in an ambiguous 3-integer date (ex. 01/05/09) as the day
(True) or month (False). If yearfirst is set to True, this distinguishes between YDM and YMD.
:param bool yearfirst: Whether to interpret the first value in an ambiguous 3-integer date (ex. 01/05/09) as the
:param yearfirst: Whether to interpret the first value in an ambiguous 3-integer date (ex. 01/05/09) as the
year. If True, the first number is taken to be the year, otherwise the last number is taken to be the year.
.. testsetup::
Expand All @@ -40,6 +42,14 @@ def parse(datetime_str, dayfirst=True, yearfirst=True):
>>> parse('2015-01-01 00:01:02 -0800')
Delorean(datetime=datetime.datetime(2015, 1, 1, 0, 1, 2), timezone=pytz.FixedOffset(-480))
If the timezone argument is supplied, the returned Delorean object will be in the timezone supplied. Any offsets in
the datetime_str will be ignored.
.. doctest::
>>> parse('2015-01-01 00:01:02 -0500', timezone='US/Pacific')
Delorean(datetime=datetime.datetime(2015, 1, 1, 0, 1, 2), timezone='US/Pacific')
If an unambiguous timezone is detected in the datetime string, a Delorean object with that datetime and
timezone will be returned.
Expand All @@ -57,7 +67,10 @@ def parse(datetime_str, dayfirst=True, yearfirst=True):
"""
dt = capture(datetime_str, dayfirst=dayfirst, yearfirst=yearfirst)

if dt.tzinfo is None:
if timezone:
dt = dt.replace(tzinfo=None)
do = Delorean(datetime=dt, timezone=timezone)
elif dt.tzinfo is None:
# assuming datetime object passed in is UTC
do = Delorean(datetime=dt, timezone='UTC')
elif isinstance(dt.tzinfo, tzoffset):
Expand All @@ -75,6 +88,7 @@ def parse(datetime_str, dayfirst=True, yearfirst=True):
# if parse string has tzinfo we return a normalized UTC
# delorean object that represents the time.
do = Delorean(datetime=dt, timezone='UTC')

return do


Expand Down
20 changes: 20 additions & 0 deletions tests/delorean_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,26 @@ def test_parse_with_tzutc_timezone(self):
self.assertEqual(do.datetime, dt)
self.assertEqual(do.timezone, pytz.utc)

def test_parse_with_timezone_parameter(self):
tz = pytz.timezone('US/Pacific')
dt = tz.localize(datetime(2015, 1, 1))
tz = dt.tzinfo
dt_str = '{:%Y-%m-%d %H:%M:%S}'.format(dt)

do = delorean.parse(dt_str, timezone='US/Pacific')
self.assertEqual(do.datetime, dt)
self.assertEqual(do.timezone, tz)

def test_parse_with_overriding_timezone_parameter(self):
tz = pytz.timezone('US/Pacific')
dt = tz.localize(datetime(2015, 1, 1))
tz = dt.tzinfo
dt_str = '{:%Y-%m-%d %H:%M:%S -0500}'.format(dt)

do = delorean.parse(dt_str, timezone='US/Pacific')
self.assertEqual(do.datetime, dt)
self.assertEqual(do.timezone, tz)

def test_move_namedday(self):
dt_next = datetime(2013, 1, 4, 4, 31, 14, 148540, tzinfo=pytz.utc)
dt_next_2 = datetime(2013, 1, 11, 4, 31, 14, 148540, tzinfo=pytz.utc)
Expand Down

0 comments on commit 922eb86

Please sign in to comment.