morpheus-base

morpheus base system
git clone git://git.2f30.org/morpheus-base
Log | Files | Refs

date.c (779B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 #include <time.h>
      5 #include <unistd.h>
      6 
      7 #include "util.h"
      8 
      9 static void
     10 usage(void)
     11 {
     12 	eprintf("usage: %s [-u] [-d format] [+FORMAT]\n", argv0);
     13 }
     14 
     15 int
     16 main(int argc, char *argv[])
     17 {
     18 	char buf[BUFSIZ];
     19 	char *fmt = "%c";
     20 	struct tm *now = NULL;
     21 	struct tm *(*tztime)(const time_t *) = localtime;
     22 	const char *tz = "local";
     23 	time_t t;
     24 
     25 	t = time(NULL);
     26 	ARGBEGIN {
     27 	case 'd':
     28 		t = estrtol(EARGF(usage()), 0);
     29 		break;
     30 	case 'u':
     31 		tztime = gmtime;
     32 		tz = "gm";
     33 		break;
     34 	default:
     35 		usage();
     36 	} ARGEND;
     37 	if (argc > 0 && argv[0][0] == '+')
     38 		fmt = &argv[0][1];
     39 	if (!(now = tztime(&t)))
     40 		eprintf("%stime failed\n", tz);
     41 
     42 	strftime(buf, sizeof buf, fmt, now);
     43 	puts(buf);
     44 
     45 	return 0;
     46 }