D369: util: add `hgdatetopython` to convert hg-style dates to datetimes
phillco (Phil Cohen)
phabricator at mercurial-scm.org
Mon Aug 14 03:06:12 UTC 2017
phillco created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.
REVISION SUMMARY
Extract the existing code from ``datestr``.
REPOSITORY
rHG Mercurial
REVISION DETAIL
https://phab.mercurial-scm.org/D369
AFFECTED FILES
mercurial/util.py
CHANGE DETAILS
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -1858,6 +1858,19 @@
tz = delta.days * 86400 + delta.seconds
return timestamp, tz
+def hgdatetopython(date):
+ """Returns a Python datetime from a Mercurial tuple-style date"""
+ t, tz = date
+ d = t - tz
+ if d > 0x7fffffff:
+ d = 0x7fffffff
+ elif d < -0x80000000:
+ d = -0x80000000
+ # Never use time.gmtime() and datetime.datetime.fromtimestamp()
+ # because they use the gmtime() system call which is buggy on Windows
+ # for negative values.
+ return datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=d)
+
def datestr(date=None, format='%a %b %d %H:%M:%S %Y %1%2'):
"""represent a (unixtime, offset) tuple as a localized time.
unixtime is seconds since the epoch, and offset is the time zone's
@@ -1882,17 +1895,8 @@
format = format.replace("%z", "%1%2")
format = format.replace("%1", "%c%02d" % (sign, q))
format = format.replace("%2", "%02d" % r)
- d = t - tz
- if d > 0x7fffffff:
- d = 0x7fffffff
- elif d < -0x80000000:
- d = -0x80000000
- # Never use time.gmtime() and datetime.datetime.fromtimestamp()
- # because they use the gmtime() system call which is buggy on Windows
- # for negative values.
- t = datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=d)
- s = encoding.strtolocal(t.strftime(encoding.strfromlocal(format)))
- return s
+ t = hgdatetopython((t, tz))
+ return encoding.strtolocal(t.strftime(encoding.strfromlocal(format)))
def shortdate(date=None):
"""turn (timestamp, tzoff) tuple into iso 8631 date."""
To: phillco, #hg-reviewers
Cc: mercurial-devel
More information about the Mercurial-devel
mailing list