sbase/date.c

50 lines
786 B
C
Raw Normal View History

2011-05-23 18:00:31 +00:00
/* See LICENSE file for copyright and license details. */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
2011-05-23 18:00:31 +00:00
#include "util.h"
2013-06-14 18:20:47 +00:00
static void
usage(void)
{
eprintf("usage: %s [-u] [-d time] [+format]\n", argv0);
2013-06-14 18:20:47 +00:00
}
2011-05-23 18:00:31 +00:00
int
main(int argc, char *argv[])
{
struct tm *now;
struct tm *(*tztime)(const time_t *) = localtime;
2011-05-23 18:00:31 +00:00
time_t t;
char buf[BUFSIZ], *fmt = "%c", *tz = "local";
2011-05-23 18:00:31 +00:00
t = time(NULL);
2013-06-14 18:20:47 +00:00
ARGBEGIN {
case 'd':
t = estrtonum(EARGF(usage()), 0, LLONG_MAX);
2013-06-14 18:20:47 +00:00
break;
case 'u':
tztime = gmtime;
tz = "gm";
break;
default:
usage();
} ARGEND;
if (argc) {
if (argc != 1 || argv[0][0] != '+')
usage();
else
fmt = &argv[0][1];
}
if (!(now = tztime(&t)))
eprintf("%stime failed\n", tz);
2011-05-23 18:00:31 +00:00
strftime(buf, sizeof(buf), fmt, now);
2011-05-23 18:00:31 +00:00
puts(buf);
2013-06-14 18:20:47 +00:00
2014-10-02 22:46:04 +00:00
return 0;
2011-05-23 18:00:31 +00:00
}