[PATCH] dates: support 'today' and 'yesterday' in parsedate (issue3764)

Paul Cavallaro ptc at fb.com
Fri Feb 1 10:09:38 CST 2013


# HG changeset patch
# User Paul Cavallaro <ptc at fb.com>
# Date 1358963505 28800
# Node ID c5610452c19dab5b9ca1e905611ecd06b1ef50e8
# Parent  1f794204abbd7dd4bc329ae0c7c4fd7ce56b33af
dates: support 'today' and 'yesterday' in parsedate (issue3764)

Adding support to parsedate in util module to understand the more idiomatic
dates 'today' and 'yesterday'.

Added unified tests and docstring tests for added functionality.

diff --git a/mercurial/help/dates.txt b/mercurial/help/dates.txt
--- a/mercurial/help/dates.txt
+++ b/mercurial/help/dates.txt
@@ -18,6 +18,8 @@
 - ``12-6``
 - ``12/6``
 - ``12/6/6`` (Dec 6 2006)
+- ``today`` (midnight)
+- ``yesterday`` (midnight)
 
 Lastly, there is Mercurial's internal format:
 
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -1027,6 +1027,14 @@
 
     The date may be a "unixtime offset" string or in one of the specified
     formats. If the date already is a (unixtime, offset) tuple, it is returned.
+
+    >>> parsedate(' today ') == parsedate(\
+                                  datetime.date.today().strftime('%b %d'))
+    True
+    >>> parsedate( 'yesterday ') == parsedate((datetime.date.today() -\
+                                               datetime.timedelta(days=1)\
+                                              ).strftime('%b %d'))
+    True
     """
     if not date:
         return 0, 0
@@ -1035,6 +1043,13 @@
     if not formats:
         formats = defaultdateformats
     date = date.strip()
+
+    if date == _('today'):
+        date = datetime.date.today().strftime('%b %d')
+    elif date == _('yesterday'):
+        date = (datetime.date.today() -
+                datetime.timedelta(days=1)).strftime('%b %d')
+
     try:
         when, offset = map(int, date.split(' '))
     except ValueError:
diff --git a/tests/test-parse-date.t b/tests/test-parse-date.t
--- a/tests/test-parse-date.t
+++ b/tests/test-parse-date.t
@@ -234,3 +234,20 @@
   Sat Apr 15 13:30:00 2006 +0000
   Wed Feb 01 13:00:30 2006 -0500
   Wed Feb 01 13:00:30 2006 +0000
+
+Test issue 3764 (interpreting 'today' and 'yesterday')
+  $ echo "hello" >> a
+  >>> import datetime
+  >>> today = datetime.date.today().strftime("%b %d")
+  >>> yesterday = (datetime.date.today() - datetime.timedelta(days=1)).strftime("%b %d")
+  >>> dates = open('dates', 'w')
+  >>> dates.write(today + '\n')
+  >>> dates.write(yesterday)
+  >>> dates.close()
+  $ hg ci -d "`sed -n '1p' dates`" -m "today is a good day to code"
+  $ hg log -d today --template '{desc}\n'
+  today is a good day to code
+  $ echo "goodbye" >> a
+  $ hg ci -d "`sed -n '2p' dates`" -m "the time traveler's code"
+  $ hg log -d yesterday --template '{desc}\n'
+  the time traveler's code


More information about the Mercurial-devel mailing list