From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed] libstdc++: Fix <chrono> printers for Python 2 [PR108212]
Date: Thu, 5 Jan 2023 16:30:07 +0000 [thread overview]
Message-ID: <20230105163007.82096-1-jwakely@redhat.com> (raw)
Tested powerpc64le-linux (python 2.7) and x86_64-linux (Python 3.10).
Pushed to trunk.
-- >8 --
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 | 19 +++++++++++++++++--
1 file changed, 17 insertions(+), 2 deletions(-)
diff --git a/libstdc++-v3/python/libstdcxx/v6/printers.py b/libstdc++-v3/python/libstdcxx/v6/printers.py
index 7e694f48f28..8cfb4f26b0e 100644
--- a/libstdc++-v3/python/libstdcxx/v6/printers.py
+++ b/libstdc++-v3/python/libstdcxx/v6/printers.py
@@ -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
--
2.39.0
reply other threads:[~2023-01-05 16:30 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230105163007.82096-1-jwakely@redhat.com \
--to=jwakely@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=libstdc++@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).