]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
it's now fr_unix_time_from_tm()
authorAlan T. DeKok <aland@freeradius.org>
Wed, 25 Aug 2021 21:17:26 +0000 (17:17 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Wed, 25 Aug 2021 21:17:26 +0000 (17:17 -0400)
all of the uses of it either set the time zone, or used mktime()
and then called fr_unix_time_from_timeval().  So to simplify things,
we just use fr_unix_time_from_tm()

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

index f075372015d503d81d8f2a7f06c43d664aba44a3..9b50678290918e9b91239e9494a272a9f800e6c1 100644 (file)
@@ -699,25 +699,24 @@ int fr_unix_time_from_str(fr_unix_time_t *date, char const *date_str, fr_time_re
                }
 
                /*
-                *      We set this, but the timegm() function ignores
-                *      it.  Note also that mktime() ignores it too,
-                *      and treats the time zone as local.
+                *      We set the time zone, but the timegm()
+                *      function ignores it.  Note also that mktime()
+                *      ignores it too, and treats the time zone as
+                *      local.
                 *
                 *      We can't store this value in s_tm.gtmoff,
                 *      because the timegm() function helpfully zeros
                 *      it out.
+                *
+                *      So insyead of using stupid C library
+                *      functions, we just roll our own.
                 */
                tz = tz_hour * 3600 + tz_min;
                if (*tail == '-') tz *= -1;
 
        done:
-               *date = fr_unix_time_from_utc(tm);
-
-               /*
-                *      Add in the time zone offset, which the posix
-                *      functions are too stupid to do.
-                */
-               *date += fr_unix_time_from_sec(tz);
+               tm->tm_gmtoff = tz;
+               *date = fr_unix_time_from_tm(tm);
                *date += subseconds;
 
                return 0;
@@ -752,8 +751,7 @@ int fr_unix_time_from_str(fr_unix_time_t *date, char const *date_str, fr_time_re
                                p = strptime(my_str, "%b %e %Y %H:%M:%S %Z", tm);
                                if (p && (*p == '\0')) {
                                        talloc_free(my_str);
-                                       t = mktime(tm);
-                                       *date = fr_unix_time_from_timeval(&(struct timeval) { .tv_sec = t });
+                                       *date = fr_unix_time_from_tm(tm);
                                        return 0;
                                }
                                talloc_free(my_str);
@@ -764,8 +762,7 @@ int fr_unix_time_from_str(fr_unix_time_t *date, char const *date_str, fr_time_re
 
        p = strptime(date_str, "%b %e %Y %H:%M:%S %Z", tm);
        if (p && (*p == '\0')) {
-               t = mktime(tm);
-               *date = fr_unix_time_from_timeval(&(struct timeval) { .tv_sec = t });
+               *date = fr_unix_time_from_tm(tm);
                return 0;
        }
 
@@ -895,13 +892,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]);
        }
 
-       *date = fr_unix_time_from_utc(tm);
-
-       /*
-        *      Add in the time zone offset, which the posix
-        *      functions are too stupid to do.
-        */
-       *date += gmtoff;
+       *date = fr_unix_time_from_tm(tm) + gmtoff;
 
        return 0;
 }
index 6223262b1237767cdc5d475c76352926ee7bc628..dbb214fd13521216ba9db5c4753fd290bcc1a339 100644 (file)
@@ -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_unix_time_from_utc(struct tm *tm)
+fr_unix_time_t fr_unix_time_from_tm(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_unix_time_from_utc(struct tm *tm)
         *      2472692 adjusts the days for Unix epoch.  It is calculated as
         *      (365.2425 * (4800 + 1970))
         */
-       return fr_unix_time_from_sec((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 + tm->tm_gmtoff);
 }
index 2fed708d6807c3327657b30d616e1f3d24402804..d4491f5a74c3f456354794d69ed4beed1ecb8f10 100644 (file)
@@ -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_unix_time_from_utc(struct tm *tm) CC_HINT(nonnull);
+fr_unix_time_t fr_unix_time_from_tm(struct tm *tm) CC_HINT(nonnull);
 
 #ifdef __cplusplus
 }