cal: support alone month name parameter

For example 'cal August' to print August for the current year.

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2016-10-07 16:02:36 +02:00
parent e5927d5411
commit 535fd6d2b6
2 changed files with 16 additions and 7 deletions

View File

@ -44,7 +44,7 @@ cal \- display a calendar
.br
.B cal
[options]
.RI <timestamp>
.RI <timestamp|monthname>
.SH DESCRIPTION
.B cal
displays a simple calendar. If no arguments are specified, the current
@ -103,10 +103,13 @@ Specifies the \fIyear\fR to be displayed; note the year must be fully specified:
.B "cal 89"
will not display a calendar for 1989.
.TP
\fBSingle string parameter (e.g. 'cal tomorrow')\fR
Specifies \fItimestamp\fR. The special placeholders are accepted when parsing timestamp, "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.
\fBSingle string parameter (e.g. 'cal tomorrow' or 'cal August')\fR
Specifies \fItimestamp\fR or a \fImonth name\fR according to the current
locales.
.sp
The special placeholders are accepted when parsing timestamp, "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

View File

@ -410,10 +410,14 @@ int main(int argc, char **argv)
if (argc == 1 && !isdigit_string(*argv)) {
usec_t x;
/* cal <timestamp> */
if (parse_timestamp(*argv, &x) == 0)
now = (time_t) (x / 1000000);
/* cal <monthname> */
else if ((ctl.req.month = monthname_to_number(&ctl, *argv)) > 0)
time(&now); /* this year */
else
errx(EXIT_FAILURE, _("failed to parse timestamp"));
errx(EXIT_FAILURE, _("failed to parse timestamp or unknown month name: %s"), *argv);
argc = 0;
} else
time(&now);
@ -460,7 +464,8 @@ int main(int argc, char **argv)
case 0:
ctl.req.day = local_time->tm_yday + 1;
ctl.req.year = local_time->tm_year + 1900;
ctl.req.month = local_time->tm_mon + 1;
if (!ctl.req.month)
ctl.req.month = local_time->tm_mon + 1;
break;
default:
usage(stderr);
@ -969,6 +974,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE * out)
{
fputs(USAGE_HEADER, out);
fprintf(out, _(" %s [options] [[[day] month] year]\n"), program_invocation_short_name);
fprintf(out, _(" %s [options] <timestamp|monthname>\n"), program_invocation_short_name);
fputs(USAGE_SEPARATOR, out);
fputs(_("Display a calendar, or some part of it.\n"), out);