/* See https://www.netmeister.org/blog/epoch.html for context. */
#include
#include
#include
#include
#include
int main() {
struct tm t;
time_t epoch = 1483228798;
int s = 58;
char buf[BUFSIZ];
printf("From epoch to time, via gmtime(3) to strftime(3):\n");
for (int i = 0; i<3; i++) {
struct tm *gmt;
if ((gmt = gmtime(&epoch)) == NULL) {
err(EXIT_FAILURE, "gmtime");
/* NOTREACHED */
}
(void)strftime(buf, BUFSIZ, "%Y-%m-%dT%H:%M:%S", gmt);
(void)printf("%ld %s\n", epoch, buf);
epoch++;
}
(void)printf("\nFrom time to epoch, via strptime(3) to mktime(3):\n");
for (int i = 0; i<3; i++) {
bzero(buf, BUFSIZ);
snprintf(buf, BUFSIZ, "2016-12-31T23:59:%02d", s);
s++;
strptime(buf, "%Y-%m-%dT%H:%M:%S", &t);
printf("%s is %ld\n", buf, mktime(&t));
}
bzero(buf, BUFSIZ);
strptime("2017-01-01T00:00:00", "%Y-%m-%dT%H:%M:%S", &t);
printf("2017-01-01T00:00:00 is %ld\n", mktime(&t));
return 0;
}