]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
cal: support timestamps
authorKarel Zak <kzak@redhat.com>
Tue, 31 May 2016 16:27:20 +0000 (18:27 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 31 May 2016 16:27:20 +0000 (18:27 +0200)
For example

$ cal '2 weeks ago'

Signed-off-by: Karel Zak <kzak@redhat.com>
misc-utils/cal.1
misc-utils/cal.c

index 683e67d19d08a3bcf1fabd57713e82d1fd6637f1..11e97462ce6ff692ee385ed3367240d87bd2016b 100644 (file)
@@ -41,6 +41,10 @@ cal \- display a calendar
 .B cal
 [options]
 .RI [[[ day ] " month" ] " year" ]
+.br
+.B cal
+[options]
+.RI <timestamp>
 .SH DESCRIPTION
 .B cal
 displays a simple calendar.  If no arguments are specified, the current
@@ -48,6 +52,19 @@ month is displayed.
 .sp
 The \fImonth\fR may be specified as a number (1-12) or as a month name according
 to the current locales.
+.sp
+.B cal
+accepts special placeholders when parsing \fItimestamp\fR, "now" may be used to
+refer to the current time, "today", "yesterday", "tomorrow" refer to
+of the current day, the day before or the next day, respectively.
+.sp
+The relative date specifications are also accepted, in this case "+" is
+evaluated to the current time plus the specified time span. Correspondingly, a
+time span that is prefixed with "-" is evaluated to the current time minus the
+specified time span, for example '+2days'. Instead of prefixing the time span
+with "+" or "-", it may also be suffixed with a space and the word "left" or
+"ago" (for example '1 week ago').
+
 .SH OPTIONS
 .TP
 \fB\-1\fR, \fB\-\-one\fR
index c48de69f525a398522e6e4aad45ca5dc689814d3..13d8b44be16416f67a20356cfbeaa5db4e7926d6 100644 (file)
@@ -74,6 +74,7 @@
 #include "mbsalign.h"
 #include "strutils.h"
 #include "optutils.h"
+#include "timeutils.h"
 
 static int has_term = 0;
 static const char *Senter = "", *Sexit = "";   /* enter and exit standout mode */
@@ -402,7 +403,16 @@ int main(int argc, char **argv)
        } else
                ctl.week_width = ctl.day_width * DAYS_IN_WEEK;
 
-       time(&now);
+       if (argc == 1 && !isdigit_string(*argv)) {
+               usec_t x;
+               if (parse_timestamp(*argv, &x) == 0)
+                       now = (time_t) (x / 1000000);
+               else
+                       errx(EXIT_FAILURE, _("failed to parse timestamp"));
+               argc = 0;
+       } else
+               time(&now);
+
        local_time = localtime(&now);
 
        switch(argc) {