From cf9c057d273399f29eb0b86afa50294ad5d14d6d Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Sat, 19 Mar 2022 21:14:39 -0400 Subject: datetime: Add function to get seconds since Epoch And use it in main(). --- diff --git a/src/datetime.c b/src/datetime.c index cbdf6b4..994d3fe 100644 --- a/src/datetime.c +++ b/src/datetime.c @@ -102,6 +102,18 @@ static const char *DATETIME_MISC_FMTS_[] = { " %a %b %d %H:%M:%S %Y ", }; +static struct tm DATETIME_EPOCH_ = { + .tm_sec = 0, + .tm_min = 0, + .tm_hour = 0, + .tm_mday = 1, + .tm_mon = 0, + .tm_year = 70, + .tm_wday = 4, + .tm_yday = 0, + .tm_isdst = -1, +}; + static void _datetime_reset_tm(struct tm *tm) { @@ -378,3 +390,13 @@ datetime_strftime_misc(const struct tm *tm, char **out, size_t *buf_sz, return _datetime_strftime(DATETIME_MISC_FMTS_, tm, out, buf_sz, buf, i); } + +double +datetime_diff_epoch(time_t t) +{ + time_t epoch; + + epoch = timegm(&DATETIME_EPOCH_); + + return difftime(t, epoch); +} diff --git a/src/datetime.h b/src/datetime.h index 554748b..e225b52 100644 --- a/src/datetime.h +++ b/src/datetime.h @@ -37,4 +37,7 @@ int datetime_strftime_misc(const struct tm *tm, char **out, size_t *out_sz, char **buf, size_t *i); +double +datetime_diff_epoch(time_t t); + #endif /* DATETIME_H_ */ diff --git a/src/main.c b/src/main.c index 5e1f799..474f662 100644 --- a/src/main.c +++ b/src/main.c @@ -57,18 +57,6 @@ static const struct option LONGOPTS_[] = { }; #endif -static struct tm EPOCH_ = { - .tm_sec = 0, - .tm_min = 0, - .tm_hour = 0, - .tm_mday = 1, - .tm_mon = 0, - .tm_year = 70, - .tm_wday = 4, - .tm_yday = 0, - .tm_isdst = -1, -}; - static void _print_usage(FILE *stream, const char *program_name) { @@ -287,8 +275,7 @@ main(int argc, char * const argv[]) free(buf); if (dbg == true) { - now = timegm(&EPOCH_); - dif = difftime(arg, now); + dif = datetime_diff_epoch(arg); printf("%" PRId64 "\n", (int64_t) dif); return EXIT_SUCCESS; } -- cgit v0.9.1