Skip to content

Commit c0995f1

Browse files
committed
Use timezone aware datetime
The official Python docs recommend explicitly passing a timezone object instead of using `utcnow` or `utcfromtimestamp`: https://docs.python.org/3/library/datetime.html#datetime.datetime.utcnow https://docs.python.org/3/library/datetime.html#datetime.datetime.utcfromtimestamp
1 parent 8171452 commit c0995f1

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/setuptools_scm/version.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
22
import re
3-
import time
43
import warnings
54
from datetime import datetime
65
from datetime import timezone
@@ -119,9 +118,11 @@ def __init__(
119118
self.distance = distance
120119
self.node = node
121120
self.node_date = node_date
122-
self.time = datetime.utcfromtimestamp(
123-
int(os.environ.get("SOURCE_DATE_EPOCH", time.time()))
124-
)
121+
if "SOURCE_DATE_EPOCH" in os.environ:
122+
date_epoch = int(os.environ["SOURCE_DATE_EPOCH"])
123+
self.time = datetime.fromtimestamp(date_epoch, timezone.utc)
124+
else:
125+
self.time = datetime.now(timezone.utc)
125126
self._extra = kw
126127
self.dirty = dirty
127128
self.preformatted = preformatted

testing/test_git.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import sys
33
from datetime import date
44
from datetime import datetime
5+
from datetime import timezone
56
from os.path import join as opj
67
from unittest.mock import Mock
78
from unittest.mock import patch
@@ -215,7 +216,7 @@ def test_git_dirty_notag(today, wd, monkeypatch):
215216
assert wd.version.startswith("0.1.dev1")
216217
if today:
217218
# the date on the tag is in UTC
218-
tag = datetime.utcnow().date().strftime(".d%Y%m%d")
219+
tag = datetime.now(timezone.utc).date().strftime(".d%Y%m%d")
219220
else:
220221
tag = ".d20090213"
221222
# we are dirty, check for the tag

0 commit comments

Comments
 (0)