sbase/date.c

40 lines
769 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"
int
main(int argc, char *argv[])
{
2011-05-25 06:56:04 -04:00
char buf[BUFSIZ], c;
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);
while((c = getopt(argc, argv, "d:u")) != -1)
2011-05-23 20:13:34 -04:00
switch(c) {
case 'd':
2011-06-10 09:55:01 -04:00
t = estrtol(optarg, 0);
2011-05-23 20:13:34 -04:00
break;
case 'u':
tztime = gmtime;
tz = "gm";
break;
2011-05-23 20:13:34 -04:00
default:
exit(EXIT_FAILURE);
}
if(optind < argc && argv[optind][0] == '+')
fmt = &argv[optind][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);
return EXIT_SUCCESS;
}