ptime

Reformat timestamps
git clone git://git.sgregoratto.me/ptime
Log | Files | Refs

ptime.c (829B)


      1 #include "config.h"
      2 
      3 #if HAVE_ERR
      4 #include <err.h>
      5 #endif
      6 #include <stdio.h>
      7 #include <time.h>
      8 
      9 int
     10 main(int argc, char **argv)
     11 {
     12 	char buf[101], *end, *fmt;
     13 	struct tm tm;
     14 
     15 #if HAVE_PLEDGE
     16 	if (pledge("stdio", NULL) == -1)
     17 		err(1, "pledge");
     18 #endif
     19 
     20 	if (argc < 3) {
     21 		goto usage;
     22 	} else if (argc > 4) {
     23 		warnx("%s: Too many arguments", argv[4]);
     24 		goto usage;
     25 	}
     26 
     27 	end = strptime(argv[2], argv[1], &tm);
     28 	if (end == NULL)
     29 		errx(1, "invalid format string");
     30 	else if (*end != '\0')
     31 		errx(1, "timestamp does not match format");
     32 
     33 	fmt = argv[3] ? argv[3] : "%s";
     34 
     35 	if (strftime(buf, sizeof(buf), fmt, &tm) == 0)
     36 		errx(1, "could not convert time to format \"%s\"", fmt);
     37 
     38 	if (puts(buf) == EOF)
     39 		err(1, "puts");
     40 
     41 	return 0;
     42 
     43 usage:
     44 	fprintf(stderr, "usage: %s format timestamp [out-format]\n", getprogname());
     45 
     46 	return 1;
     47 }