changeset 18537:ae60735e37d2

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.
author Paul Cavallaro <ptc@fb.com>
date Wed, 23 Jan 2013 09:51:45 -0800
parents ae645d4f084c
children 94317c2d53b8
files mercurial/help/dates.txt mercurial/util.py tests/test-parse-date.t
diffstat 3 files changed, 34 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/help/dates.txt	Mon Jan 28 12:19:21 2013 -0800
+++ b/mercurial/help/dates.txt	Wed Jan 23 09:51:45 2013 -0800
@@ -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:
 
--- a/mercurial/util.py	Mon Jan 28 12:19:21 2013 -0800
+++ b/mercurial/util.py	Wed Jan 23 09:51:45 2013 -0800
@@ -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:
--- a/tests/test-parse-date.t	Mon Jan 28 12:19:21 2013 -0800
+++ b/tests/test-parse-date.t	Wed Jan 23 09:51:45 2013 -0800
@@ -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