]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Fix <chrono> printers for Python 2 [PR108212]
authorJonathan Wakely <jwakely@redhat.com>
Thu, 5 Jan 2023 13:41:21 +0000 (13:41 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Thu, 5 Jan 2023 16:28:43 +0000 (16:28 +0000)
The datetime.timezone.utc singleton doesn't exist in Python 2, but we
can create it ourselves by deriving from datetime.tzinfo.

libstdc++-v3/ChangeLog:

PR libstdc++/108212
* python/libstdcxx/v6/printers.py (_utc_timezone): New global
variable.
(StdChronoTimePointPrinter::to_string): Use it.

libstdc++-v3/python/libstdcxx/v6/printers.py

index 7e694f48f28d9a1b835fcf569e4600909a054ffe..8cfb4f26b0e04efe71761126768c941e7bfa6304 100644 (file)
@@ -19,7 +19,7 @@ import gdb
 import itertools
 import re
 import sys, os, errno
-from datetime import datetime, timezone
+import datetime
 
 ### Python 2 + Python 3 compatibility code
 
@@ -45,6 +45,7 @@ if sys.version_info[0] > 2:
     izip = zip
     # Also, int subsumes long
     long = int
+    _utc_timezone = datetime.timezone.utc
 else:
     ### Python 2 stuff
     class Iterator:
@@ -64,6 +65,20 @@ else:
     # In Python 2, we still need these from itertools
     from itertools import imap, izip
 
+    # Python 2 does not provide the datetime.UTC singleton.
+    class UTC(datetime.tzinfo):
+        """Concrete tzinfo class representing the UTC time zone"""
+
+        def utcoffset(self, dt):
+            return datetime.timedelta(0)
+
+        def tzname(self, dt):
+            return "UTC"
+
+        def dst(self, dt):
+            return datetime.timedelta(0)
+    _utc_timezone = UTC()
+
 # Try to use the new-style pretty-printing if available.
 _use_gdb_pp = True
 try:
@@ -1955,7 +1970,7 @@ class StdChronoTimePointPrinter:
             num, den = printer._ratio()
             secs = (r * num / den) + offset
             try:
-                dt = datetime.fromtimestamp(secs, timezone.utc)
+                dt = datetime.fromtimestamp(secs, _utc_timezone)
                 time = ' [{:%Y-%m-%d %H:%M:%S}]'.format(dt)
             except:
                 pass