Skip to content

Commit 0e10353

Browse files
committed
Fix timezone offset error
The previous implementation raised a TypeError when the ISO date string included a timezone offset. This is now resolved by making use of datetime.fromisoformat to encounter the offset problems, which correctly handles timezone-aware ISO strings. Removing PAD_micro variable
1 parent d449a8e commit 0e10353

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

warcio/timeutils.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import time
88
import calendar
99

10-
from datetime import datetime, timezone
10+
from datetime import datetime, timezone, timedelta
1111
from email.utils import parsedate, formatdate
1212

1313
#=================================================================
@@ -22,7 +22,6 @@
2222
PAD_14_DOWN = '10000101000000'
2323
PAD_14_UP = '29991231235959'
2424
PAD_6_UP = '299912'
25-
PAD_MICRO = '000000'
2625

2726

2827
def iso_date_to_datetime(string, tz_aware=False):
@@ -53,22 +52,17 @@ def iso_date_to_datetime(string, tz_aware=False):
5352
5453
>>> iso_date_to_datetime('2013-12-26T10:11:12.000000Z', tz_aware=True)
5554
datetime.datetime(2013, 12, 26, 10, 11, 12, tzinfo=datetime.timezone.utc)
56-
"""
57-
58-
nums = DATE_TIMESPLIT.split(string)
59-
if nums[-1] == '':
60-
nums = nums[:-1]
61-
62-
if len(nums) == 7:
63-
nums[6] = nums[6][:6]
64-
nums[6] += PAD_MICRO[len(nums[6]):]
55+
56+
>>> iso_date_to_datetime('2013-12-26T10:11:12.000000+02:00', tz_aware=True)
57+
datetime.datetime(2013, 12, 26, 10, 11, 12, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200)))
6558
66-
tzinfo = None
67-
if tz_aware:
68-
tzinfo = timezone.utc
69-
70-
the_datetime = datetime(*(int(num) for num in nums), tzinfo=tzinfo)
71-
return the_datetime
59+
60+
>>> iso_date_to_datetime('2013-12-26T10:11:12.000000-02:00', tz_aware=True) == datetime(2013, 12, 26, 10, 11, 12, tzinfo=timezone(timedelta(seconds=-7200)))
61+
True
62+
"""
63+
if not tz_aware:
64+
return datetime.fromisoformat(string).replace(tzinfo=None)
65+
return datetime.fromisoformat(string)
7266

7367

7468
def http_date_to_datetime(string, tz_aware=False):

0 commit comments

Comments
 (0)