Allow the use of human readable dates (issue 251)
authorJose M. Prieto <jmprieto@gmx.net>
Fri, 30 Jun 2006 18:47:35 +0200
changeset 2522 85f796baab10
parent 2521 9cceb439048b
child 2523 4ab59a3acd16
Allow the use of human readable dates (issue 251)
mercurial/changelog.py
mercurial/util.py
tests/test-parse-date
tests/test-parse-date.out
--- a/mercurial/changelog.py	Fri Jun 30 16:16:35 2006 +0200
+++ b/mercurial/changelog.py	Fri Jun 30 18:47:35 2006 +0200
@@ -43,17 +43,15 @@
             # time zone offset. values must fit in signed 32 bits for
             # current 32-bit linux runtimes. timezones go from UTC-12
             # to UTC+14
-            try:
-                when, offset = map(int, date.split(' '))
-            except ValueError:
-                raise ValueError(_('invalid date: %r') % date)
+            when, offset = util.parsedate(date)
             if abs(when) > 0x7fffffff:
                 raise ValueError(_('date exceeds 32 bits: %d') % when)
             if offset < -50400 or offset > 43200:
                 raise ValueError(_('impossible time zone offset: %d') % offset)
+            parseddate = "%d %d" % (when, offset)
         else:
-            date = "%d %d" % util.makedate()
+            parseddate = "%d %d" % util.makedate()
         list.sort()
-        l = [hex(manifest), user, date] + list + ["", desc]
+        l = [hex(manifest), user, parseddate] + list + ["", desc]
         text = "\n".join(l)
         return self.addrevision(text, transaction, self.count(), p1, p2)
--- a/mercurial/util.py	Fri Jun 30 16:16:35 2006 +0200
+++ b/mercurial/util.py	Fri Jun 30 18:47:35 2006 +0200
@@ -859,6 +859,37 @@
         s += " %+03d%02d" % (-tz / 3600, ((-tz % 3600) / 60))
     return s
 
+def strdate(string, format='%a %b %d %H:%M:%S %Y'):
+    """parse a localized time string and return a (unixtime, offset) tuple.
+    if the string cannot be parsed, ValueError is raised."""
+    def hastimezone(string):
+        return (string[-4:].isdigit() and
+               (string[-5] == '+' or string[-5] == '-') and
+               string[-6].isspace())
+
+    if hastimezone(string):
+        date, tz = string.rsplit(None, 1)
+        tz = int(tz)
+        offset = - 3600 * (tz / 100) - 60 * (tz % 100)
+    else:
+        date, offset = string, 0
+    when = int(time.mktime(time.strptime(date, format))) + offset
+    return when, offset
+
+def parsedate(string, formats=('%Y-%m-%d %H:%M:%S', '%Y-%m-%d %H:%M')):
+    """parse a localized time string and return a (unixtime, offset) tuple.
+    The date may be a "unixtime offset" string or in one of the specified
+    formats."""
+    try:
+        when, offset = map(int, string.split(' '))
+        return when, offset
+    except ValueError: pass
+    for format in formats:
+        try:
+            return strdate(string, format)
+        except ValueError: pass
+    raise ValueError(_('invalid date: %r') % string)
+
 def shortuser(user):
     """Return a short representation of a user name or email address."""
     f = user.find('@')
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-parse-date	Fri Jun 30 18:47:35 2006 +0200
@@ -0,0 +1,14 @@
+#!/bin/sh
+
+hg init
+echo "test-parse-date" > a
+hg add a
+hg ci -d "2006-02-01 13:00:30" -m "rev 0"
+echo "hi!" >> a
+hg ci -d "2006-02-01 13:00:30 -0500" -m "rev 1"
+hg tag -d "2006-04-15 13:30" "Hi"
+hg backout --merge -d "2006-04-15 13:30 +0200" -m "rev 3" 1
+hg ci -d "1150000000 14400" -m "rev 4 (merge)"
+echo "fail" >> a
+hg ci -d "should fail" -m "fail"
+hg log --template '{date|date}\n'
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-parse-date.out	Fri Jun 30 18:47:35 2006 +0200
@@ -0,0 +1,13 @@
+reverting a
+changeset 3:107ce1ee2b43 backs out changeset 1:25a1420a55f8
+merging with changeset 2:99a1acecff55
+1 files updated, 0 files merged, 0 files removed, 0 files unresolved
+(branch merge, don't forget to commit)
+abort: invalid date: 'should fail'
+transaction abort!
+rollback completed
+Sun Jun 11 00:26:40 2006 -0400
+Sat Apr 15 13:30:00 2006 +0200
+Sat Apr 15 13:30:00 2006 +0000
+Wed Feb 01 13:00:30 2006 -0500
+Wed Feb 01 13:00:30 2006 +0000