From: Alan T. DeKok Date: Sun, 22 Jan 2023 11:33:06 +0000 (-0500) Subject: add week / month / year time resolution X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=83b50a7d9fb370875d64de3f8df700bb46d90040;p=thirdparty%2Ffreeradius-server.git add week / month / year time resolution which are vaguely correct for months, and correct to the second for years. The main purpose here is to allow for at least rough parsing of years / months --- diff --git a/src/lib/util/time.c b/src/lib/util/time.c index 6d632e0ed0f..0003b449396 100644 --- a/src/lib/util/time.c +++ b/src/lib/util/time.c @@ -56,7 +56,10 @@ int64_t const fr_time_multiplier_by_res[] = { [FR_TIME_RES_SEC] = NSEC, [FR_TIME_RES_MIN] = (int64_t)NSEC * 60, [FR_TIME_RES_HOUR] = (int64_t)NSEC * 3600, - [FR_TIME_RES_DAY] = (int64_t)NSEC * 86400 + [FR_TIME_RES_DAY] = (int64_t)NSEC * 86400, + [FR_TIME_RES_WEEK] = (int64_t)NSEC * 86400 * 7, + [FR_TIME_RES_MONTH] = FR_TIME_DUR_MONTH, + [FR_TIME_RES_YEAR] = FR_TIME_DUR_YEAR, }; fr_table_num_ordered_t const fr_time_precision_table[] = { @@ -82,12 +85,20 @@ fr_table_num_ordered_t const fr_time_precision_table[] = { { L("h"), FR_TIME_RES_HOUR }, { L("days"), FR_TIME_RES_DAY }, - { L("d"), FR_TIME_RES_DAY } + { L("d"), FR_TIME_RES_DAY }, + + { L("weeks"), FR_TIME_RES_WEEK }, + { L("w"), FR_TIME_RES_WEEK }, /* - * Note that there is no TIME_RES_MONTH or TIME_RES_YEAR, because months and years have variable - * lengths. + * These use special values FR_TIME_DUR_MONTH and FR_TIME_DUR_YEAR */ + { L("months"), FR_TIME_RES_MONTH }, + { L("M"), FR_TIME_RES_MONTH }, + + { L("years"), FR_TIME_RES_YEAR }, + { L("y"), FR_TIME_RES_YEAR }, + }; size_t fr_time_precision_table_len = NUM_ELEMENTS(fr_time_precision_table); @@ -1226,6 +1237,9 @@ fr_slen_t fr_unix_time_to_str(fr_sbuff_t *out, fr_unix_time_t time, fr_time_res_ */ switch (res) { case FR_TIME_RES_INVALID: + case FR_TIME_RES_YEAR: + case FR_TIME_RES_MONTH: + case FR_TIME_RES_WEEK: case FR_TIME_RES_DAY: case FR_TIME_RES_HOUR: case FR_TIME_RES_MIN: diff --git a/src/lib/util/time.h b/src/lib/util/time.h index 73ebd2c4025..b2630a16407 100644 --- a/src/lib/util/time.h +++ b/src/lib/util/time.h @@ -44,6 +44,9 @@ typedef enum { FR_TIME_RES_MIN, FR_TIME_RES_HOUR, FR_TIME_RES_DAY, + FR_TIME_RES_WEEK, + FR_TIME_RES_MONTH, + FR_TIME_RES_YEAR, FR_TIME_RES_CSEC, FR_TIME_RES_MSEC, FR_TIME_RES_USEC, @@ -368,6 +371,18 @@ typedef struct { #define MSEC (1000) #define CSEC (100) +/* + * Pre-defined "magic" values for time in a month and year. The number of seconds in a year is: + * + * 1 year is 365.2425 days. times 86400 seconds in a day. + * + * The average month is simply one twelfth of that. Note that the exact value for both year and month + * duration are really magic values, which people will never stumble upon themselves. As such, they can + * be used (somewhat, in some cases) as magic tokens meaning "year" or "month". + */ +#define FR_TIME_DUR_YEAR ((int64_t)NSEC * 31556952) +#define FR_TIME_DUR_MONTH (FR_TIME_DUR_YEAR/12) + extern _Atomic int64_t our_realtime; #ifdef HAVE_CLOCK_GETTIME