The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Macros | Functions | Variables
time.c File Reference

Platform independent time functions. More...

#include <freeradius-devel/autoconf.h>
#include <freeradius-devel/util/time.h>
#include <freeradius-devel/util/misc.h>
+ Include dependency graph for time.c:

Go to the source code of this file.

Macros

#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))
 

Functions

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. More...
 
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. More...
 
int fr_time_delta_from_time_zone (char const *tz, fr_time_delta_t *delta)
 Return time delta from the time zone. More...
 
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. More...
 
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. More...
 
bool fr_time_is_dst (void)
 Whether or not we're daylight savings. More...
 
int64_t fr_time_scale (int64_t t, fr_time_res_t hint)
 Scale an input time to NSEC, clamping it at max / min. More...
 
int fr_time_start (void)
 Initialize the local time. More...
 
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. More...
 
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. More...
 
int fr_time_sync (void)
 Get a new fr_time_monotonic_to_realtime value. More...
 
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. More...
 
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)
 Convert unix time to string. More...
 
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)
 

Variables

int64_t fr_time_epoch
 monotonic clock at boot, i.e. our epoch More...
 
_Atomic int64_t fr_time_monotonic_to_realtime
 difference between the two clocks More...
 
int64_t const fr_time_multiplier_by_res []
 
fr_table_num_ordered_t const fr_time_precision_table []
 
size_t fr_time_precision_table_len = NUM_ELEMENTS(fr_time_precision_table)
 
static long gmtoff [2] = {0, 0}
 from localtime_r(), tm_gmtoff More...
 
static bool isdst = false
 from localtime_r(), tm_is_dst More...
 
static char const * months []
 
static const char * names [8]
 
static char const * tab_string = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
 
static char const * tz_names [2] = { NULL, NULL }
 normal, DST, from localtime_r(), tm_zone More...
 

Detailed Description

Platform independent time functions.

Id
dd999be5d429b01edf95ccba99036b573374eda0

Definition in file time.c.

Macro Definition Documentation

◆ CHECK

#define CHECK (   _x,
  _max 
)    if ((tm->tm_ ## _x < 0) || (tm->tm_ ## _x >= _max)) tm->tm_ ## _x = _max - 1

◆ MOD

#define MOD (   a,
 
)    (((a<0) ? (-a) : (a))%(b))

Function Documentation

◆ fr_time_delta_from_str()

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.

Parameters
[out]outWhere to write fr_time_delta_t
[in]inString to parse.
[in]inlenLength of string.
[in]hintscale for the parsing. Default is "seconds"
Returns
  • >0 on success.
  • <0 on failure.

Definition at line 445 of file time.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_time_delta_from_substr()

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.

Parameters
[out]outWhere to write fr_time_delta_t
[in]inString to parse.
[in]hintscale for the parsing. Default is "seconds".
[in]no_trailingasserts that there should be a terminal sequence after the time delta. Allows us to produce better errors.
[in]ttterminal sequences.
Returns
  • >= 0 on success.
  • <0 on failure.

Definition at line 214 of file time.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ 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]tztime zone name
[out]deltathe 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.

+ Here is the caller graph for this function:

◆ fr_time_delta_to_str()

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.

Parameters
[out]outWhere to write the string version of the time delta.
[in]deltato print.
[in]resto print resolution with.
[in]is_unsignedwhether 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 468 of file time.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_time_elapsed_fprint()

void fr_time_elapsed_fprint ( FILE *  fp,
fr_time_elapsed_t const *  elapsed,
char const *  prefix,
int  tab_offset 
)

Definition at line 625 of file time.c.

+ Here is the caller graph for this function:

◆ fr_time_elapsed_update()

void fr_time_elapsed_update ( fr_time_elapsed_t elapsed,
fr_time_t  start,
fr_time_t  end 
)

Definition at line 580 of file time.c.

+ Here is the caller graph for this function:

◆ fr_time_gmtoff()

fr_time_delta_t fr_time_gmtoff ( void  )

Get the offset to gmt.

Definition at line 1233 of file time.c.

+ Here is the caller graph for this function:

◆ fr_time_is_dst()

bool fr_time_is_dst ( void  )

Whether or not we're daylight savings.

Definition at line 1241 of file time.c.

+ Here is the caller graph for this function:

◆ fr_time_scale()

int64_t fr_time_scale ( int64_t  t,
fr_time_res_t  hint 
)

Scale an input time to NSEC, clamping it at max / min.

Parameters
tinput time / time delta
hinttime 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 716 of file time.c.

+ Here is the caller graph for this function:

◆ 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
  • <0 on error
  • 0 on success

Definition at line 150 of file time.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_time_strftime_local()

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.

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]outWhere to write the formatted time string.
[in]timeInternal server time to convert to wallclock time and copy out as formatted string.
[in]fmtTime format string.
Returns
  • >0 the number of bytes written to the sbuff.
  • 0 if there's insufficient space in the sbuff.

Definition at line 536 of file time.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_time_strftime_utc()

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.

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]outWhere to write the formatted time string.
[in]timeInternal server time to convert to wallclock time and copy out as formatted string.
[in]fmtTime format string.
Returns
  • >0 the number of bytes written to the sbuff.
  • 0 if there's insufficient space in the sbuff.

Definition at line 565 of file time.c.

+ Here is the call graph for this function:

◆ 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.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_unix_time_from_str()

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.

Parameters
date_strinput date string.
datetime_t to write result to.
[in]hintscale for the parsing. Default is "seconds"
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 827 of file time.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_unix_time_from_tm()

fr_unix_time_t fr_unix_time_from_tm ( struct tm *  tm)

Definition at line 661 of file time.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_unix_time_to_str()

fr_slen_t fr_unix_time_to_str ( fr_sbuff_t out,
fr_unix_time_t  time,
fr_time_res_t  res 
)

Convert unix time to string.

Parameters
[out]outWhere to write the string.
[in]timeto convert.
[in]resWhat base resolution to print the time as.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 1154 of file time.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_part()

static int get_part ( char **  str,
int *  date,
int  min,
int  max,
char  term,
char const *  name 
)
static

Definition at line 779 of file time.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mystrtok()

static char* mystrtok ( char **  ptr,
char const *  sep 
)
static

Definition at line 757 of file time.c.

+ Here is the caller graph for this function:

Variable Documentation

◆ fr_time_epoch

int64_t 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:
= {
[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_WEEK] = (int64_t)NSEC * 86400 * 7,
}
#define MSEC
Definition: time.h:379
@ FR_TIME_RES_MONTH
Definition: time.h:55
@ FR_TIME_RES_MSEC
Definition: time.h:58
@ FR_TIME_RES_WEEK
Definition: time.h:54
@ FR_TIME_RES_MIN
Definition: time.h:51
@ FR_TIME_RES_CSEC
Definition: time.h:57
@ FR_TIME_RES_HOUR
Definition: time.h:52
@ FR_TIME_RES_YEAR
Definition: time.h:56
@ FR_TIME_RES_DAY
Definition: time.h:53
@ FR_TIME_RES_NSEC
Definition: time.h:60
@ FR_TIME_RES_USEC
Definition: time.h:59
@ FR_TIME_RES_SEC
Definition: time.h:50
#define NSEC
Definition: time.h:377
#define USEC
Definition: time.h:378
#define FR_TIME_DUR_MONTH
Definition: time.h:392
#define CSEC
Definition: time.h:380
#define FR_TIME_DUR_YEAR
Definition: time.h:391

Definition at line 32 of file time.c.

◆ fr_time_precision_table

fr_table_num_ordered_t const fr_time_precision_table[]

Definition at line 46 of file time.c.

◆ fr_time_precision_table_len

size_t fr_time_precision_table_len = NUM_ELEMENTS(fr_time_precision_table)

Definition at line 84 of file time.c.

◆ gmtoff

long gmtoff[2] = {0, 0}
static

from localtime_r(), tm_gmtoff

Definition at line 90 of file time.c.

◆ isdst

bool isdst = false
static

from localtime_r(), tm_is_dst

Definition at line 91 of file time.c.

◆ months

char const* months[]
static
Initial value:
= {
"jan", "feb", "mar", "apr", "may", "jun",
"jul", "aug", "sep", "oct", "nov", "dec" }

Definition at line 813 of file time.c.

◆ names

const char* names[8]
static
Initial value:
= {
"1us", "10us", "100us",
"1ms", "10ms", "100ms",
"1s", "10s"
}

Definition at line 617 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

Definition at line 623 of file time.c.

◆ tz_names

char const* tz_names[2] = { NULL, NULL }
static

normal, DST, from localtime_r(), tm_zone

Definition at line 89 of file time.c.