Sfoglia il codice sorgente

python 3.12: remove deprecated datetime.utcfromtimestamp

lauren n. liberda 9 mesi fa
parent
commit
d81aeef9f8
1 ha cambiato i file con 19 aggiunte e 1 eliminazioni
  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.
 """
 
+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):
     """Make the directory, and all its ancestors as required.  Any of the
     directories are allowed to already exist."""
@@ -139,7 +157,7 @@ def describe(directory):
             # clock time with environment variable SOURCE_DATE_EPOCH
             # containing a (presumably) fixed timestamp.
             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)
 
 def parse_args():