Patchwork [v8] Allow commit date before Unix epoch, clean fix for (issue2513)

login
register
mail settings
Submitter Florent Gallaire
Date April 8, 2016, 4:05 p.m.
Message ID <CAB9F1cd1Uw1km8fWwtWa5gOz5qF6M7=uR69GFFLyeTCcyiXm3Q@mail.gmail.com>
Download mbox | patch
Permalink /patch/14435/
State Changes Requested
Delegated to: Yuya Nishihara
Headers show

Comments

Florent Gallaire - April 8, 2016, 4:05 p.m.
# HG changeset patch
# User Florent Gallaire <fgallaire@gmail.com>
# Date 1460131286 -7200
#      Fri Apr 08 18:01:26 2016 +0200
# Node ID 9060da16455219aec0bd8ed277c4eee5555266bc
# Parent  c5565fc8848dd084d104ca40c33d1acdfcff8bc6
date: reallow negative timestamp, fix for Windows buggy gmtime() (issue2513)

DVCS are very useful to store various texts (as legislation) written before
Unix epoch. Fri, 13 Dec 1901 is a nice gain over Thu, 01 Jan 1970.
Revert dd24f3e7ca9e and e1002cf9fe54, fix c208dcd0f709. Add tests.

Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Yuya Nishihara - April 9, 2016, 3:44 a.m.
On Fri, 8 Apr 2016 18:05:13 +0200, Florent Gallaire wrote:
> # HG changeset patch
> # User Florent Gallaire <fgallaire@gmail.com>
> # Date 1460131286 -7200
> #      Fri Apr 08 18:01:26 2016 +0200
> # Node ID 9060da16455219aec0bd8ed277c4eee5555266bc
> # Parent  c5565fc8848dd084d104ca40c33d1acdfcff8bc6
> date: reallow negative timestamp, fix for Windows buggy gmtime() (issue2513)

This patch is getting bigger, and we prefer small patches that do one
functional change per patch.

https://www.mercurial-scm.org/wiki/ContributingChanges#Organizing_patches

Can you make a separate patch to fix boundary check of negative integer?

Patch

diff -r c5565fc8848d -r 9060da164552 mercurial/util.py
--- a/mercurial/util.py    Wed Apr 06 22:26:47 2016 -0400
+++ b/mercurial/util.py    Fri Apr 08 18:01:26 2016 +0200
@@ -1585,9 +1585,6 @@ 
     number of seconds away from UTC. if timezone is false, do not
     append time zone to string."""
     t, tz = date or makedate()
-    if t < 0:
-        t = 0   # time.gmtime(lt) fails on Windows for lt < -43200
-        tz = 0
     if "%1" in format or "%2" in format or "%z" in format:
         sign = (tz > 0) and "-" or "+"
         minutes = abs(tz) // 60
@@ -1595,12 +1592,16 @@ 
         format = format.replace("%z", "%1%2")
         format = format.replace("%1", "%c%02d" % (sign, q))
         format = format.replace("%2", "%02d" % r)
-    try:
-        t = time.gmtime(float(t) - tz)
-    except ValueError:
-        # time was out of range
-        t = time.gmtime(sys.maxint)
-    s = time.strftime(format, t)
+    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 = t.strftime(format)
     return s

 def shortdate(date=None):
@@ -1719,10 +1720,8 @@ 
     # time zone offset. values must fit in signed 32 bits for
     # current 32-bit linux runtimes. timezones go from UTC-12
     # to UTC+14
-    if abs(when) > 0x7fffffff:
+    if when < -0x80000000 or when > 0x7fffffff:
         raise Abort(_('date exceeds 32 bits: %d') % when)
-    if when < 0:
-        raise Abort(_('negative date value: %d') % when)
     if offset < -50400 or offset > 43200:
         raise Abort(_('impossible time zone offset: %d') % offset)
     return when, offset
diff -r c5565fc8848d -r 9060da164552 tests/test-commit.t
--- a/tests/test-commit.t    Wed Apr 06 22:26:47 2016 -0400
+++ b/tests/test-commit.t    Fri Apr 08 18:01:26 2016 +0200
@@ -27,8 +27,21 @@ 
   $ hg commit -d '111111111111 0' -m commit-7
   abort: date exceeds 32 bits: 111111111111
   [255]
-  $ hg commit -d '-7654321 3600' -m commit-7
-  abort: negative date value: -7654321
+  $ hg commit -d '-111111111111 0' -m commit-7
+  abort: date exceeds 32 bits: -111111111111
+  [255]
+  $ echo foo >> foo
+  $ hg commit -d '1901-12-13 20:45:52 +0000' -m commit-7-2
+  $ echo foo >> foo
+  $ hg commit -d '-2147483648 0' -m commit-7-3
+  $ hg log -T '{rev} {date|isodatesec}\n' -l2
+  3 1901-12-13 20:45:52 +0000
+  2 1901-12-13 20:45:52 +0000
+  $ hg commit -d '1901-12-13 20:45:51 +0000' -m commit-7
+  abort: date exceeds 32 bits: -2147483649
+  [255]
+  $ hg commit -d '-2147483649 0' -m commit-7
+  abort: date exceeds 32 bits: -2147483649
   [255]

 commit added file that has been deleted
@@ -54,7 +67,7 @@ 
   dir/file
   committing manifest
   committing changelog
-  committed changeset 2:d2a76177cb42
+  committed changeset 4:1957363f1ced

   $ echo > dir.file
   $ hg add
@@ -78,7 +91,7 @@ 
   dir/file
   committing manifest
   committing changelog
-  committed changeset 3:1cd62a2d8db5
+  committed changeset 5:a31d8f87544a
   $ cd ..

   $ hg commit -m commit-14 does-not-exist
@@ -102,7 +115,7 @@ 
   dir/file
   committing manifest
   committing changelog
-  committed changeset 4:49176991390e
+  committed changeset 6:32d054c9d085

 An empty date was interpreted as epoch origin
_______________________________________________