mbgtools-lx  4.2.8
nanotime.c File Reference
#include <nanotime.h>
#include <mbgtime.h>
#include <mbgerror.h>
#include <mbgmktm.h>
#include <str_util.h>
#include <timeutil.h>

Go to the source code of this file.

Macros

#define _NANOTIME
 

Functions

double nano_time_to_double (const NANO_TIME *p)
 Convert a NANO_TIME time to double. More...
 
void double_to_nano_time (NANO_TIME *p, double d)
 Setup a NANO_TIME structure from a time provided as double. More...
 
size_t snprint_nano_time (char *s, size_t max_len, const NANO_TIME *nt)
 Print nano time into string buffer. More...
 
void normalize_nano_time_64 (NANO_TIME_64 *nt)
 Normalize a NANO_TIME_64 time. More...
 
double nano_time_64_to_double (const NANO_TIME_64 *p)
 Convert a NANO_TIME_64 time to double. More...
 
void double_to_nano_time_64 (NANO_TIME_64 *p, double d)
 Setup a NANO_TIME_64 structure from a time as double. More...
 
size_t snprint_nano_time_64 (char *s, size_t max_len, const NANO_TIME_64 *nt)
 Print a normalized NANO_TIME_64 into a string buffer. More...
 
static __mbg_inline void div_nano_time_64 (NANO_TIME_64 *p, long div, long mul)
 Generic function to divide NANO_TIME_64 value. More...
 
static __mbg_inline void str_to_nano_time_64 (const char *s, NANO_TIME_64 *p, int base, int frac_digits)
 Generic function to set up a NANO_TIME_64 structure from a string. More...
 
void str_s_to_nano_time_64 (const char *s, NANO_TIME_64 *p)
 Set up a NANO_TIME_64 structure from a string with a time in seconds and fractions. More...
 
void str_ms_to_nano_time_64 (const char *s, NANO_TIME_64 *p)
 Set up a NANO_TIME_64 structure from a string with a time in milliseconds and fractions. More...
 
int nano_time_64_to_tm_gps (TM_GPS *tm_gps, const NANO_TIME_64 *nt)
 Convert NANO_TIME_64 to TM_GPS. More...
 
int tm_gps_to_nano_time_64 (NANO_TIME_64 *nt, const TM_GPS *tm)
 Convert TM_GPS to NANO_TIME_64. More...
 

Macro Definition Documentation

◆ _NANOTIME

#define _NANOTIME

Definition at line 34 of file nanotime.c.

Function Documentation

◆ div_nano_time_64()

static __mbg_inline void div_nano_time_64 ( NANO_TIME_64 p,
long  div,
long  mul 
)
static

Generic function to divide NANO_TIME_64 value.

Parameters
[in,out]pAddress of a NANO_TIME_64 structure to be divided
[in]divThe number by which to divide
[in]mulThe complementary number to div, i.e. mul = 1000000000 / div

Definition at line 224 of file nanotime.c.

References NANO_TIME_64::nano_secs, and NANO_TIME_64::secs.

Referenced by str_ms_to_nano_time_64().

◆ double_to_nano_time()

void double_to_nano_time ( NANO_TIME p,
double  d 
)

Setup a NANO_TIME structure from a time provided as double.

Parameters
[out]pAddress of a NANO_TIME structure to be set up
[in]dThe time to be converted, in seconds with fractions, as double
See also
nano_time_to_double

Definition at line 80 of file nanotime.c.

References NANO_TIME::nano_secs, and NANO_TIME::secs.

◆ double_to_nano_time_64()

void double_to_nano_time_64 ( NANO_TIME_64 p,
double  d 
)

Setup a NANO_TIME_64 structure from a time as double.

Parameters
[out]pAddress of a NANO_TIME_64 structure to be set up
[in]dThe time to be converted, in seconds with fractions, as double
See also
nano_time_64_to_double

Definition at line 184 of file nanotime.c.

References NANO_TIME_64::nano_secs, and NANO_TIME_64::secs.

◆ nano_time_64_to_double()

double nano_time_64_to_double ( const NANO_TIME_64 p)

Convert a NANO_TIME_64 time to double.

Parameters
[in]pAddress of a NANO_TIME_64 structure to be converted
Returns
The computed number of seconds with fractions, as double
See also
double_to_nano_time_64

Definition at line 163 of file nanotime.c.

References NANO_TIME_64::nano_secs, and NANO_TIME_64::secs.

◆ nano_time_64_to_tm_gps()

int nano_time_64_to_tm_gps ( TM_GPS tm_gps,
const NANO_TIME_64 nt 
)

Convert NANO_TIME_64 to TM_GPS.

Parameters
[out]tm_gpsThe TM_GPS to be filled
[in]ntThe NANO_TIME_64 to be converted
Returns
1 on success, 0 on error
See also
tm_gps_to_nano_time_64

Definition at line 341 of file nanotime.c.

References cvt_to_time_t(), TM_GPS::frac, TM_GPS::hour, mbg_gmtime(), mbg_rc_is_success, TM_GPS::mday, TM_GPS::min, TM_GPS::month, TM_GPS::offs_from_utc, TM_GPS::sec, NANO_TIME_64::secs, TM_GPS::wday, TM_GPS::yday, and TM_GPS::year.

◆ nano_time_to_double()

double nano_time_to_double ( const NANO_TIME p)

Convert a NANO_TIME time to double.

Parameters
[in]pAddress of a NANO_TIME structure to be converted
Returns
The computed number of seconds with fractions, as double
See also
double_to_nano_time

Definition at line 59 of file nanotime.c.

References NANO_TIME::nano_secs, and NANO_TIME::secs.

◆ normalize_nano_time_64()

void normalize_nano_time_64 ( NANO_TIME_64 nt)

Normalize a NANO_TIME_64 time.

After normalization, the following can be assumed:

  • nano_secs is in the range [-10^9 + 1, 10^9 - 1]
  • if secs is not 0, secs and nano_secs have the same sign
Parameters
[in,out]ntThe NANO_TIME_64 to be normalized

Definition at line 128 of file nanotime.c.

References NANO_TIME_64::nano_secs, NSEC_PER_SEC, and NANO_TIME_64::secs.

◆ snprint_nano_time()

size_t snprint_nano_time ( char *  s,
size_t  max_len,
const NANO_TIME nt 
)

Print nano time into string buffer.

Parameters
[out]sThe string buffer to be filled
[in]max_lenSize of the output buffer for 0-terminated string
[in]ntThe NANO_TIME to be printed

Definition at line 102 of file nanotime.c.

References _nano_time_negative, NANO_TIME::nano_secs, NANO_TIME::secs, and snprintf_safe().

Referenced by show_ptp_state().

◆ snprint_nano_time_64()

size_t snprint_nano_time_64 ( char *  s,
size_t  max_len,
const NANO_TIME_64 nt 
)

Print a normalized NANO_TIME_64 into a string buffer.

Parameters
[out]sThe string buffer to be filled
[in]max_lenSize of the output buffer for 0-terminated string
[in]ntThe NANO_TIME_64 to be printed

Definition at line 204 of file nanotime.c.

References _abs64, _nano_time_negative, NANO_TIME_64::nano_secs, NANO_TIME_64::secs, and snprintf_safe().

◆ str_ms_to_nano_time_64()

void str_ms_to_nano_time_64 ( const char *  s,
NANO_TIME_64 p 
)

Set up a NANO_TIME_64 structure from a string with a time in milliseconds and fractions.

Parameters
[in]sA string with a time in milliseconds, with fractions separated by decimal point
[out]pAddress of a NANO_TIME_64 structure to be set up

Definition at line 315 of file nanotime.c.

References div_nano_time_64(), and str_s_to_nano_time_64().

◆ str_s_to_nano_time_64()

void str_s_to_nano_time_64 ( const char *  s,
NANO_TIME_64 p 
)

Set up a NANO_TIME_64 structure from a string with a time in seconds and fractions.

Parameters
[in]sA string with a time in seconds, with fractions separated by decimal point
[out]pAddress of a NANO_TIME_64 structure to be set up

Definition at line 300 of file nanotime.c.

References str_to_nano_time_64().

Referenced by str_ms_to_nano_time_64().

◆ str_to_nano_time_64()

static __mbg_inline void str_to_nano_time_64 ( const char *  s,
NANO_TIME_64 p,
int  base,
int  frac_digits 
)
static

Generic function to set up a NANO_TIME_64 structure from a string.

Parameters
[in]sA string with a time in seconds, with fractions separated by decimal point
[out]pAddress of a NANO_TIME_64 structure to be set up
[in]baseThe base of the conversion, i.e. 10 for a string with decimal numbers, or 16 for a hex string
[in]frac_digitsThe number of fractions required to represent 1 nanosecond, e.g. 9 for a decimal string like 0.000000001, or TODO for a hex string like 0x00000000.00000000

Definition at line 254 of file nanotime.c.

References NANO_TIME_64::nano_secs, and NANO_TIME_64::secs.

Referenced by str_s_to_nano_time_64().

◆ tm_gps_to_nano_time_64()

int tm_gps_to_nano_time_64 ( NANO_TIME_64 nt,
const TM_GPS tm 
)

Convert TM_GPS to NANO_TIME_64.

Parameters
[out]ntThe NANO_TIME_64 to be filled
[in]tmThe TM_GPS to be converted
Returns
1 on success, 0 on error
See also
nano_time_64_to_tm_gps

Definition at line 380 of file nanotime.c.

References TM_GPS::hour, mbg_mktime(), TM_GPS::mday, TM_GPS::min, TM_GPS::month, NANO_TIME_64::nano_secs, TM_GPS::sec, NANO_TIME_64::secs, and TM_GPS::year.