Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/utils/dateutil.py @ 50748:faccec1edc2c stable
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...
author | Mads Kiilerich <mads@kiilerich.com> |
---|---|
date | Tue, 27 Jun 2023 13:51:50 +0200 |
parents | 6000f5b25c9b |
children | 9d3721552b6c |
comparison
equal
deleted
inserted
replaced
50747:fa2eca7423f3 | 50748:faccec1edc2c |
---|---|
81 if timestamp < 0: | 81 if timestamp < 0: |
82 hint = _(b"check your clock") | 82 hint = _(b"check your clock") |
83 raise error.InputError( | 83 raise error.InputError( |
84 _(b"negative timestamp: %d") % timestamp, hint=hint | 84 _(b"negative timestamp: %d") % timestamp, hint=hint |
85 ) | 85 ) |
86 delta = datetime.datetime.utcfromtimestamp( | 86 tz = round( |
87 timestamp | 87 timestamp |
88 ) - datetime.datetime.fromtimestamp(timestamp) | 88 - datetime.datetime.fromtimestamp( |
89 tz = delta.days * 86400 + delta.seconds | 89 timestamp, |
90 ) | |
91 .replace(tzinfo=datetime.timezone.utc) | |
92 .timestamp() | |
93 ) | |
90 return timestamp, tz | 94 return timestamp, tz |
91 | 95 |
92 | 96 |
93 def datestr(date=None, format=b'%a %b %d %H:%M:%S %Y %1%2'): | 97 def datestr(date=None, format=b'%a %b %d %H:%M:%S %Y %1%2'): |
94 # type: (Optional[hgdate], bytes) -> bytes | 98 # type: (Optional[hgdate], bytes) -> bytes |