Skip to content

Commit

Permalink
support for string based timeDates with formatting parameter; fixes a…
Browse files Browse the repository at this point in the history
  • Loading branch information
adriank committed Dec 3, 2018
1 parent ed6fa5f commit 8ac6948
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
13 changes: 9 additions & 4 deletions objectpath/utils/timeutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
except ImportError:
pass

from objectpath.core import STR_TYPES

HOURS_IN_DAY = 24

Expand Down Expand Up @@ -200,15 +201,19 @@ def dateTime(arg):
return datetime.datetime(*dt)
if l is 2:
date = time = None
if type(arg[0]) is datetime.date:
typeArg0 = type(arg[0])
typeArg1 = type(arg[1])
if typeArg0 in STR_TYPES:
return datetime.datetime.strptime(arg[0], arg[1])
if typeArg0 is datetime.date:
d = arg[0]
date = [d.year, d.month, d.day]
if type(arg[0]) in (tuple, list):
if typeArg0 in (tuple, list):
date = arg[0]
if type(arg[1]) is datetime.time:
if typeArg1 is datetime.time:
t = arg[1]
time = [t.hour, t.minute, t.second, t.microsecond]
if type(arg[1]) in (tuple, list):
if typeArg1 in (tuple, list):
time = arg[1]
return datetime.datetime(*date + time)

Expand Down
3 changes: 3 additions & 0 deletions tests/test_ObjectPath.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,9 @@ def test_builtin_time(self):
self.assertIsInstance(
execute("dateTime([2001,12,30,12,23,21,777777])"), datetime.datetime
)
self.assertIsInstance(execute('dateTime("1980-05-11 04:22:33", "%Y-%m-%d %H:%M:%S")'), datetime.datetime)
self.assertEqual(str(execute('dateTime("1980-05-11 04:22:33", "%Y-%m-%d %H:%M:%S")')), "1980-05-11 04:22:33")

self.assertEqual(
execute("toMillis(dateTime([2001,12,30,12,23,21,777777]))"),
1009715001777
Expand Down

0 comments on commit 8ac6948

Please sign in to comment.