Patchwork dates: support 'today' and 'yesterday' in parsedate (issue3764)

login
register
mail settings
Submitter Paul Cavallaro
Date Feb. 1, 2013, 4:09 p.m.
Message ID <c5610452c19dab5b9ca1.1359734978@devrs153.prn1.facebook.com>
Download mbox | patch
Permalink /patch/786/
State Accepted
Commit ae60735e37d2866908a3e4b77b99aa08e523c08c
Headers show

Comments

Paul Cavallaro - Feb. 1, 2013, 4:09 p.m.
# HG changeset patch
# User Paul Cavallaro <ptc@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.
Greg Ward - Feb. 1, 2013, 4:31 p.m.
On 01 February 2013, Paul Cavallaro said:
> # HG changeset patch
> # User Paul Cavallaro <ptc@fb.com>
> # Date 1358963505 28800
> # Node ID c5610452c19dab5b9ca1e905611ecd06b1ef50e8
> # Parent  1f794204abbd7dd4bc329ae0c7c4fd7ce56b33af
> dates: support 'today' and 'yesterday' in parsedate (issue3764)

Mercurial is in feature freeze for the 2.5 release. Please resend in a
few days, after 2.5 is out.

       Greg
Bryan O'Sullivan - Feb. 1, 2013, 5:22 p.m.
On Fri, Feb 1, 2013 at 8:31 AM, Greg Ward <greg@gerg.ca> wrote:

> Mercurial is in feature freeze for the 2.5 release. Please resend in a
> few days, after 2.5 is out.
>

I'll just apply the patch in a few days.

Patch

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