coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Feature request: date: varity support


From: Pádraig Brady
Subject: Re: Feature request: date: varity support
Date: Wed, 30 Oct 2013 12:06:00 +0000
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130110 Thunderbird/17.0.2

On 10/24/2013 03:00 AM, Markus Mayer wrote:
> In a recent project, I made use of the "date -v ..." capability of FreBSD's 
> date(1) command, which came in very handy to handle "yesterday's log files". 
> I subsequently discovered "date -v" seems to be a FreeBSD-only feature and 
> that GNU date (and therefore Linux) does not support it.
> 
> Considering how useful it seemed to me, I decided to try and port the 
> functionality to GNU date -- changing the original FreeBSD code as little as 
> possible.
> 
> This turned out to be far less of an issue than I anticipated. vary.c needed 
> only a few small tweaks to build on Linux and within the coreutils framework. 
> Hooking everything into date.c wasn't much harder. The result can be found 
> here: https://github.com/mmayer/coreutils/tree/feature/vary
> 
>    $ uname -sr
>    Linux 3.2.0-54-generic
> 
>    $ ./src/date --version | head -1
>    date (GNU coreutils) 8.21.131-cba81e-dirty
> 
>    # Now
>    $ ./src/date
>    Wed Oct 23 18:18:59 PDT 2013
> 
>    # Yesterday
>    $ ./src/date -v-1d
>    Tue Oct 22 18:19:04 PDT 2013
> 
>    # Last day in February 2013
>    $ ./src/date -v1d -v3m -v13y -v-1d
>    Thu Feb 28 18:19:09 PST 2013
> 
>    # Last day in February 2012
>    $ ./src/date -v1d -v3m -v12y -v-1d
>    Wed Feb 29 18:19:11 PST 2012
> 
>    # Last Friday of the current month
>    $ ./src/date -v1d -v+1m -v-1d -v-fri
>    Fri Oct 25 18:19:14 PDT 2013
> 
> Unlike FreeBSD's date command, "varity" arguments also work with date files 
> (batch_convert()):
> 
>    $ cat dates.txt
>    2004-02-29 16:21:42
>    2008-05-27 12:01:12
> 
>    # Regular batch processing
>    $ ./src/date -f dates.txt
>    Sun Feb 29 16:21:42 PST 2004
>    Tue May 27 12:01:12 PDT 2008
> 
>    # Subtract a day
>    $ ./src/date -f dates.txt  -v-1d
>    Sat Feb 28 16:21:42 PST 2004
>    Mon May 26 12:01:12 PDT 2008
> 
>    # Add a year (Feb 29 becomes Mar 1)
>    $ ./src/date -f dates.txt  -v+1y
>    Tue Mar  1 16:21:42 PST 2005
>    Wed May 27 12:01:12 PDT 2009
> 
> Please let me know if this functionality is something you would consider 
> accepting into mainline. If so, I'll re-organize the patches to fit best 
> practices for coreutils.

Thanks for taking the time to do the patch.
However we'd need copyright assignment of the code for
it to be acceptable to coreutils.

More importantly this functionality is already in date,
albeit in a slightly different form.

   # Now
   $ date
   Wed Oct 30 11:17:18 GMT 2013

   # Yesterday
   $ date -d '-1day'
   Tue Oct 29 11:18:06 GMT 2013

   # Last day in February 2013
   $ date -d '2013-03-01 -1day'
   Thu Feb 28 00:00:00 GMT 2013

   # Last day in February 2012
   $ date -d '2012-03-01 -1day'
   Wed Feb 29 00:00:00 GMT 2012

   # Last Friday of the current month
   # Interestingly GNU date seems to ignore relative DoW items?
   $ date -d "2013-11-02 last friday"
   Sat Nov  2 00:00:00 GMT 2013
   # So getting this is a bit contrived. Am I missing something?
   $ ldom=$(date -d "$(date +%Y-%m-01) +1month -1day" +%Y-%m-%d)
   $ date -d "$ldom -$(date -d "$ldom +2 days" +%u) days"
   Fri Oct 25 00:00:00 IST 2013

While this functionality is useful,
and compatibility with more platforms is desirable,
perhaps the best approach would be to favor the
most popular format in use, which I'm thinking
would be the GNU date syntax currently?
I.E. patch FreeBSD's date rather than GNU's?

Given the complexity of date input formats already,
I think we'd make things worse by adding more variations to the mix?
http://www.gnu.org/software/coreutils/manual/html_node/Date-input-formats.html

thanks,
Pádraig.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]