From fdcc890c024ec0079f483b24acf972a0976304fb Mon Sep 17 00:00:00 2001 From: Arran Cudbard-Bell Date: Wed, 25 Aug 2021 15:56:10 -0500 Subject: [PATCH] More fr_time_from_utc fixes --- src/lib/util/misc.c | 14 ++++---------- src/lib/util/time.c | 4 ++-- src/lib/util/time.h | 2 +- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/lib/util/misc.c b/src/lib/util/misc.c index fc0ebb168a0..f075372015d 100644 --- a/src/lib/util/misc.c +++ b/src/lib/util/misc.c @@ -711,16 +711,15 @@ int fr_unix_time_from_str(fr_unix_time_t *date, char const *date_str, fr_time_re if (*tail == '-') tz *= -1; done: - t = fr_time_from_utc(tm); + *date = fr_unix_time_from_utc(tm); /* * Add in the time zone offset, which the posix * functions are too stupid to do. */ - t += tz; - - *date = fr_unix_time_from_timeval(&(struct timeval) { .tv_sec = t }); + *date += fr_unix_time_from_sec(tz); *date += subseconds; + return 0; } @@ -896,12 +895,7 @@ int fr_unix_time_from_str(fr_unix_time_t *date, char const *date_str, fr_time_re tm->tm_min = atoi(f[1]); } - t = fr_time_from_utc(tm); - - /* - * Get the UTC time, and manually add in the offset from GMT. - */ - *date = fr_unix_time_from_timeval(&(struct timeval) { .tv_sec = t }); + *date = fr_unix_time_from_utc(tm); /* * Add in the time zone offset, which the posix diff --git a/src/lib/util/time.c b/src/lib/util/time.c index 47e158f1963..6223262b123 100644 --- a/src/lib/util/time.c +++ b/src/lib/util/time.c @@ -539,7 +539,7 @@ void fr_time_elapsed_fprint(FILE *fp, fr_time_elapsed_t const *elapsed, char con /* * Based on https://blog.reverberate.org/2020/05/12/optimizing-date-algorithms.html */ -fr_unix_time_t fr_time_from_utc(struct tm *tm) +fr_unix_time_t fr_unix_time_from_utc(struct tm *tm) { static const uint16_t month_yday[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; @@ -553,5 +553,5 @@ fr_unix_time_t fr_time_from_utc(struct tm *tm) * 2472692 adjusts the days for Unix epoch. It is calculated as * (365.2425 * (4800 + 1970)) */ - return (days - 2472692) * 86400 + (tm->tm_hour * 3600) + (tm->tm_min * 60) + tm->tm_sec; + return fr_unix_time_from_sec((days - 2472692) * 86400 + (tm->tm_hour * 3600) + (tm->tm_min * 60) + tm->tm_sec); } diff --git a/src/lib/util/time.h b/src/lib/util/time.h index 265db12452d..2fed708d680 100644 --- a/src/lib/util/time.h +++ b/src/lib/util/time.h @@ -371,7 +371,7 @@ size_t fr_time_strftime_utc(fr_sbuff_t *out, fr_time_t time, char const *fmt) void fr_time_elapsed_update(fr_time_elapsed_t *elapsed, fr_time_t start, fr_time_t end) CC_HINT(nonnull); void fr_time_elapsed_fprint(FILE *fp, fr_time_elapsed_t const *elapsed, char const *prefix, int tabs) CC_HINT(nonnull(1,2)); -fr_unix_time_t fr_time_from_utc(struct tm *tm) CC_HINT(nonnull); +fr_unix_time_t fr_unix_time_from_utc(struct tm *tm) CC_HINT(nonnull); #ifdef __cplusplus } -- 2.47.3