strip microseconds in text.parse_datetime()
This commit is contained in:
@@ -252,10 +252,13 @@ def parse_datetime(date_string, format="%Y-%m-%dT%H:%M:%S%z", utcoffset=0):
|
||||
o = d.utcoffset()
|
||||
if o is not None:
|
||||
# convert to naive UTC
|
||||
d = d.replace(tzinfo=None) - o
|
||||
elif utcoffset:
|
||||
# apply manual UTC offset
|
||||
d += datetime.timedelta(0, utcoffset * -3600)
|
||||
d = d.replace(tzinfo=None, microsecond=0) - o
|
||||
else:
|
||||
if d.microsecond:
|
||||
d = d.replace(microsecond=0)
|
||||
if utcoffset:
|
||||
# apply manual UTC offset
|
||||
d += datetime.timedelta(0, utcoffset * -3600)
|
||||
return d
|
||||
except (TypeError, IndexError, KeyError):
|
||||
return None
|
||||
|
||||
@@ -406,6 +406,10 @@ class TestText(unittest.TestCase):
|
||||
f("2019-05-07T21:25:02+0900"),
|
||||
datetime.datetime(2019, 5, 7, 12, 25, 2),
|
||||
)
|
||||
self.assertEqual(
|
||||
f("2019-05-07T21:25:02.753+0900", "%Y-%m-%dT%H:%M:%S.%f%z"),
|
||||
datetime.datetime(2019, 5, 7, 12, 25, 2),
|
||||
)
|
||||
self.assertEqual(
|
||||
f("2019-05-07T21:25:02", "%Y-%m-%dT%H:%M:%S", utcoffset=9),
|
||||
datetime.datetime(2019, 5, 7, 12, 25, 2),
|
||||
|
||||
Reference in New Issue
Block a user