Skip to content

Commit

Permalink
Switch tzinfo arg to aware bool since times should always be utc
Browse files Browse the repository at this point in the history
  • Loading branch information
tw4l committed Nov 11, 2024
1 parent 349ac49 commit 20d7c8f
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions warcio/timeutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
"""

Expand All @@ -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)


Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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)
"""
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 20d7c8f

Please sign in to comment.