From a0f4c295c07bafb9519fc5d4a7a1dc8756275ab1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Sat, 11 Apr 2020 02:05:00 +0200 Subject: [PATCH] add optional 'utcoffset' argument to 'parse_datetime()' --- gallery_dl/text.py | 8 ++++++-- test/test_text.py | 6 +++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/gallery_dl/text.py b/gallery_dl/text.py index a3f4e0ac..3bb63903 100644 --- a/gallery_dl/text.py +++ b/gallery_dl/text.py @@ -233,7 +233,7 @@ def parse_timestamp(ts, default=None): return default -def parse_datetime(date_string, format="%Y-%m-%dT%H:%M:%S%z"): +def parse_datetime(date_string, format="%Y-%m-%dT%H:%M:%S%z", utcoffset=0): """Create a datetime object by parsing 'date_string'""" try: if format.endswith("%z") and date_string[-3] == ":": @@ -244,7 +244,11 @@ def parse_datetime(date_string, format="%Y-%m-%dT%H:%M:%S%z"): d = datetime.datetime.strptime(ds, format) o = d.utcoffset() if o is not None: - d = d.replace(tzinfo=None) - o # convert to naive UTC + # convert to naive UTC + d = d.replace(tzinfo=None) - o + elif utcoffset: + # apply manual UTC offset + d += datetime.timedelta(0, utcoffset * -3600) return d except (TypeError, IndexError, KeyError): return None diff --git a/test/test_text.py b/test/test_text.py index 6a6d83ae..03908236 100644 --- a/test/test_text.py +++ b/test/test_text.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -# Copyright 2015-2018 Mike Fährmann +# Copyright 2015-2020 Mike Fährmann # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as @@ -375,6 +375,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", "%Y-%m-%dT%H:%M:%S", utcoffset=9), + datetime.datetime(2019, 5, 7, 12, 25, 2), + ) self.assertEqual( f("2019-05-07 21:25:02"), "2019-05-07 21:25:02",