diff options
author | P. J. McDermott <pj@pehjota.net> | 2022-03-19 21:14:39 (EDT) |
---|---|---|
committer | P. J. McDermott <pj@pehjota.net> | 2022-03-19 21:14:39 (EDT) |
commit | cf9c057d273399f29eb0b86afa50294ad5d14d6d (patch) | |
tree | 1628fcea81ea10afddb68abeed208de34db1c520 | |
parent | ea2528f95f32099d76322b3327e350aeea7c249b (diff) | |
download | atsign-cf9c057d273399f29eb0b86afa50294ad5d14d6d.zip atsign-cf9c057d273399f29eb0b86afa50294ad5d14d6d.tar.gz atsign-cf9c057d273399f29eb0b86afa50294ad5d14d6d.tar.bz2 |
datetime: Add function to get seconds since Epoch
And use it in main().
-rw-r--r-- | src/datetime.c | 22 | ||||
-rw-r--r-- | src/datetime.h | 3 | ||||
-rw-r--r-- | src/main.c | 15 |
3 files changed, 26 insertions, 14 deletions
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_ */ @@ -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; } |