flexPTP 1.0
An IEEE 1588 PTP implementation designed for microcontrollers
Loading...
Searching...
No Matches
timeutils.h File Reference

This module defines storage classes for timestamps and operations on time values. More...

#include <stdbool.h>
#include <stdint.h>

Go to the source code of this file.

Data Structures

struct  TimestampU
 Timestamp (unsigned) More...
 
struct  TimestampI
 Timestamp (signed) More...
 

Macros

#define NANO_PREFIX   (1000000000)
 Integer nano prefix.
 
#define NANO_PREFIX_F   (1000000000.0f)
 Float nanno prefix.
 
#define T_SEC_PER_MINUTE   (60)
 seconds per minute
 
#define T_SEC_PER_HOUR   (60 * T_SEC_PER_MINUTE)
 seconds per hour
 
#define T_SEC_PER_DAY   (24 * T_SEC_PER_HOUR)
 seconds per day
 
#define T_SEC_PER_YEAR   (365 * T_SEC_PER_DAY)
 seconds per year
 
#define T_SEC_PER_LEAPYEAR   (366 * T_SEC_PER_DAY)
 seconds per leapyear
 
#define T_SEC_PER_FOURYEAR   (3 * T_SEC_PER_YEAR + T_SEC_PER_LEAPYEAR)
 seconds per four years
 

Functions

TimestampItsUToI (TimestampI *ti, const TimestampU *tu)
 
TimestampIaddTime (TimestampI *r, const TimestampI *a, const TimestampI *b)
 
TimestampIsubTime (TimestampI *r, const TimestampI *a, const TimestampI *b)
 
TimestampIdivTime (TimestampI *r, const TimestampI *a, int divisor)
 
uint64_t nsU (const TimestampU *t)
 
int64_t nsI (const TimestampI *t)
 
void normTime (TimestampI *t)
 
int64_t tsToTick (const TimestampI *ts, uint32_t tps)
 
TimestampInsToTsI (TimestampI *r, int64_t ns)
 
bool nonZeroI (const TimestampI *a)
 
void tsPrint (char *str, const TimestampI *ts)
 

Detailed Description

This module defines storage classes for timestamps and operations on time values.

Definition in file timeutils.h.

Macro Definition Documentation

◆ NANO_PREFIX

#define NANO_PREFIX   (1000000000)

Integer nano prefix.

Definition at line 34 of file timeutils.h.

◆ NANO_PREFIX_F

#define NANO_PREFIX_F   (1000000000.0f)

Float nanno prefix.

Definition at line 35 of file timeutils.h.

◆ T_SEC_PER_DAY

#define T_SEC_PER_DAY   (24 * T_SEC_PER_HOUR)

seconds per day

Definition at line 39 of file timeutils.h.

◆ T_SEC_PER_FOURYEAR

#define T_SEC_PER_FOURYEAR   (3 * T_SEC_PER_YEAR + T_SEC_PER_LEAPYEAR)

seconds per four years

Definition at line 42 of file timeutils.h.

◆ T_SEC_PER_HOUR

#define T_SEC_PER_HOUR   (60 * T_SEC_PER_MINUTE)

seconds per hour

Definition at line 38 of file timeutils.h.

◆ T_SEC_PER_LEAPYEAR

#define T_SEC_PER_LEAPYEAR   (366 * T_SEC_PER_DAY)

seconds per leapyear

Definition at line 41 of file timeutils.h.

◆ T_SEC_PER_MINUTE

#define T_SEC_PER_MINUTE   (60)

seconds per minute

Definition at line 37 of file timeutils.h.

◆ T_SEC_PER_YEAR

#define T_SEC_PER_YEAR   (365 * T_SEC_PER_DAY)

seconds per year

Definition at line 40 of file timeutils.h.

Function Documentation

◆ addTime()

TimestampI * addTime ( TimestampI r,
const TimestampI a,
const TimestampI b 
)

Add up timestamps. r = a + b;

Parameters
rresult
afirst operand
bsecond operand
Returns
r

Definition at line 16 of file timeutils.c.

◆ divTime()

TimestampI * divTime ( TimestampI r,
const TimestampI a,
int  divisor 
)

Divide (inversely scale) a time value by an integer.

Parameters
rpointer to the result field
adividend
divisordivisor
Returns
r

Definition at line 31 of file timeutils.c.

◆ nonZeroI()

bool nonZeroI ( const TimestampI a)

Does the timestamp differ from zero?

Parameters
atimestamp
Returns
a != 0

Definition at line 67 of file timeutils.c.

◆ normTime()

void normTime ( TimestampI t)

Normalize time. Move whole seconds from the nanoseconds field to the seconds.

Parameters
tTime. It will be overwritten.

Definition at line 49 of file timeutils.c.

◆ nsI()

int64_t nsI ( const TimestampI t)

Convert signed time into nanoseconds.

Parameters
ttime
Returns
time in nanoseconds

Definition at line 44 of file timeutils.c.

◆ nsToTsI()

TimestampI * nsToTsI ( TimestampI r,
int64_t  ns 
)

Convert nanoseconds to time.

Parameters
rresults
nstime in nanoseconds
Returns
r

Definition at line 61 of file timeutils.c.

◆ nsU()

uint64_t nsU ( const TimestampU t)

Convert unsigned time into nanoseconds.

Parameters
ttime
Returns
time in nanoseconds

Definition at line 39 of file timeutils.c.

◆ subTime()

TimestampI * subTime ( TimestampI r,
const TimestampI a,
const TimestampI b 
)

Substract timestamps. r = a - b;

Parameters
rresult
afirst operand
bsecond operand
Returns
r

Definition at line 24 of file timeutils.c.

◆ tsPrint()

void tsPrint ( char *  str,
const TimestampI ts 
)

Print datetime to string.

Parameters
strdestination area, must provide at least 20 consecutive bytes!
tspointer to timestamp object holding the time value

Definition at line 73 of file timeutils.c.

◆ tsToTick()

int64_t tsToTick ( const TimestampI ts,
uint32_t  tps 
)

Convert duration (~time value) to hardware ticks.

Parameters
tstime
tpsticks per second
Returns
duration in ticks (rounded to floor during division)

Definition at line 55 of file timeutils.c.

◆ tsUToI()

TimestampI * tsUToI ( TimestampI ti,
const TimestampU tu 
)

Convert unsigned timestamp to signed.

Parameters
tidestination (signed) timestamp
tusource (unsigned) timestamp

Definition at line 8 of file timeutils.c.