Re: [PATCH 1 of 6] utils: stop using datetime.utcfromtimestamp() deprecated in Python 3.12

2023-06-29 Thread Mads Kiilerich

On 28/06/2023 16:43, Mads Kiilerich wrote:

# HG changeset patch
# User Mads Kiilerich 
# Date 1687866710 -7200
#  Tue Jun 27 13:51:50 2023 +0200
# Branch stable
# Node ID ec46e9b39b2a6f5d0c59aa803e2eb5d9dfc44cc3
# Parent  2b0598121a71fa19c2174e4eee3400ec3a3b1c26
utils: stop using datetime.utcfromtimestamp() deprecated in Python 3.12

Python3.12 made tests fail with warnings:
   DeprecationWarning: datetime.utcfromtimestamp() is deprecated and scheduled 
for removal in a future version. Use timezone-aware objects to represent 
datetimes in UTC: datetime.fromtimestamp(timestamp, datetime.UTC).

Computing the diff while in timestamp seconds seems to preserve to the original
intent from ae04af1ce78d.

It would be nice to have some doctest coverage of this, with the problematic
corner cases that has popped up over time...



Some potential test coverage that verified that the change preserve the 
fix from ae04af1ce78d:


    """Return a unix timestamp (or the current time) as a (unixtime,
    offset) tuple based off the local timezone.

    >>> import os, time
    >>> os.environ['TZ'] = 'Asia/Novokuznetsk'
    >>> time.tzset()

    >>> def dtu(*a):
    ...    return datetime.datetime(*a, tzinfo=datetime.timezone.utc)

    # Old winter timezone, +7
    >>> makedate(dtu(2010,  1,  1,  5,  0, 0).timestamp())
    (1262322000.0, -25200)

    # Same timezone in summer, +7, so no DST
    >>> makedate(dtu(2010,  7,  1,  5,  0, 0).timestamp())
    (1277960400.0, -25200)

    # Changing to new winter timezone, from +7 to +6 (ae04af1ce78d 
testcase)

    >>> makedate(dtu(2010, 10, 30, 20,  0, 0).timestamp() - 1)
    (1288468799.0, -25200)
    >>> makedate(dtu(2010, 10, 30, 20,  0, 0).timestamp())
    (1288468800.0, -21600)
    >>> makedate(dtu(2011,  1,  1,  5,  0, 0).timestamp())
    (1293858000.0, -21600)

    # Introducing DST, changing +6 to +7
    >>> makedate(dtu(2011,  3, 26, 20,  0, 0).timestamp() - 1)
    (1301169599.0, -21600)
    >>> makedate(dtu(2011,  3, 26, 20,  0, 0).timestamp())
    (1301169600.0, -25200)
    """

but it relies heavily on a corner case in the time database and setting 
the timezone globally, so it might be fragile and not a good candidate 
for adding to the test suite. The functions are already accidentally 
covered by the suite.


/Mads
___
Mercurial-devel mailing list
Mercurial-devel@lists.mercurial-scm.org
https://lists.mercurial-scm.org/mailman/listinfo/mercurial-devel


[PATCH 1 of 6] utils: stop using datetime.utcfromtimestamp() deprecated in Python 3.12

2023-06-28 Thread Mads Kiilerich
# HG changeset patch
# User Mads Kiilerich 
# Date 1687866710 -7200
#  Tue Jun 27 13:51:50 2023 +0200
# Branch stable
# Node ID ec46e9b39b2a6f5d0c59aa803e2eb5d9dfc44cc3
# Parent  2b0598121a71fa19c2174e4eee3400ec3a3b1c26
utils: stop using datetime.utcfromtimestamp() deprecated in Python 3.12

Python3.12 made tests fail with warnings:
  DeprecationWarning: datetime.utcfromtimestamp() is deprecated and scheduled 
for removal in a future version. Use timezone-aware objects to represent 
datetimes in UTC: datetime.fromtimestamp(timestamp, datetime.UTC).

Computing the diff while in timestamp seconds seems to preserve to the original
intent from ae04af1ce78d.

It would be nice to have some doctest coverage of this, with the problematic
corner cases that has popped up over time...

diff --git a/hgext/convert/common.py b/hgext/convert/common.py
--- a/hgext/convert/common.py
+++ b/hgext/convert/common.py
@@ -567,8 +567,10 @@ class mapfile(dict):
 
 def makedatetimestamp(t):
 """Like dateutil.makedate() but for time t instead of current time"""
-delta = datetime.datetime.utcfromtimestamp(
+tz = round(
 t
-) - datetime.datetime.fromtimestamp(t)
-tz = delta.days * 86400 + delta.seconds
+- datetime.datetime.fromtimestamp(t)
+.replace(tzinfo=datetime.timezone.utc)
+.timestamp()
+)
 return t, tz
diff --git a/mercurial/utils/dateutil.py b/mercurial/utils/dateutil.py
--- a/mercurial/utils/dateutil.py
+++ b/mercurial/utils/dateutil.py
@@ -83,10 +83,14 @@ def makedate(timestamp=None):
 raise error.InputError(
 _(b"negative timestamp: %d") % timestamp, hint=hint
 )
-delta = datetime.datetime.utcfromtimestamp(
+tz = round(
 timestamp
-) - datetime.datetime.fromtimestamp(timestamp)
-tz = delta.days * 86400 + delta.seconds
+- datetime.datetime.fromtimestamp(
+timestamp,
+)
+.replace(tzinfo=datetime.timezone.utc)
+.timestamp()
+)
 return timestamp, tz
 
 

___
Mercurial-devel mailing list
Mercurial-devel@lists.mercurial-scm.org
https://lists.mercurial-scm.org/mailman/listinfo/mercurial-devel