瀏覽代碼

python 3.12: remove deprecated datetime.utcfromtimestamp

lauren n. liberda 10 月之前
父節點
當前提交
d81aeef9f8
共有 1 個文件被更改,包括 19 次插入1 次删除
  1. 19 1
      build_info.py

+ 19 - 1
build_info.py

@@ -49,6 +49,24 @@ tokens substituted:
    ommitted then the string is printed to stdout.
    ommitted then the string is printed to stdout.
 """
 """
 
 
+try:
+    utc = datetime.timezone.utc
+except AttributeError:
+    # Python 2? In datetime.date.today().year? Yes.
+    class UTC(datetime.tzinfo):
+        ZERO = datetime.timedelta(0)
+
+        def utcoffset(self, dt):
+            return self.ZERO
+
+        def tzname(self, dt):
+            return "UTC"
+
+        def dst(self, dt):
+            return self.ZERO
+    utc = UTC()
+
+
 def mkdir_p(directory):
 def mkdir_p(directory):
     """Make the directory, and all its ancestors as required.  Any of the
     """Make the directory, and all its ancestors as required.  Any of the
     directories are allowed to already exist."""
     directories are allowed to already exist."""
@@ -139,7 +157,7 @@ def describe(directory):
             # clock time with environment variable SOURCE_DATE_EPOCH
             # clock time with environment variable SOURCE_DATE_EPOCH
             # containing a (presumably) fixed timestamp.
             # containing a (presumably) fixed timestamp.
             timestamp = int(os.environ.get('SOURCE_DATE_EPOCH', time.time()))
             timestamp = int(os.environ.get('SOURCE_DATE_EPOCH', time.time()))
-            formatted = datetime.datetime.utcfromtimestamp(timestamp).isoformat()
+            formatted = datetime.datetime.fromtimestamp(timestamp, utc).isoformat()
             return 'unknown hash, {}'.format(formatted)
             return 'unknown hash, {}'.format(formatted)
 
 
 def parse_args():
 def parse_args():