1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-22 02:32:33 +01:00

apply workaround from 4736912 in parse_datetime() itself

This commit is contained in:
Mike Fährmann 2019-05-09 21:53:17 +02:00
parent 523ebc9b0b
commit b0e85a42e3
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
3 changed files with 12 additions and 4 deletions

View File

@ -42,7 +42,7 @@ class PixivExtractor(Extractor):
del work["meta_pages"]
work["num"] = ""
work["tags"] = [tag["name"] for tag in work["tags"]]
work["date"] = text.parse_datetime(work["create_date"][:-3] + "00")
work["date"] = text.parse_datetime(work["create_date"])
work.update(metadata)
yield Message.Directory, work

View File

@ -226,12 +226,15 @@ def parse_timestamp(ts, default=None):
def parse_datetime(date_string, format="%Y-%m-%dT%H:%M:%S%z"):
"""Create a datetime object by parsing 'date_string'"""
try:
if format.endswith("%z") and date_string[-3] == ":":
# workaround for Python < 3.7: +00:00 -> +0000
date_string = date_string[:-3] + date_string[-2:]
d = datetime.datetime.strptime(date_string, format)
o = d.utcoffset()
if o is not None:
d = d.replace(tzinfo=None) - o # convert to naive UTC
return d
except TypeError:
except (TypeError, IndexError, KeyError):
return None
except (ValueError, OverflowError):
return date_string

View File

@ -353,9 +353,14 @@ class TestText(unittest.TestCase):
def test_parse_datetime(self, f=text.parse_datetime):
null = datetime.datetime.utcfromtimestamp(0)
self.assertEqual(f("1970-01-01T00:00:00+0000"), null)
self.assertEqual(f("1970.01.01", "%Y.%m.%d") , null)
self.assertEqual(f("1970-01-01T00:00:00+00:00"), null)
self.assertEqual(f("1970-01-01T00:00:00+0000") , null)
self.assertEqual(f("1970.01.01", "%Y.%m.%d") , null)
self.assertEqual(
f("2019-05-07T21:25:02+09:00"),
datetime.datetime(2019, 5, 7, 12, 25, 2),
)
self.assertEqual(
f("2019-05-07T21:25:02+0900"),
datetime.datetime(2019, 5, 7, 12, 25, 2),