Fix up representation of dates in hgweb.
authorBryan O'Sullivan <bos@serpentine.com>
Thu, 22 Sep 2005 22:46:50 -0700
changeset 1320 5f277e73778f
parent 1319 5a15df632e6a
child 1321 b47f96a178a3
Fix up representation of dates in hgweb. Date display is now uniform both on the command line and via the web interface.
mercurial/commands.py
mercurial/hgweb.py
mercurial/util.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -30,20 +30,6 @@ def relpath(repo, args):
         return [util.normpath(os.path.join(cwd, x)) for x in args]
     return args
 
-def datestr(change=None):
-    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
-    return time.asctime(time.gmtime(float(t) - tz)) + " %+05d" % (int(tz)/-36)
-
 def matchpats(repo, cwd, pats=[], opts={}, head=''):
     return util.matcher(repo.root, cwd, pats or ['.'], opts.get('include'),
                         opts.get('exclude'), head)
@@ -278,11 +264,11 @@ def dodiff(fp, ui, repo, node1, node2, f
     if node2:
         change = repo.changelog.read(node2)
         mmap2 = repo.manifest.read(change[0])
-        date2 = datestr(change)
+        date2 = util.datestr(change)
         def read(f):
             return repo.file(f).read(mmap2[f])
     else:
-        date2 = datestr()
+        date2 = util.datestr()
         if not node1:
             node1 = repo.dirstate.parents()[0]
         def read(f):
@@ -296,7 +282,7 @@ def dodiff(fp, ui, repo, node1, node2, f
 
     change = repo.changelog.read(node1)
     mmap = repo.manifest.read(change[0])
-    date1 = datestr(change)
+    date1 = util.datestr(change)
 
     for f in c:
         to = None
@@ -333,7 +319,7 @@ def show_changeset(ui, repo, rev=0, chan
         return
 
     changes = log.read(changenode)
-    date = datestr(changes)
+    date = util.datestr(changes)
 
     parents = [(log.rev(p), ui.verbose and hex(p) or short(p))
                for p in log.parents(changenode)
--- a/mercurial/hgweb.py
+++ b/mercurial/hgweb.py
@@ -9,7 +9,7 @@
 import os, cgi, sys
 from demandload import demandload
 demandload(globals(), "mdiff time re socket zlib errno ui hg ConfigParser")
-demandload(globals(), "zipfile tempfile StringIO tarfile BaseHTTPServer")
+demandload(globals(), "zipfile tempfile StringIO tarfile BaseHTTPServer util")
 from node import *
 
 def templatepath():
@@ -18,7 +18,7 @@ def templatepath():
         if os.path.isdir(p):
             return p
 
-def age(t):
+def age(x):
     def plural(t, c):
         if c == 1:
             return t
@@ -27,7 +27,8 @@ def age(t):
         return "%d %s" % (c, plural(t, c))
 
     now = time.time()
-    delta = max(1, int(now - t))
+    then = int(x[2].split(' ')[0])
+    delta = max(1, int(now - then))
 
     scales = [["second", 1],
               ["minute", 60],
@@ -151,23 +152,18 @@ class templater:
                 yield tmpl
                 return
 
-def rfc822date(x):
-    return time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime(x))
-
 common_filters = {
     "escape": cgi.escape,
     "age": age,
-    "date": (lambda x: time.asctime(time.gmtime(x))),
+    "date": util.datestr,
     "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": rfc822date,
+    "rfc822date": lambda x: util.datestr(x, "%a, %d %b %Y %H:%M:%S"),
     }
 
-
-
 class hgweb:
     def __init__(self, repo, name=None):
         if type(repo) == type(""):
@@ -189,7 +185,7 @@ class hgweb:
             self.allowpull = self.repo.ui.configbool("web", "allowpull", True)
 
     def date(self, cs):
-        return time.asctime(time.gmtime(float(cs[2].split(' ')[0])))
+        return util.datestr(cs)
 
     def listfiles(self, files, mf):
         for f in files[:self.maxfiles]:
@@ -307,7 +303,6 @@ class hgweb:
                 n = cl.node(i)
                 changes = cl.read(n)
                 hn = hex(n)
-                t = float(changes[2].split(' ')[0])
 
                 l.insert(0, {"parity": parity,
                              "author": changes[1],
@@ -316,7 +311,7 @@ class hgweb:
                              "changelogtag": self.showtag("changelogtag",n),
                              "manifest": hex(changes[0]),
                              "desc": changes[4],
-                             "date": t,
+                             "date": changes,
                              "files": self.listfilediffs(changes[3], n),
                              "rev": i,
                              "node": hn})
@@ -368,7 +363,6 @@ class hgweb:
 
                 count += 1
                 hn = hex(n)
-                t = float(changes[2].split(' ')[0])
 
                 yield self.t('searchentry',
                              parity=count & 1,
@@ -378,7 +372,7 @@ class hgweb:
                              changelogtag=self.showtag("changelogtag",n),
                              manifest=hex(changes[0]),
                              desc=changes[4],
-                             date=t,
+                             date=changes,
                              files=self.listfilediffs(changes[3], n),
                              rev=i,
                              node=hn)
@@ -399,7 +393,6 @@ class hgweb:
         cl = self.repo.changelog
         changes = cl.read(n)
         p1 = cl.parents(n)[0]
-        t = float(changes[2].split(' ')[0])
 
         files = []
         mf = self.repo.manifest.read(changes[0])
@@ -425,7 +418,7 @@ class hgweb:
                      manifest=hex(changes[0]),
                      author=changes[1],
                      desc=changes[4],
-                     date=t,
+                     date=changes,
                      files=files,
                      archives=archivelist())
 
@@ -443,7 +436,6 @@ class hgweb:
                 lr = fl.linkrev(n)
                 cn = cl.node(lr)
                 cs = cl.read(cl.node(lr))
-                t = float(cs[2].split(' ')[0])
 
                 l.insert(0, {"parity": parity,
                              "filenode": hex(n),
@@ -451,7 +443,7 @@ class hgweb:
                              "file": f,
                              "node": hex(cn),
                              "author": cs[1],
-                             "date": t,
+                             "date": cs,
                              "parent": self.parents("filelogparent",
                                                     fl.parents(n),
                                                     fl.rev, file=f),
@@ -471,7 +463,6 @@ class hgweb:
         cl = self.repo.changelog
         cn = cl.node(changerev)
         cs = cl.read(cn)
-        t = float(cs[2].split(' ')[0])
         mfn = cs[0]
 
         def lines():
@@ -489,7 +480,7 @@ class hgweb:
                      node=hex(cn),
                      manifest=hex(mfn),
                      author=cs[1],
-                     date=t,
+                     date=cs,
                      parent=self.parents("filerevparent",
                                          fl.parents(n), fl.rev, file=f),
                      permissions=self.repo.manifest.readflags(mfn)[f])
@@ -504,7 +495,6 @@ class hgweb:
         cl = self.repo.changelog
         cn = cl.node(changerev)
         cs = cl.read(cn)
-        t = float(cs[2].split(' ')[0])
         mfn = cs[0]
 
         def annotate(**map):
@@ -542,7 +532,7 @@ class hgweb:
                      node=hex(cn),
                      manifest=hex(mfn),
                      author=cs[1],
-                     date=t,
+                     date=cs,
                      parent=self.parents("fileannotateparent",
                                          fl.parents(n), fl.rev, file=f),
                      permissions=self.repo.manifest.readflags(mfn)[f])
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -12,7 +12,7 @@ platform-specific details from the core.
 
 import os, errno
 from demandload import *
-demandload(globals(), "re cStringIO shutil popen2 tempfile threading")
+demandload(globals(), "re cStringIO shutil popen2 tempfile threading time")
 
 def pipefilter(s, cmd):
     '''filter string S through command CMD, returning its output'''
@@ -543,3 +543,23 @@ def filechunkiter(f, size = 65536):
     while len(s) >= 0:
         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
+    return ("%s %+03d%02d" %
+            (time.strftime(format, time.gmtime(float(t) - tz)),
+             -tz / 3600,
+             ((-tz % 3600) / 60)))