diff options
author | Karel Zak | 2016-05-31 18:27:20 +0200 |
---|---|---|
committer | Karel Zak | 2016-05-31 18:27:20 +0200 |
commit | cd28d6a405c9cfcaf30fe248a4df84133dbfa17a (patch) | |
tree | 09aee2fba8104cc24a521a0769f70f93bc6f8772 | |
parent | cal: allow to specify month by name (diff) | |
download | kernel-qcow2-util-linux-cd28d6a405c9cfcaf30fe248a4df84133dbfa17a.tar.gz kernel-qcow2-util-linux-cd28d6a405c9cfcaf30fe248a4df84133dbfa17a.tar.xz kernel-qcow2-util-linux-cd28d6a405c9cfcaf30fe248a4df84133dbfa17a.zip |
cal: support timestamps
For example
$ cal '2 weeks ago'
Signed-off-by: Karel Zak <kzak@redhat.com>
-rw-r--r-- | misc-utils/cal.1 | 17 | ||||
-rw-r--r-- | misc-utils/cal.c | 12 |
2 files changed, 28 insertions, 1 deletions
diff --git a/misc-utils/cal.1 b/misc-utils/cal.1 index 683e67d19..11e97462c 100644 --- a/misc-utils/cal.1 +++ b/misc-utils/cal.1 @@ -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 diff --git a/misc-utils/cal.c b/misc-utils/cal.c index c48de69f5..13d8b44be 100644 --- a/misc-utils/cal.c +++ b/misc-utils/cal.c @@ -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) { |