Skip to content

Commit 6d047b3

Browse files
Merge pull request #29 from nickmaccarthy/issue_28
Issue 28
2 parents fab35b3 + 6158aa5 commit 6d047b3

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
# Changelog
2+
## 1.5.5 (2021-04-26)
3+
* [FIX] [Issue #28](https://github.com/nickmaccarthy/python-datemath/issues/28)
4+
* `datemath()` object now returns the expected `datetime` object instead of an `Arrow` object
5+
* added tests to catch invalid object types of helpers
6+
27
## 1.5.4 (2021-04-20)
38
* skipped due to name conflict on pypi, all changes in this are from `1.5.3`
49

VERSION.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.5.4
1+
1.5.5

datemath/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ def dm(expr, **kwargs):
66

77
def datemath(expr, **kwargs):
88
''' does our datemath and returns a datetime object '''
9-
return parse(expr, **kwargs)
9+
return parse(expr, **kwargs).datetime

tests.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class TestDM(unittest.TestCase):
1515

1616

1717
def testParse(self):
18+
# Make sure our helpers return the correct objects
19+
self.assertIsInstance(datemath('now'), pydatetime)
20+
self.assertIsInstance(dm('now'), arrow.arrow.Arrow)
1821

1922
# Baisc dates
2023
self.assertEqual(dm('2016.01.02').format(iso8601), '2016-01-02T00:00:00+00:00')
@@ -42,13 +45,16 @@ def testParse(self):
4245
self.assertEqual(dm('2016-01-01', tz='US/Eastern'), pydatetime(2016, 1, 1, tzinfo=tz.gettz('US/Eastern')))
4346
self.assertEqual(datemath('2016-01-01T01:00:00', tz='US/Central'), pydatetime(2016, 1, 1, 1, 0, 0, tzinfo=tz.gettz('US/Central')))
4447
self.assertEqual(datemath('2016-01-01T02:00:00', tz='US/Eastern'), pydatetime(2016, 1, 1, 2, tzinfo=tz.gettz('US/Eastern')))
48+
4549
# TZ offset inside of date string
4650
self.assertEqual(datemath('2016-01-01T16:20:00.5+12:00'), pydatetime(2016, 1, 1, 16, 20, 0, 500000, tzinfo=tz.tzoffset(None, timedelta(hours=12))))
4751
self.assertEqual(datemath('2016-01-01T16:20:00.5-05:00'), pydatetime(2016, 1, 1, 16, 20, 0, 500000, tzinfo=tz.tzoffset(None, timedelta(hours=-5))))
4852
self.assertEqual(datemath('2016-01-01T16:20:00.5-00:00'), pydatetime(2016, 1, 1, 16, 20, 0, 500000, tzinfo=tz.tzoffset(None, timedelta(hours=0))))
53+
4954
# TZ offset inside of date string with datemath
5055
self.assertEqual(datemath('2016-01-01T16:20:00.5+12:00||+1d'), pydatetime(2016, 1, 2, 16, 20, 0, 500000, tzinfo=tz.tzoffset(None, timedelta(hours=12))))
5156
self.assertEqual(datemath('2016-01-01T16:20:00.6+12:00||+2d+1h'), pydatetime(2016, 1, 3, 17, 20, 0, 600000, tzinfo=tz.tzoffset(None, timedelta(hours=12))))
57+
5258
# If a TZ offset is in a datetime string, and there is a tz param used, the TZ offset will take precedence for the returned timeobj
5359
self.assertEqual(datemath('2016-01-01T16:20:00.6+12:00||+2d+1h', tz='US/Eastern'), pydatetime(2016, 1, 3, 17, 20, 0, 600000, tzinfo=tz.tzoffset(None, timedelta(hours=12))))
5460

@@ -62,6 +68,7 @@ def testParse(self):
6268
self.assertEqual(dm('+1M').format(iso8601), arrow.utcnow().shift(months=+1).format(iso8601))
6369
self.assertEqual(dm('+1Y').format(iso8601), arrow.utcnow().shift(years=+1).format(iso8601))
6470
self.assertEqual(dm('+1y').format(iso8601), arrow.utcnow().shift(years=+1).format(iso8601))
71+
6572
# subtraction
6673
self.assertEqual(dm('-1s').format(iso8601), arrow.utcnow().shift(seconds=-1).format(iso8601))
6774
self.assertEqual(dm('-1m').format(iso8601), arrow.utcnow().shift(minutes=-1).format(iso8601))
@@ -71,6 +78,7 @@ def testParse(self):
7178
self.assertEqual(dm('-1M').format(iso8601), arrow.utcnow().shift(months=-1).format(iso8601))
7279
self.assertEqual(dm('-1Y').format(iso8601), arrow.utcnow().shift(years=-1).format(iso8601))
7380
self.assertEqual(dm('-1y').format(iso8601), arrow.utcnow().shift(years=-1).format(iso8601))
81+
7482
# rounding
7583
self.assertEqual(dm('/s').format(iso8601), arrow.utcnow().floor('second').format(iso8601))
7684
self.assertEqual(dm('/m').format(iso8601), arrow.utcnow().floor('minute').format(iso8601))
@@ -80,6 +88,7 @@ def testParse(self):
8088
self.assertEqual(dm('/M').format(iso8601), arrow.utcnow().floor('month').format(iso8601))
8189
self.assertEqual(dm('/Y').format(iso8601), arrow.utcnow().floor('year').format(iso8601))
8290
self.assertEqual(dm('/y').format(iso8601), arrow.utcnow().floor('year').format(iso8601))
91+
8392
# rounding up
8493
self.assertEqual(dm('/s', roundDown=False).format(iso8601), arrow.utcnow().ceil('second').format(iso8601))
8594
self.assertEqual(dm('/m', roundDown=False).format(iso8601), arrow.utcnow().ceil('minute').format(iso8601))
@@ -89,8 +98,7 @@ def testParse(self):
8998
self.assertEqual(dm('/M', roundDown=False).format(iso8601), arrow.utcnow().ceil('month').format(iso8601))
9099
self.assertEqual(dm('/Y', roundDown=False).format(iso8601), arrow.utcnow().ceil('year').format(iso8601))
91100
self.assertEqual(dm('/y', roundDown=False).format(iso8601), arrow.utcnow().ceil('year').format(iso8601))
92-
93-
101+
94102
# roundDown option tests
95103
self.assertEqual(dm('2016-01-01T14:00:00||/d').format(iso8601), '2016-01-01T00:00:00+00:00')
96104
self.assertEqual(dm('2016-01-01T14:00:00||/d', roundDown=False).format(iso8601), '2016-01-01T23:59:59+00:00')
@@ -143,6 +151,7 @@ def testParse(self):
143151
self.assertEqual(dm(1367900664).format(iso8601), '2013-05-07T04:24:24+00:00')
144152

145153
self.assertRaises(DateMathException, dm, '1451610061000')
154+
146155
try:
147156
dm(1451610061000)
148157
except DateMathException as e:

0 commit comments

Comments
 (0)