dates: improve timezone handling
authorMatt Mackall <mpm@selenic.com>
Tue, 11 Mar 2008 17:42:41 -0500
changeset 6229 c3182eeb70ea
parent 6228 c0c4c7b1e8d3
child 6230 c7253d1a774e
dates: improve timezone handling datestr: - add format specifiers %1 and %2 for timezone hours and minutes - remove timezone and timezone format options - correctly find timezone hours and minutes for fractional and negative timezones - update users strdate: - correctly find timezone hours and minutes for fractional and negative timezones
hgext/notify.py
hgext/patchbomb.py
mercurial/templatefilters.py
mercurial/util.py
--- a/hgext/notify.py	Tue Mar 11 17:42:29 2008 -0500
+++ b/hgext/notify.py	Tue Mar 11 17:42:41 2008 -0500
@@ -210,9 +210,7 @@
             del msg['From']
             msg['From'] = sender
 
-        msg['Date'] = util.datestr(date=util.makedate(),
-                                   format="%a, %d %b %Y %H:%M:%S",
-                                   timezone=True)
+        msg['Date'] = util.datestr(format="%a, %d %b %Y %H:%M:%S %1%2")
         fix_subject()
         fix_sender()
 
--- a/hgext/patchbomb.py	Tue Mar 11 17:42:29 2008 -0500
+++ b/hgext/patchbomb.py	Tue Mar 11 17:42:41 2008 -0500
@@ -391,8 +391,7 @@
             m['In-Reply-To'] = parent
         else:
             parent = m['Message-Id']
-        m['Date'] = util.datestr(date=start_time,
-                                 format="%a, %d %b %Y %H:%M:%S", timezone=True)
+        m['Date'] = util.datestr(start_time, "%a, %d %b %Y %H:%M:%S %1%2")
 
         start_time = (start_time[0] + 1, start_time[1])
         m['From'] = sender
@@ -419,8 +418,7 @@
         elif opts.get('mbox'):
             ui.status('Writing ', m['Subject'], ' ...\n')
             fp = open(opts.get('mbox'), 'In-Reply-To' in m and 'ab+' or 'wb+')
-            date = util.datestr(date=start_time,
-                                format='%a %b %d %H:%M:%S %Y', timezone=False)
+            date = util.datestr(start_time, '%a %b %d %H:%M:%S %Y')
             fp.write('From %s %s\n' % (sender_addr, date))
             fp.write(m.as_string(0))
             fp.write('\n\n')
--- a/mercurial/templatefilters.py	Tue Mar 11 17:42:29 2008 -0500
+++ b/mercurial/templatefilters.py	Tue Mar 11 17:42:41 2008 -0500
@@ -69,14 +69,6 @@
     except IndexError:
         return ''
 
-def isodate(date):
-    '''turn a (timestamp, tzoff) tuple into an iso 8631 date and time.'''
-    return util.datestr(date, format='%Y-%m-%d %H:%M')
-
-def hgdate(date):
-    '''turn a (timestamp, tzoff) tuple into an hg cset timestamp.'''
-    return "%d %d" % date
-
 def nl2br(text):
     '''replace raw newlines with xhtml line breaks.'''
     return text.replace('\n', '<br/>\n')
@@ -142,13 +134,13 @@
     "fill76": lambda x: fill(x, width=76),
     "firstline": firstline,
     "tabindent": lambda x: indent(x, '\t'),
-    "hgdate": hgdate,
-    "isodate": isodate,
+    "hgdate": lambda x: "%d %d" % x,
+    "isodate": lambda x: util.datestr(x, '%Y-%m-%d %H:%M %1%2'),
     "obfuscate": obfuscate,
     "permissions": permissions,
     "person": person,
-    "rfc822date": lambda x: util.datestr(x, "%a, %d %b %Y %H:%M:%S"),
-    "rfc3339date": lambda x: util.datestr(x, "%Y-%m-%dT%H:%M:%S", True, "%+03d:%02d"),
+    "rfc822date": lambda x: util.datestr(x, "%a, %d %b %Y %H:%M:%S %1%2"),
+    "rfc3339date": lambda x: util.datestr(x, "%Y-%m-%dT%H:%M:%S%1:%2"),
     "short": lambda x: x[:12],
     "shortdate": util.shortdate,
     "stringify": templater.stringify,
--- a/mercurial/util.py	Tue Mar 11 17:42:29 2008 -0500
+++ b/mercurial/util.py	Tue Mar 11 17:42:41 2008 -0500
@@ -1524,20 +1524,23 @@
         tz = time.timezone
     return time.mktime(lt), tz
 
-def datestr(date=None, format='%a %b %d %H:%M:%S %Y', timezone=True, timezone_format=" %+03d%02d"):
+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
     number of seconds away from UTC. if timezone is false, do not
     append time zone to string."""
     t, tz = date or makedate()
+    if "%1" in format or "%2" in format:
+        sign = (tz > 0) and "-" or "+"
+        minutes = abs(tz) / 60
+        format = format.replace("%1", "%c%02d" % (sign, minutes / 60))
+        format = format.replace("%2", "%02d" % (minutes % 60))
     s = time.strftime(format, time.gmtime(float(t) - tz))
-    if timezone:
-        s += timezone_format % (int(-tz / 3600.0), ((-tz % 3600) / 60))
     return s
 
 def shortdate(date=None):
     """turn (timestamp, tzoff) tuple into iso 8631 date."""
-    return datestr(date, format='%Y-%m-%d', timezone=False)
+    return datestr(date, format='%Y-%m-%d')
 
 def strdate(string, format, defaults=[]):
     """parse a localized time string and return a (unixtime, offset) tuple.
@@ -1545,9 +1548,10 @@
     def timezone(string):
         tz = string.split()[-1]
         if tz[0] in "+-" and len(tz) == 5 and tz[1:].isdigit():
-            tz = int(tz)
-            offset = - 3600 * (tz / 100) - 60 * (tz % 100)
-            return offset
+            sign = (tz[0] == "+") and 1 or -1
+            hours = int(tz[1:3])
+            minutes = int(tz[3:5])
+            return -sign * (hours * 60 + minutes) * 60
         if tz == "GMT" or tz == "UTC":
             return 0
         return None
@@ -1601,7 +1605,7 @@
                 elif part[0] in "dm":
                     defaults[part] = "1"
                 else:
-                    defaults[part] = datestr(now, "%" + part[0], False)
+                    defaults[part] = datestr(now, "%" + part[0])
 
         for format in formats:
             try: