changeset 3808:d6529582942a

improve date parsing for numerous new date formats Add lots of new date formats Formats without year, month, or day default to current Strip leading and trailing whitespace
author Matt Mackall <mpm@selenic.com>
date Wed, 06 Dec 2006 13:13:31 -0600
parents e43b48f0f718
children 4d93b37b5963
files mercurial/util.py
diffstat 1 files changed, 36 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/util.py	Wed Dec 06 13:13:27 2006 -0600
+++ b/mercurial/util.py	Wed Dec 06 13:13:31 2006 -0600
@@ -72,8 +72,29 @@
         raise Abort("decoding near '%s': %s!\n" % (sub, inst))
 
 # used by parsedate
-defaultdateformats = ('%Y-%m-%d %H:%M:%S', '%Y-%m-%d %H:%M',
-                      '%a %b %d %H:%M:%S %Y')
+defaultdateformats = (
+    '%Y-%m-%d %H:%M:%S',
+    '%Y-%m-%d %I:%M:%S%p',
+    '%Y-%m-%d %H:%M',
+    '%Y-%m-%d %I:%M%p',
+    '%Y-%m-%d',
+    '%m-%d',
+    '%m/%d',
+    '%m/%d/%y',
+    '%m/%d/%Y',
+    '%a %b %d %H:%M:%S %Y',
+    '%a %b %d %I:%M:%S%p %Y',
+    '%b %d %H:%M:%S %Y',
+    '%b %d %I:%M:%S%p',
+    '%b %d %H:%M',
+    '%b %d %I:%M%p',
+    '%b %d %Y',
+    '%b %d',
+    '%H:%M:%S',
+    '%I:%M:%SP',
+    '%H:%M',
+    '%I:%M%p',
+)
 
 class SignalInterrupt(Exception):
     """Exception raised on SIGTERM and SIGHUP."""
@@ -1052,6 +1073,18 @@
         offset = - 3600 * (tz / 100) - 60 * (tz % 100)
     else:
         date, offset = string, None
+
+    # add missing elements
+    if '%y' not in format.lower():
+        date += "@" + datestr(makedate(), "%Y", False)
+        format += "@%Y"
+    if '%m' not in format and '%b' not in format:
+        date += "@" + datestr(makedate(), "%m", False)
+        format += "@%m"
+    if '%d' not in format:
+        date += "@" + datestr(makedate(), "%d", False)
+        format += "@%d"
+
     timetuple = time.strptime(date, format)
     localunixtime = int(calendar.timegm(timetuple))
     if offset is None:
@@ -1070,6 +1103,7 @@
         return 0, 0
     if not formats:
         formats = defaultdateformats
+    string = string.strip()
     try:
         when, offset = map(int, string.split(' '))
     except ValueError: