1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-22 10:42:34 +01:00

strip microseconds in text.parse_datetime()

This commit is contained in:
Mike Fährmann 2020-06-17 21:40:16 +02:00
parent 0371fd54a1
commit 37d71f6e09
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
2 changed files with 11 additions and 4 deletions

View File

@ -252,8 +252,11 @@ def parse_datetime(date_string, format="%Y-%m-%dT%H:%M:%S%z", utcoffset=0):
o = d.utcoffset() o = d.utcoffset()
if o is not None: if o is not None:
# convert to naive UTC # convert to naive UTC
d = d.replace(tzinfo=None) - o d = d.replace(tzinfo=None, microsecond=0) - o
elif utcoffset: else:
if d.microsecond:
d = d.replace(microsecond=0)
if utcoffset:
# apply manual UTC offset # apply manual UTC offset
d += datetime.timedelta(0, utcoffset * -3600) d += datetime.timedelta(0, utcoffset * -3600)
return d return d

View File

@ -406,6 +406,10 @@ class TestText(unittest.TestCase):
f("2019-05-07T21:25:02+0900"), f("2019-05-07T21:25:02+0900"),
datetime.datetime(2019, 5, 7, 12, 25, 2), 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( self.assertEqual(
f("2019-05-07T21:25:02", "%Y-%m-%dT%H:%M:%S", utcoffset=9), f("2019-05-07T21:25:02", "%Y-%m-%dT%H:%M:%S", utcoffset=9),
datetime.datetime(2019, 5, 7, 12, 25, 2), datetime.datetime(2019, 5, 7, 12, 25, 2),