sbase/date.c

46 lines
776 B
C
Raw Normal View History

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