Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit dbaf7d6

Browse files
committed
handle python < 3.10 to get utc time correctly
1 parent 036c5ed commit dbaf7d6

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

CloudFlare/tests/test_graphql.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55
import random
66
import datetime
7+
import pytz
78
import json
89

910
sys.path.insert(0, os.path.abspath('.'))
@@ -44,8 +45,12 @@ def test_find_zone(domain_name=None):
4445
print('zone: %s %s' % (zone_id, zone_name), file=sys.stderr)
4546

4647
def rfc3339_iso8601_time(hour_delta=0, with_hms=False):
47-
# format time (with an hour offset in RFC3339 ISO8601 format (and do it UTC time)
48-
dt = (datetime.datetime.now(datetime.UTC).replace(microsecond=0) + datetime.timedelta(hours=hour_delta))
48+
# format time (with an hour offset in RFC3339 or ISO8601 format (and do it UTC time)
49+
if sys.version_info[:3][0] <= 3 and sys.version_info[:3][1] <= 10:
50+
dt = datetime.datetime.utcnow().replace(microsecond=0, tzinfo=pytz.UTC)
51+
else:
52+
dt = datetime.datetime.now(datetime.UTC).replace(microsecond=0)
53+
dt += datetime.timedelta(hours=hour_delta)
4954
if with_hms:
5055
return dt.isoformat().replace('+00:00', 'Z')
5156
return dt.strftime('%Y-%m-%d')

CloudFlare/tests/test_images_v2_direct_upload.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import json
66
import random
77
import datetime
8+
import pytz
89

910
sys.path.insert(0, os.path.abspath('.'))
1011
sys.path.insert(0, os.path.abspath('..'))
@@ -15,8 +16,12 @@
1516
cf = None
1617

1718
def rfc3339_iso8601_time(hour_delta=0, with_hms=False):
18-
# format time (with an hour offset in RFC3339 ISO8601 format (and do it UTC time)
19-
dt = (datetime.datetime.now(datetime.UTC).replace(microsecond=0) + datetime.timedelta(hours=hour_delta))
19+
# format time (with an hour offset in RFC3339 or ISO8601 format (and do it UTC time)
20+
if sys.version_info[:3][0] <= 3 and sys.version_info[:3][1] <= 10:
21+
dt = datetime.datetime.utcnow().replace(microsecond=0, tzinfo=pytz.UTC)
22+
else:
23+
dt = datetime.datetime.now(datetime.UTC).replace(microsecond=0)
24+
dt += datetime.timedelta(hours=hour_delta)
2025
if with_hms:
2126
return dt.isoformat().replace('+00:00', 'Z')
2227
return dt.strftime('%Y-%m-%d')

0 commit comments

Comments
 (0)