[hackers] [sbase] Add -u option to date (POSIX and Plan 9, and useful) || Robert Ransom

From: <hg_AT_suckless.org>
Date: Fri, 27 Apr 2012 10:33:38 +0200 (CEST)

changeset: 112:0f342bad96dd
tag: tip
user: Robert Ransom <rransom.8774_AT_gmail.com>
date: Mon Apr 23 08:32:24 2012 -0700
files: date.1 date.c
description:
Add -u option to date (POSIX and Plan 9, and useful)


diff -r 49376e8fcecf -r 0f342bad96dd date.1
--- a/date.1 Mon Apr 23 16:34:34 2012 +0200
+++ b/date.1 Mon Apr 23 08:32:24 2012 -0700
_AT_@ -5,6 +5,7 @@
 .B date
 .RB [ \-d
 .IR time ]
+.RB [ \-u ]
 .RI [+ format ]
 .SH DESCRIPTION
 .B date
_AT_@ -18,5 +19,8 @@
 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 -r 49376e8fcecf -r 0f342bad96dd date.c
--- a/date.c Mon Apr 23 16:34:34 2012 +0200
+++ b/date.c Mon Apr 23 08:32:24 2012 -0700
_AT_@ -11,21 +11,27 @@
         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);
Received on Fri Apr 27 2012 - 10:33:38 CEST

This archive was generated by hypermail 2.3.0 : Fri Apr 27 2012 - 10:36:06 CEST