From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id D49413858C31; Thu, 5 Jan 2023 16:28:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D49413858C31 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1672936134; bh=Yz5c8ClVoB5AGYXH3n4AQFoh7ht/yx4cqfLdHoc7QKw=; h=From:To:Subject:Date:From; b=wW8QICiicxbac5/ieWNL1bqPXIfvaDozFxY5XFqky3C0yGKr1cNXeGU51n5BMRjcG EdsGudJO2xGZohXHcTTXPa9fVKqGcsDmvcGtW173OwWVqYbtHXAmBGbilWJHOesYa5 APcrFdjsGTfvvIVYaG5HuxB3oW5LV6NiW4JJI/bs= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-5034] libstdc++: Fix printers for Python 2 [PR108212] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: e2eab3c4edb6aa9a93f982c4554cd756000934ca X-Git-Newrev: 80ff207da6d8784e227eb93f75c4ac5a300c8420 Message-Id: <20230105162854.D49413858C31@sourceware.org> Date: Thu, 5 Jan 2023 16:28:54 +0000 (GMT) List-Id: https://gcc.gnu.org/g:80ff207da6d8784e227eb93f75c4ac5a300c8420 commit r13-5034-g80ff207da6d8784e227eb93f75c4ac5a300c8420 Author: Jonathan Wakely Date: Thu Jan 5 13:41:21 2023 +0000 libstdc++: Fix printers for Python 2 [PR108212] 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. Diff: --- 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