Skip to content

Commit cbc5ca9

Browse files
committed
Merge branch 'mr/trespeuch/ruff' into 'master'
Fix ruff DTZ violations See merge request it/e3-aws!133
2 parents ee22f38 + c617e5e commit cbc5ca9

File tree

5 files changed

+55
-7
lines changed

5 files changed

+55
-7
lines changed

src/e3/aws/cfn/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77
import time
88
import uuid
99
from datetime import datetime
10+
11+
try:
12+
from datetime import UTC
13+
except ImportError:
14+
# Python < 3.11 does not have datetime.UTC
15+
from datetime import timezone
16+
17+
UTC = timezone.utc
18+
1019
from enum import Enum
1120

1221
import botocore.client
@@ -490,7 +499,7 @@ def __init__(
490499
"deploying the stack (see Stack cfn_role_arn parameter)"
491500
)
492501
self.cfn_role_arn = cfn_role_arn
493-
self.creation_date = datetime.now().timestamp()
502+
self.creation_date = datetime.now(tz=UTC).timestamp()
494503

495504
# The uuid is used by create and create_change_set. It allows then
496505
# to track for example events associated with that deployment

src/e3/aws/cfn/main.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111
import tempfile
1212
import time
1313
from datetime import datetime
14+
15+
try:
16+
from datetime import UTC
17+
except ImportError:
18+
# Python < 3.11 does not have datetime.UTC
19+
from datetime import timezone
20+
21+
UTC = timezone.utc
22+
23+
1424
from pathlib import Path
1525

1626
import botocore.exceptions
@@ -171,7 +181,7 @@ def __init__(
171181
self.aws_env: Session | AWSEnv | None = None
172182
self.deploy_branch = deploy_branch
173183

174-
self.timestamp = datetime.utcnow().strftime("%Y-%m-%d/%H:%M:%S.%f")
184+
self.timestamp = datetime.now(tz=UTC).strftime("%Y-%m-%d/%H:%M:%S.%f")
175185

176186
if s3_bucket is not None:
177187
s3_root_key = f"{s3_key.strip('/')}/"

src/e3/aws/ec2/ami.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55
import re
66
from datetime import datetime
77

8+
try:
9+
from datetime import UTC
10+
except ImportError:
11+
# Python < 3.11 does not have datetime.UTC
12+
from datetime import timezone
13+
14+
UTC = timezone.utc
15+
16+
817
from dateutil.parser import parse as parse_date
918

1019
from e3.aws import session
@@ -67,12 +76,15 @@ def creation_date(self) -> datetime:
6776
6877
:return: AMI creation date
6978
"""
70-
return parse_date(self.data["CreationDate"]).replace(tzinfo=None)
79+
creation_date = parse_date(self.data["CreationDate"])
80+
if creation_date.tzinfo is None:
81+
creation_date = creation_date.replace(tzinfo=UTC)
82+
return creation_date
7183

7284
@property
7385
def age(self) -> int:
7486
"""Return age of the AMI in days."""
75-
age = datetime.now() - self.creation_date
87+
age = datetime.now(tz=UTC) - self.creation_date
7688
return int(age.total_seconds() / (3600 * 24))
7789

7890
@property

tests/tests_e3_aws/cfn/main/main_test.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44

55
import textwrap
66
from datetime import datetime
7+
8+
try:
9+
from datetime import UTC
10+
except ImportError:
11+
# Python < 3.11 does not have datetime.UTC
12+
from datetime import timezone
13+
14+
UTC = timezone.utc
15+
716
from pathlib import Path
817

918
import pytest
@@ -129,7 +138,7 @@ def create_stack(self) -> list[Stack]:
129138
"Stacks": [
130139
{
131140
"StackName": stack_name,
132-
"CreationTime": datetime(2016, 1, 20, 22, 9),
141+
"CreationTime": datetime(2016, 1, 20, 22, 9, tzinfo=UTC),
133142
"StackStatus": "CREATE_COMPLETE",
134143
"StackId": stack_name + "1",
135144
}
@@ -174,7 +183,7 @@ def create_stack(self) -> list[Stack]:
174183
"Stacks": [
175184
{
176185
"StackName": stack_name,
177-
"CreationTime": datetime(2016, 1, 20, 22, 9),
186+
"CreationTime": datetime(2016, 1, 20, 22, 9, tzinfo=UTC),
178187
"StackStatus": "UPDATE_COMPLETE",
179188
"StackId": stack_name + "1",
180189
}

tests/tests_e3_aws/session/session_test.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44

55
from datetime import datetime
66

7+
try:
8+
from datetime import UTC
9+
except ImportError:
10+
# Python < 3.11 does not have datetime.UTC
11+
from datetime import timezone
12+
13+
UTC = timezone.utc
14+
715
import pytest
816

917
from e3.aws import AWSEnv, AWSSessionRunError, Session
@@ -28,7 +36,7 @@ def test_run(capfd: CaptureFixture) -> None:
2836
"AccessKeyId": "12345678912345678",
2937
"SecretAccessKey": "12345678912345678",
3038
"SessionToken": "12345678912345678",
31-
"Expiration": datetime(4042, 1, 1),
39+
"Expiration": datetime(4042, 1, 1, tzinfo=UTC),
3240
}
3341
},
3442
{

0 commit comments

Comments
 (0)