From c0995f1af190617a6ca4171e49c4af04d6adf526 Mon Sep 17 00:00:00 2001 From: Anderson Bravalheri Date: Mon, 17 Jan 2022 18:11:18 +0000 Subject: 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 --- src/setuptools_scm/version.py | 9 +++++---- 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 -- cgit v1.2.1