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.

Regards,
-Markus

Reply via email to