Platform independent time functions.  
More...
#include <freeradius-devel/autoconf.h>
#include <freeradius-devel/util/time.h>
#include <freeradius-devel/util/skip.h>
Go to the source code of this file.
|  | 
| #define | CHECK(_x,  _max)   if ((tm->tm_ ## _x < 0) || (tm->tm_ ## _x >= _max)) tm->tm_ ## _x = _max - 1 | 
|  | 
| #define | MOD(a,  b)   (((a<0) ? (-a) : (a))%(b)) | 
|  | 
|  | 
| fr_slen_t | fr_time_delta_from_str (fr_time_delta_t *out, char const *in, size_t inlen, fr_time_res_t hint) | 
|  | Create fr_time_delta_t from a string. 
 | 
|  | 
| fr_slen_t | fr_time_delta_from_substr (fr_time_delta_t *out, fr_sbuff_t *in, fr_time_res_t hint, bool no_trailing, fr_sbuff_term_t const *tt) | 
|  | Create fr_time_delta_t from a string. 
 | 
|  | 
| int | fr_time_delta_from_time_zone (char const *tz, fr_time_delta_t *delta) | 
|  | Return time delta from the time zone. 
 | 
|  | 
| fr_slen_t | fr_time_delta_to_str (fr_sbuff_t *out, fr_time_delta_t delta, fr_time_res_t res, bool is_unsigned) | 
|  | Print fr_time_delta_t to a string with an appropriate suffix. 
 | 
|  | 
| void | fr_time_elapsed_fprint (FILE *fp, fr_time_elapsed_t const *elapsed, char const *prefix, int tab_offset) | 
|  | 
| void | fr_time_elapsed_update (fr_time_elapsed_t *elapsed, fr_time_t start, fr_time_t end) | 
|  | 
| fr_time_delta_t | fr_time_gmtoff (void) | 
|  | Get the offset to gmt. 
 | 
|  | 
| bool | fr_time_is_dst (void) | 
|  | Whether or not we're daylight savings. 
 | 
|  | 
| int64_t | fr_time_scale (int64_t t, fr_time_res_t hint) | 
|  | Scale an input time to NSEC, clamping it at max / min. 
 | 
|  | 
| int | fr_time_start (void) | 
|  | Initialize the local time. 
 | 
|  | 
| size_t | fr_time_strftime_local (fr_sbuff_t *out, fr_time_t time, char const *fmt) | 
|  | Copy a time string (local timezone) to an sbuff. 
 | 
|  | 
| size_t | fr_time_strftime_utc (fr_sbuff_t *out, fr_time_t time, char const *fmt) | 
|  | Copy a time string (UTC) to an sbuff. 
 | 
|  | 
| int | fr_time_sync (void) | 
|  | Get a new fr_time_monotonic_to_realtime value. 
 | 
|  | 
| int | fr_unix_time_from_str (fr_unix_time_t *date, char const *date_str, fr_time_res_t hint) | 
|  | Convert string in various formats to a fr_unix_time_t. 
 | 
|  | 
| fr_unix_time_t | fr_unix_time_from_tm (struct tm *tm) | 
|  | 
| fr_slen_t | fr_unix_time_to_str (fr_sbuff_t *out, fr_unix_time_t time, fr_time_res_t res, bool utc) | 
|  | Convert unix time to string. 
 | 
|  | 
| static int | get_part (char **str, int *date, int min, int max, char term, char const *name) | 
|  | 
| static char * | mystrtok (char **ptr, char const *sep) | 
|  | 
Platform independent time functions. 
- Id
- a965af202b4766b99523aa2d2cc47834494cbf57 
- Copyright
- 2016-2019 Alan DeKok (aland.nosp@m.@fre.nosp@m.eradi.nosp@m.us.o.nosp@m.rg) 
- 
2019-2020 Arran Cudbard-Bell (a.cud.nosp@m.bard.nosp@m.b@fre.nosp@m.erad.nosp@m.ius.o.nosp@m.rg) 
Definition in file time.c.
◆ CHECK
      
        
          | #define CHECK | ( |  | _x, | 
        
          |  |  |  | _max | 
        
          |  | ) |  | if ((tm->tm_ ## _x < 0) || (tm->tm_ ## _x >= _max)) tm->tm_ ## _x = _max - 1 | 
      
 
 
◆ MOD
      
        
          | #define MOD | ( |  | a, | 
        
          |  |  |  | b | 
        
          |  | ) |  | (((a<0) ? (-a) : (a))%(b)) | 
      
 
 
◆ fr_time_delta_from_str()
Create fr_time_delta_t from a string. 
- Parameters
- 
  
    | [out] | out | Where to write fr_time_delta_t |  | [in] | in | String to parse. |  | [in] | inlen | Length of string. |  | [in] | hint | scale for the parsing. Default is "seconds" |  
 
- Returns
- 
- >0 on success.
- <0 on failure. 
 
Definition at line 412 of file time.c.
 
 
◆ fr_time_delta_from_substr()
Create fr_time_delta_t from a string. 
- Parameters
- 
  
    | [out] | out | Where to write fr_time_delta_t |  | [in] | in | String to parse. |  | [in] | hint | scale for the parsing. Default is "seconds". |  | [in] | no_trailing | asserts that there should be a terminal sequence after the time delta. Allows us to produce better errors. |  | [in] | tt | terminal sequences. |  
 
- Returns
- 
- >= 0 on success.
- <0 on failure. 
 
Definition at line 214 of file time.c.
 
 
◆ fr_time_delta_from_time_zone()
      
        
          | int fr_time_delta_from_time_zone | ( | char const * | tz, | 
        
          |  |  | fr_time_delta_t * | delta | 
        
          |  | ) |  |  | 
      
 
Return time delta from the time zone. 
Returns the delta between UTC and the timezone specified by tz
- Parameters
- 
  
    | [in] | tz | time zone name |  | [out] | delta | the time delta |  
 
- Returns
- 
- 0 converted OK
- <0 on error
 
- Note
- This function ONLY handles a limited number of time zones: local and gmt. It is impossible in general to parse arbitrary time zone strings, as there are duplicates. 
Definition at line 176 of file time.c.
 
 
◆ fr_time_delta_to_str()
Print fr_time_delta_t to a string with an appropriate suffix. 
- Parameters
- 
  
    | [out] | out | Where to write the string version of the time delta. |  | [in] | delta | to print. |  | [in] | res | to print resolution with. |  | [in] | is_unsigned | whether the value should be printed unsigned. |  
 
- Returns
- 
- >0 the number of bytes written to out.
- <0 how many additional bytes would have been required. 
 
Definition at line 435 of file time.c.
 
 
◆ fr_time_elapsed_fprint()
      
        
          | void fr_time_elapsed_fprint | ( | FILE * | fp, | 
        
          |  |  | fr_time_elapsed_t const * | elapsed, | 
        
          |  |  | char const * | prefix, | 
        
          |  |  | int | tab_offset | 
        
          |  | ) |  |  | 
      
 
 
◆ fr_time_elapsed_update()
◆ fr_time_gmtoff()
Get the offset to gmt. 
Definition at line 1199 of file time.c.
 
 
◆ fr_time_is_dst()
      
        
          | bool fr_time_is_dst | ( | void |  | ) |  | 
      
 
Whether or not we're daylight savings. 
Definition at line 1207 of file time.c.
 
 
◆ fr_time_scale()
Scale an input time to NSEC, clamping it at max / min. 
- Parameters
- 
  
    | t | input time / time delta |  | hint | time resolution hint |  
 
- Returns
- 
- INT64_MIN on underflow
- 0 on invalid hint
- INT64_MAX on overflow
- otherwise a valid number, multiplied by the relevant scale, so that the result is in nanoseconds. 
 
Definition at line 683 of file time.c.
 
 
◆ fr_time_start()
      
        
          | int fr_time_start | ( | void |  | ) |  | 
      
 
Initialize the local time. 
MUST be called when the program starts. MUST NOT be called after that.
- Returns
- 
Definition at line 150 of file time.c.
 
 
◆ fr_time_strftime_local()
Copy a time string (local timezone) to an sbuff. 
- Note
- This function will attempt to extend the sbuff by double the length of the fmt string. It is recommended to either pre-extend the sbuff before calling this function, or avoid using format specifiers that expand to character strings longer than 4 bytes.
- Parameters
- 
  
    | [in] | out | Where to write the formatted time string. |  | [in] | time | Internal server time to convert to wallclock time and copy out as formatted string. |  | [in] | fmt | Time format string. |  
 
- Returns
- 
- >0 the number of bytes written to the sbuff.
- 0 if there's insufficient space in the sbuff. 
 
Definition at line 503 of file time.c.
 
 
◆ fr_time_strftime_utc()
Copy a time string (UTC) to an sbuff. 
- Note
- This function will attempt to extend the sbuff by double the length of the fmt string. It is recommended to either pre-extend the sbuff before calling this function, or avoid using format specifiers that expand to character strings longer than 4 bytes.
- Parameters
- 
  
    | [in] | out | Where to write the formatted time string. |  | [in] | time | Internal server time to convert to wallclock time and copy out as formatted string. |  | [in] | fmt | Time format string. |  
 
- Returns
- 
- >0 the number of bytes written to the sbuff.
- 0 if there's insufficient space in the sbuff. 
 
Definition at line 532 of file time.c.
 
 
◆ fr_time_sync()
      
        
          | int fr_time_sync | ( | void |  | ) |  | 
      
 
Get a new fr_time_monotonic_to_realtime value. 
Should be done regularly to adjust for changes in system time.
- Returns
- 
- 0 on success.
- -1 on failure. 
 
Definition at line 102 of file time.c.
 
 
◆ fr_unix_time_from_str()
Convert string in various formats to a fr_unix_time_t. 
- Parameters
- 
  
    |  | date_str | input date string. |  |  | date | time_t to write result to. |  | [in] | hint | scale for the parsing. Default is "seconds" |  
 
- Returns
- 
- 0 on success.
- -1 on failure. 
 
Definition at line 794 of file time.c.
 
 
◆ fr_unix_time_from_tm()
◆ fr_unix_time_to_str()
Convert unix time to string. 
- Parameters
- 
  
    | [out] | out | Where to write the string. |  | [in] | time | to convert. |  | [in] | res | What base resolution to print the time as. |  | [in] | utc | If true, use UTC, otherwise local time. |  
 
- Returns
- 
- 0 on success.
- -1 on failure. 
 
Definition at line 1122 of file time.c.
 
 
◆ get_part()
  
  | 
        
          | static int get_part | ( | char ** | str, |  
          |  |  | int * | date, |  
          |  |  | int | min, |  
          |  |  | int | max, |  
          |  |  | char | term, |  
          |  |  | char const * | name |  
          |  | ) |  |  |  | static | 
 
 
◆ mystrtok()
  
  | 
        
          | static char * mystrtok | ( | char ** | ptr, |  
          |  |  | char const * | sep |  
          |  | ) |  |  |  | static | 
 
 
◆ fr_time_epoch
monotonic clock at boot, i.e. our epoch 
Definition at line 86 of file time.c.
 
 
◆ fr_time_monotonic_to_realtime
      
        
          | _Atomic int64_t fr_time_monotonic_to_realtime | 
      
 
difference between the two clocks 
Definition at line 87 of file time.c.
 
 
◆ fr_time_multiplier_by_res
      
        
          | int64_t const fr_time_multiplier_by_res[] | 
      
 
Initial value:= {
}
#define FR_TIME_DUR_MONTH
Definition at line 32 of file time.c.
 
 
◆ fr_time_precision_table
◆ fr_time_precision_table_len
◆ gmtoff
◆ isdst
◆ months
Initial value:= {
        "jan", "feb", "mar", "apr", "may", "jun",
        "jul", "aug", "sep", "oct", "nov", "dec" }
Definition at line 780 of file time.c.
 
 
◆ names
Initial value:= {
        "1us", "10us", "100us",
        "1ms", "10ms", "100ms",
        "1s", "10s"
}
Definition at line 584 of file time.c.
 
 
◆ tab_string
  
  | 
        
          | char const* tab_string = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t" |  | static | 
 
 
◆ tz_names
  
  | 
        
          | char const* tz_names[2] = { NULL, NULL } |  | static |