diff options
author | Anderson Bravalheri <andersonbravalheri@gmail.com> | 2022-01-17 18:11:18 +0000 |
---|---|---|
committer | Anderson Bravalheri <andersonbravalheri@gmail.com> | 2022-01-17 18:24:55 +0000 |
commit | c0995f1af190617a6ca4171e49c4af04d6adf526 (patch) | |
tree | bf74c86cd92e0fe2e9610c1553f250f659715450 | |
parent | 81714522b68cc84547f49ca6fc1ab4e46daaad2c (diff) | |
download | setuptools-scm-c0995f1af190617a6ca4171e49c4af04d6adf526.tar.gz |
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
-rw-r--r-- | src/setuptools_scm/version.py | 9 | ||||
-rw-r--r-- | testing/test_git.py | 3 |
2 files changed, 7 insertions, 5 deletions
diff --git a/src/setuptools_scm/version.py b/src/setuptools_scm/version.py index ccbfa2b..faaf8fa 100644 --- a/src/setuptools_scm/version.py +++ b/src/setuptools_scm/version.py @@ -1,6 +1,5 @@ import os import re -import time import warnings from datetime import datetime from datetime import timezone @@ -119,9 +118,11 @@ class ScmVersion: self.distance = distance self.node = node self.node_date = node_date - self.time = datetime.utcfromtimestamp( - int(os.environ.get("SOURCE_DATE_EPOCH", time.time())) - ) + if "SOURCE_DATE_EPOCH" in os.environ: + date_epoch = int(os.environ["SOURCE_DATE_EPOCH"]) + self.time = datetime.fromtimestamp(date_epoch, timezone.utc) + else: + self.time = datetime.now(timezone.utc) self._extra = kw self.dirty = dirty self.preformatted = preformatted diff --git a/testing/test_git.py b/testing/test_git.py index c911551..b1b42ac 100644 --- a/testing/test_git.py +++ b/testing/test_git.py @@ -2,6 +2,7 @@ import os import sys from datetime import date from datetime import datetime +from datetime import timezone from os.path import join as opj from unittest.mock import Mock from unittest.mock import patch @@ -215,7 +216,7 @@ def test_git_dirty_notag(today, wd, monkeypatch): assert wd.version.startswith("0.1.dev1") if today: # the date on the tag is in UTC - tag = datetime.utcnow().date().strftime(".d%Y%m%d") + tag = datetime.now(timezone.utc).date().strftime(".d%Y%m%d") else: tag = ".d20090213" # we are dirty, check for the tag |