From: Alan T. DeKok Date: Wed, 7 Apr 2021 15:08:21 +0000 (-0400) Subject: scale unix time from a number, based on a hint X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=89533176f76218a3ebbe8295a3bbe50f14fde475;p=thirdparty%2Ffreeradius-server.git scale unix time from a number, based on a hint --- diff --git a/src/lib/util/misc.c b/src/lib/util/misc.c index 1a05432162..a6e6552500 100644 --- a/src/lib/util/misc.c +++ b/src/lib/util/misc.c @@ -570,11 +570,12 @@ static int get_part(char **str, int *date, int min, int max, char term, char con * * @param date_str input date string. * @param date time_t to write result to. + * @param[in] hint scale for the parsing. Default is "seconds" * @return * - 0 on success. * - -1 on failure. */ -int fr_unix_time_from_str(fr_unix_time_t *date, char const *date_str) +int fr_unix_time_from_str(fr_unix_time_t *date, char const *date_str, fr_time_res_t hint) { int i; time_t t; @@ -591,7 +592,29 @@ int fr_unix_time_from_str(fr_unix_time_t *date, char const *date_str) */ t = strtoul(date_str, &tail, 10); if (*tail == '\0') { - *date = fr_unix_time_from_timeval(&(struct timeval) { .tv_sec = t }); + switch (hint) { + case FR_TIME_RES_SEC: + t = fr_unix_time_from_sec(t); + break; + + case FR_TIME_RES_MSEC: + t = fr_unix_time_from_msec(t); + break; + + case FR_TIME_RES_USEC: + t = fr_unix_time_from_usec(t); + break; + + case FR_TIME_RES_NSEC: + t = fr_unix_time_from_nsec(t); + break; + + default: + fr_strerror_printf("Invalid hint %d for time delta", hint); + return -1; + } + + *date = t; return 0; } diff --git a/src/lib/util/misc.h b/src/lib/util/misc.h index da6354777a..4c175e9c4a 100644 --- a/src/lib/util/misc.h +++ b/src/lib/util/misc.h @@ -198,7 +198,7 @@ int fr_blocking(int fd); ssize_t fr_writev(int fd, struct iovec vector[], int iovcnt, fr_time_delta_t timeout); ssize_t fr_utf8_to_ucs2(uint8_t *out, size_t outlen, char const *in, size_t inlen); size_t fr_snprint_uint128(char *out, size_t outlen, uint128_t const num); -int fr_unix_time_from_str(fr_unix_time_t *date, char const *date_str); +int fr_unix_time_from_str(fr_unix_time_t *date, char const *date_str, fr_time_res_t hint); bool fr_multiply(uint64_t *result, uint64_t lhs, uint64_t rhs); uint64_t fr_multiply_mod(uint64_t lhs, uint64_t rhs, uint64_t mod); diff --git a/src/lib/util/value.c b/src/lib/util/value.c index 67b719df0c..ca33ab60a5 100644 --- a/src/lib/util/value.c +++ b/src/lib/util/value.c @@ -1718,11 +1718,12 @@ ssize_t fr_value_box_from_network_dbuff(TALLOC_CTX *ctx, break; case FR_TIME_RES_MSEC: - date *= 1000000; + fprintf(stderr, "mSEC %lld --> nSEC %lld\n", date, date * (NSEC / MSEC)); + date *= (NSEC / MSEC); break; case FR_TIME_RES_USEC: - date *= 1000; + date *= (NSEC / USEC); break; case FR_TIME_RES_NSEC: @@ -4552,7 +4553,11 @@ parse: case FR_TYPE_DATE: { - if (fr_unix_time_from_str(&dst->vb_date, in) < 0) return -1; + if (dst_enumv) { + if (fr_unix_time_from_str(&dst->vb_date, in, dst_enumv->flags.flag_time_res) < 0) return -1; + } else { + if (fr_unix_time_from_str(&dst->vb_date, in, FR_TIME_RES_SEC) < 0) return -1; + } dst->enumv = dst_enumv; } @@ -4841,12 +4846,12 @@ ssize_t fr_value_box_print(fr_sbuff_t *out, fr_value_box_t const *data, fr_sbuff break; case FR_TIME_RES_MSEC: - subseconds /= 1000000; + subseconds /= (NSEC / MSEC); FR_SBUFF_IN_SPRINTF_RETURN(&our_out, ".%03" PRIi64, subseconds); break; case FR_TIME_RES_USEC: - subseconds /= 1000; + subseconds /= (NSEC / USEC); FR_SBUFF_IN_SPRINTF_RETURN(&our_out, ".%06" PRIi64, subseconds); break;