[PATCH STABLE] util.datestr: do not crash on revisions with negative timestamp (issue2513)

Benjamin Pollack benjamin at bitquabit.com
Tue Nov 23 07:36:52 CST 2010


On Nov 23, 2010, at 8:28 AM, Adrian Buehlmann wrote:

> # HG changeset patch
> # User Adrian Buehlmann <adrian at cadifra.com>
> # Date 1290514300 -3600
> # Branch stable
> # Node ID 9c16c05a8e0fec5d0449b2464b6a52ce9a6ce6d6
> # Parent  f08df4d38442bf641859f3de860ce0e5b6ba7763
> util.datestr: do not crash on revisions with negative timestamp (issue2513)
> 
> Python's time.gmtime(lt) fails on Windows, producing a traceback with
> 
>  ValueError: (22, 'Invalid argument')
> 
> if lt < -43200.
> 
> We get a local time boundary value of -43200 if we take "the epoch"
> 
>   Thu Jan 01 00:00:00 1970 = time.gmtime(0)
> 
> from timezone 'UTC+0' into timezone 'UTC-12'. All other timezones will have
> larger local time values for that point in time.
> 
> Aborting with a traceback on 'hg log' for revisions with a timestamp value
> < -43200 is clearly not acceptable.
> 
> Returning "invalid timestamp" or similar as string representation is not an
> option either, since that may crash other tools which parse the output of
> 'hg log'.
> 
> Instead, we teach util.datestr() to return the epoch in timezone UTC+0 on
> *all platforms*, represented by the string
> 
>  Thu Jan 01 00:00:00 1970 +0000
> 
> if the timestamp's unix time value is negative.
> 
> (based on a patch originally proposed by Benjamin Pollack)
> 
> diff --git a/mercurial/util.py b/mercurial/util.py
> --- a/mercurial/util.py
> +++ b/mercurial/util.py
> @@ -1026,6 +1026,9 @@ def datestr(date=None, format='%a %b %d 
>     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:
>         sign = (tz > 0) and "-" or "+"
>         minutes = abs(tz) // 60

This makes sense to me, but I'm unclear why you dropped the part of the patch
preventing Mercurial from recording timestamps before the epoch in the first
place. That still seems like it ought to be part of this patch.

--Benjamin


More information about the Mercurial-devel mailing list