Skip to content

Commit 3052eab

Browse files
committed
Timezone fix for date strings: #6
1 parent b71a7fc commit 3052eab

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

datemath/helpers.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
import arrow
4242
import re
43+
from dateutil import tz
4344

4445
debug = False
4546

@@ -127,21 +128,22 @@ def parse(expression, now=None, tz='UTC', type=None, roundDown=True):
127128

128129
if not math or math == '':
129130
rettime = time
130-
131131
rettime = evaluate(math, time, tz, roundDown)
132+
132133
if type:
133134
return getattr(rettime, type)
134135
else:
135136
return rettime
136137

137138

138-
def parseTime(timestamp, tz='UTC'):
139+
def parseTime(timestamp, timezone='UTC'):
139140
'''
140141
parses a date/time stamp and returns and arrow object
141142
'''
142-
#if timestamp and len(timestamp) >= 4 and (timestamp >= 0 or timestamp < 0):
143143
if timestamp and len(timestamp) >= 4:
144-
return arrow.get(timestamp)
144+
ts = arrow.get(timestamp)
145+
ts = ts.replace(tzinfo=tz.gettz(timezone))
146+
return ts
145147

146148

147149
def roundDate(now, unit, tz='UTC', roundDown=True):
@@ -236,3 +238,5 @@ def evaluate(expression, now, timeZone='UTC', roundDown=True):
236238
#print(type(parse('now', type='datetime')))
237239
#print(parse('now-2.5h'))
238240
#print(parse('2016-01-01||-3.2h'))
241+
#print(parse('now', tz='US/Pacific'))
242+
#print(parse('2017-09-22T10:20:00', tz='US/Eastern'))

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ requests==2.13.0
1111
requests-toolbelt==0.7.1
1212
six==1.10.0
1313
traceback2==1.4.0
14-
twine==1.8.1
14+
twine>=1.8.1
1515
unittest2==1.1.0

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
with open(path.join(here, 'README.md'), encoding='utf-8') as f:
1717
long_description = f.read()
1818

19-
version = '1.4.6'
19+
version = '1.4.7'
2020

2121
setup(
2222
name='python-datemath',

tests.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import unittest2 as unittest
22
import arrow
3+
from datetime import datetime as pydatetime
34
from datemath import dm
45
from datemath import datemath
6+
from dateutil import tz
57

68
iso8601 = 'YYYY-MM-DDTHH:mm:ssZZ'
79
class TestDM(unittest.TestCase):
@@ -27,6 +29,13 @@ def testParse(self):
2729
self.assertEqual(dm('2016-01-01||/d', roundDown=False).format('YYYY-MM-DDTHH:mm:ssZZ'), '2016-01-01T23:59:59-00:00')
2830
self.assertEqual(dm('2014-11-18||/y', roundDown=False).format('YYYY-MM-DDTHH:mm:ssZZ'), '2014-12-31T23:59:59-00:00')
2931

32+
# Timezone Tests
33+
self.assertEqual(dm('now', tz='US/Pacific').format(iso8601), arrow.utcnow().to('US/Pacific').format(iso8601))
34+
self.assertEqual(dm('2017-09-22 10:20:00', tz='US/Pacific').datetime, pydatetime(2017, 9, 22, 10, 20, 00, tzinfo=tz.gettz('US/Pacific')))
35+
self.assertEqual(dm('2016-01-01', tz='UTC'), arrow.get('2016-01-01').to('UTC'))
36+
self.assertEqual(dm('2016-01-01', tz='US/Eastern'), pydatetime(2016, 1, 1, tzinfo=tz.gettz('US/Eastern')))
37+
self.assertEqual(datemath('2016-01-01T01:00:00', tz='US/Central'), pydatetime(2016, 1, 1, 1, 0, 0, tzinfo=tz.gettz('US/Central')))
38+
3039
# relitive formats
3140
# addition
3241
self.assertEqual(dm('+1s').format(iso8601), arrow.utcnow().replace(seconds=+1).format(iso8601))

0 commit comments

Comments
 (0)