From c018f86fc752e1f1743985852993bfa755c637bd Mon Sep 17 00:00:00 2001 From: Robert Ransom Date: Mon, 23 Apr 2012 08:32:24 -0700 Subject: [PATCH] Add -u option to date (POSIX and Plan 9, and useful) --- date.1 | 4 ++++ date.c | 12 +++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/date.1 b/date.1 index 2e174bd..176509f 100644 --- a/date.1 +++ b/date.1 @@ -5,6 +5,7 @@ date \- print date and time .B date .RB [ \-d .IR time ] +.RB [ \-u ] .RI [+ format ] .SH DESCRIPTION .B date @@ -18,5 +19,8 @@ is given it is used to format the date as per prints .I time instead of the system time, given as the number of seconds since the Unix epoch. +.TP +.B \-u +prints UTC time instead of local time. .SH SEE ALSO .IR strftime (3) diff --git a/date.c b/date.c index 04b2651..7cab24d 100644 --- a/date.c +++ b/date.c @@ -11,21 +11,27 @@ main(int argc, char *argv[]) char buf[BUFSIZ], c; char *fmt = "%c"; struct tm *now = NULL; + struct tm *(*tztime)(const time_t *) = localtime; + const char *tz = "local"; time_t t; t = time(NULL); - while((c = getopt(argc, argv, "d:")) != -1) + while((c = getopt(argc, argv, "d:u")) != -1) switch(c) { case 'd': t = estrtol(optarg, 0); break; + case 'u': + tztime = gmtime; + tz = "gm"; + break; default: exit(EXIT_FAILURE); } if(optind < argc && argv[optind][0] == '+') fmt = &argv[optind][1]; - if(!(now = localtime(&t))) - eprintf("localtime failed\n"); + if(!(now = tztime(&t))) + eprintf("%stime failed\n", tz); strftime(buf, sizeof buf, fmt, now); puts(buf);