Clean up date and timezone handling.
authorBryan O'Sullivan <bos@serpentine.com>
Thu, 22 Sep 2005 23:19:47 -0700
changeset 1321 b47f96a178a3
parent 1320 5f277e73778f
child 1322 b3d44e9b3092
Clean up date and timezone handling. We used to pass changelog dates around as a "unixtime timezone" string containing a pair of encoded ints. Now, they get passed around as a (unixtime, timezone) tuple of numbers, which makes much more sense.
mercurial/changelog.py
mercurial/commands.py
mercurial/hgweb.py
mercurial/util.py
--- a/mercurial/changelog.py	Thu Sep 22 22:46:50 2005 -0700
+++ b/mercurial/changelog.py	Thu Sep 22 23:19:47 2005 -0700
@@ -5,8 +5,9 @@
 # This software may be used and distributed according to the terms
 # of the GNU General Public License, incorporated herein by reference.
 
-import os, time
 from revlog import *
+from demandload import demandload
+demandload(globals(), "os time util")
 
 class changelog(revlog):
     def __init__(self, opener):
@@ -20,11 +21,15 @@
         l = text[:last].splitlines()
         manifest = bin(l[0])
         user = l[1]
-        date = l[2]
-        if " " not in date:
-            date += " 0" # some tools used -d without a timezone
+        date = l[2].split(' ')
+        time = int(date.pop(0))
+        try:
+            # various tools did silly things with the time zone field.
+            timezone = int(date[0])
+        except:
+            timezone = 0
         files = l[3:]
-        return (manifest, user, date, files, desc)
+        return (manifest, user, (time, timezone), files, desc)
 
     def read(self, node):
         return self.extract(self.revision(node))
@@ -44,9 +49,7 @@
             if abs(offset) >= 43200:
                 raise ValueError('impossible time zone offset: %d' % offset)
         else:
-            if time.daylight: offset = time.altzone
-            else: offset = time.timezone
-            date = "%d %d" % (time.time(), offset)
+            date = "%d %d" % util.makedate()
         list.sort()
         l = [hex(manifest), user, date] + list + ["", desc]
         text = "\n".join(l)
--- a/mercurial/commands.py	Thu Sep 22 22:46:50 2005 -0700
+++ b/mercurial/commands.py	Thu Sep 22 23:19:47 2005 -0700
@@ -264,7 +264,7 @@
     if node2:
         change = repo.changelog.read(node2)
         mmap2 = repo.manifest.read(change[0])
-        date2 = util.datestr(change)
+        date2 = util.datestr(change[2])
         def read(f):
             return repo.file(f).read(mmap2[f])
     else:
@@ -282,7 +282,7 @@
 
     change = repo.changelog.read(node1)
     mmap = repo.manifest.read(change[0])
-    date1 = util.datestr(change)
+    date1 = util.datestr(change[2])
 
     for f in c:
         to = None
@@ -319,7 +319,7 @@
         return
 
     changes = log.read(changenode)
-    date = util.datestr(changes)
+    date = util.datestr(changes[2])
 
     parents = [(log.rev(p), ui.verbose and hex(p) or short(p))
                for p in log.parents(changenode)
--- a/mercurial/hgweb.py	Thu Sep 22 22:46:50 2005 -0700
+++ b/mercurial/hgweb.py	Thu Sep 22 23:19:47 2005 -0700
@@ -27,7 +27,7 @@
         return "%d %s" % (c, plural(t, c))
 
     now = time.time()
-    then = int(x[2].split(' ')[0])
+    then = x[2][0]
     delta = max(1, int(now - then))
 
     scales = [["second", 1],
@@ -155,13 +155,13 @@
 common_filters = {
     "escape": cgi.escape,
     "age": age,
-    "date": util.datestr,
+    "date": lambda x: util.datestr(x[2]),
     "addbreaks": nl2br,
     "obfuscate": obfuscate,
     "short": (lambda x: x[:12]),
     "firstline": (lambda x: x.splitlines(1)[0]),
     "permissions": (lambda x: x and "-rwxr-xr-x" or "-rw-r--r--"),
-    "rfc822date": lambda x: util.datestr(x, "%a, %d %b %Y %H:%M:%S"),
+    "rfc822date": lambda x: util.datestr(x[2], "%a, %d %b %Y %H:%M:%S"),
     }
 
 class hgweb:
@@ -185,7 +185,7 @@
             self.allowpull = self.repo.ui.configbool("web", "allowpull", True)
 
     def date(self, cs):
-        return util.datestr(cs)
+        return util.datestr(cs[2])
 
     def listfiles(self, files, mf):
         for f in files[:self.maxfiles]:
--- a/mercurial/util.py	Thu Sep 22 22:46:50 2005 -0700
+++ b/mercurial/util.py	Thu Sep 22 23:19:47 2005 -0700
@@ -544,21 +544,17 @@
         yield s
         s = f.read(size)
 
-def datestr(change=None, format='%c'):
-    """represent a change date as a localized time.
-    a change date is a 'unixtime offset' string, where unixtime is
-    seconds since the epoch, and offset is seconds away from UTC."""
-    if change is None:
-        t = time.time()
-        if time.daylight: tz = time.altzone
-        else: tz = time.timezone
-    else:
-        t, tz = change[2].split(' ')
-        try:
-            # a conversion tool was sticking non-integer offsets into repos
-            tz = int(tz)
-        except ValueError:
-            tz = 0
+def makedate():
+    t = time.time()
+    if time.daylight: tz = time.altzone
+    else: tz = time.timezone
+    return t, tz
+    
+def datestr(date=None, format='%c'):
+    """represent a (unixtime, offset) tuple as a localized time.
+    unixtime is seconds since the epoch, and offset is the time zone's
+    number of seconds away from UTC."""
+    t, tz = date or makedate()
     return ("%s %+03d%02d" %
             (time.strftime(format, time.gmtime(float(t) - tz)),
              -tz / 3600,