]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
add week / month / year time resolution
authorAlan T. DeKok <aland@freeradius.org>
Sun, 22 Jan 2023 11:33:06 +0000 (06:33 -0500)
committerAlan T. DeKok <aland@freeradius.org>
Sun, 22 Jan 2023 16:33:32 +0000 (11:33 -0500)
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

src/lib/util/time.c
src/lib/util/time.h

index 6d632e0ed0f84a2e23d1ca753c757fb82a3be7be..0003b44939685f3b628a3e8984b74763497f43b2 100644 (file)
@@ -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:
index 73ebd2c402597dfdffe39b3c74afcc91057185fb..b2630a16407cf0d0187948a5113c7797dd61c702 100644 (file)
@@ -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