From 20d7c8f86d2a3b17e0d9be86fe7fce3d9ff52aec Mon Sep 17 00:00:00 2001 From: Tessa Walsh Date: Mon, 11 Nov 2024 17:57:54 -0500 Subject: [PATCH] Switch tzinfo arg to aware bool since times should always be utc --- warcio/timeutils.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/warcio/timeutils.py b/warcio/timeutils.py index cf8a857c..3d8db4d0 100644 --- a/warcio/timeutils.py +++ b/warcio/timeutils.py @@ -25,7 +25,7 @@ PAD_MICRO = '000000' -def iso_date_to_datetime(string, tzinfo: timezone = None): +def iso_date_to_datetime(string, aware=False): """ >>> iso_date_to_datetime('2013-12-26T10:11:12Z') datetime.datetime(2013, 12, 26, 10, 11, 12) @@ -48,10 +48,10 @@ def iso_date_to_datetime(string, tzinfo: timezone = None): >>> iso_date_to_datetime('2013-12-26T10:11:12.000000Z') datetime.datetime(2013, 12, 26, 10, 11, 12) - >>> iso_date_to_datetime('2013-12-26T10:11:12Z', tzinfo=timezone.utc) + >>> iso_date_to_datetime('2013-12-26T10:11:12Z', aware=True) datetime.datetime(2013, 12, 26, 10, 11, 12, tzinfo=datetime.timezone.utc) - >>> iso_date_to_datetime('2013-12-26T10:11:12.000000Z', tzinfo=timezone.utc) + >>> iso_date_to_datetime('2013-12-26T10:11:12.000000Z', aware=True) datetime.datetime(2013, 12, 26, 10, 11, 12, tzinfo=datetime.timezone.utc) """ @@ -63,18 +63,26 @@ def iso_date_to_datetime(string, tzinfo: timezone = None): nums[6] = nums[6][:6] nums[6] += PAD_MICRO[len(nums[6]):] + tz_info = None + if aware: + tz_info = timezone.utc + the_datetime = datetime(*(int(num) for num in nums), tzinfo=tzinfo) return the_datetime -def http_date_to_datetime(string, tzinfo: timezone = None): +def http_date_to_datetime(string, aware=False): """ >>> http_date_to_datetime('Thu, 26 Dec 2013 09:50:10 GMT') datetime.datetime(2013, 12, 26, 9, 50, 10) - >>> http_date_to_datetime('Thu, 26 Dec 2013 09:50:10 GMT', tzinfo=timezone.utc) + >>> http_date_to_datetime('Thu, 26 Dec 2013 09:50:10 GMT', aware=True) datetime.datetime(2013, 12, 26, 9, 50, 10, tzinfo=datetime.timezone.utc) """ + tz_info = None + if aware: + tz_info = timezone.utc + return datetime(*parsedate(string)[:6], tzinfo=tzinfo) @@ -212,7 +220,7 @@ def pad_timestamp(string, pad_str=PAD_6_UP): return string -def timestamp_to_datetime(string, tzinfo: timezone = None): +def timestamp_to_datetime(string, aware=False): """ # >14-digit -- rest ignored >>> timestamp_to_datetime('2014122609501011') @@ -295,15 +303,15 @@ def timestamp_to_datetime(string, tzinfo: timezone = None): datetime.datetime(2010, 12, 31, 23, 59, 59) # 14-digit with tzinfo - >>> timestamp_to_datetime('20141226095010', tzinfo=timezone.utc) + >>> timestamp_to_datetime('20141226095010', aware=True) datetime.datetime(2014, 12, 26, 9, 50, 10, tzinfo=datetime.timezone.utc) # 6-digit padding with tzinfo - >>> timestamp_to_datetime('201410', tzinfo=timezone.utc) + >>> timestamp_to_datetime('201410', aware=True) datetime.datetime(2014, 10, 31, 23, 59, 59, tzinfo=datetime.timezone.utc) # not a number! with tzinfo - >>> timestamp_to_datetime('2010abc', tzinfo=timezone.utc) + >>> timestamp_to_datetime('2010abc', aware=True) datetime.datetime(2010, 12, 31, 23, 59, 59, tzinfo=datetime.timezone.utc) """ @@ -333,6 +341,10 @@ def extract(string, start, end, min_, max_): minute = extract(string, 10, 12, 0, 59) second = extract(string, 12, 14, 0, 59) + tzinfo = None + if aware: + tzinfo = timezone.utc + return datetime(year=year, month=month, day=day,