Yate
Public Member Functions | Static Public Member Functions | List of all members
Time Class Reference

A time holding class. More...

#include <yateclass.h>

Public Member Functions

 Time ()
 
 Time (u_int64_t usec)
 
 Time (const struct timeval *tv)
 
 Time (const struct timeval &tv)
 
 ~Time ()
 
u_int32_t sec () const
 
u_int64_t msec () const
 
u_int64_t usec () const
 
 operator u_int64_t () const
 
Timeoperator= (u_int64_t usec)
 
Timeoperator+= (int64_t delta)
 
Timeoperator-= (int64_t delta)
 
void toTimeval (struct timeval *tv) const
 
uint32_t toNtp (uint32_t *over=0, bool rfc2030=true)
 

Static Public Member Functions

static void toTimeval (struct timeval *tv, u_int64_t usec)
 
static u_int64_t fromTimeval (const struct timeval *tv)
 
static u_int64_t fromTimeval (const struct timeval &tv)
 
static u_int64_t now ()
 
static u_int64_t msecNow ()
 
static u_int32_t secNow ()
 
static unsigned int toEpoch (int year, unsigned int month, unsigned int day, unsigned int hour, unsigned int minute, unsigned int sec, int offset=0)
 
static bool toDateTime (unsigned int epochTimeSec, int &year, unsigned int &month, unsigned int &day, unsigned int &hour, unsigned int &minute, unsigned int &sec, unsigned int *wDay=0)
 
static uint32_t toNtp (uint32_t sec, uint32_t *over=0, bool rfc2030=true)
 
static uint32_t fromNtp (uint32_t val, uint32_t *under=0, bool rfc2030=true)
 
static unsigned int toString (char *buf, uint64_t time, int frac=0)
 
static unsigned int appendTo (String &buf, uint64_t time, int frac=0)
 
static uint64_t toEpoch (const char *buf, unsigned int len, int frac=0)
 
static bool isLeap (unsigned int year)
 
static int timeZone (u_int32_t when=secNow())
 

Detailed Description

A time holding class.

The Time class holds a time moment with microsecond accuracy

Constructor & Destructor Documentation

Time ( )
inline

Constructs a Time object from the current time

Time ( u_int64_t  usec)
inline

Constructs a Time object from a given time

Parameters
usecTime in microseconds
Time ( const struct timeval *  tv)
inlineexplicit

Constructs a Time object from a timeval structure pointer

Parameters
tvPointer to the timeval structure
Time ( const struct timeval &  tv)
inlineexplicit

Constructs a Time object from a timeval structure

Parameters
tvReference of the timeval structure
~Time ( )
inline

Do-nothing destructor that keeps the compiler from complaining about inlining derivates or members of Time type

Member Function Documentation

static unsigned int appendTo ( String buf,
uint64_t  time,
int  frac = 0 
)
inlinestatic

Convert system time to a string representation Minimal representation is yyyy-mm-ddThh:mm:ssZ Destination buffer length must be at least 20 if no fractions are going to be filled For milliseconds fractions extra 4 bytes must be available in buffer For microseconds fractions extra 7 bytes must be available in buffer

Parameters
bufBuffer to append to
timeSystem time in microseconds to convert
fracAdd second fractions. 0: none, negative: microseconds, positive: milliseconds
Returns
The number of chars added to buffer. 0 on failure

References String::append(), and Time::toString().

static uint32_t fromNtp ( uint32_t  val,
uint32_t *  under = 0,
bool  rfc2030 = true 
)
static

Convert 32bit NTP (seconds since 1900) to system time

Parameters
valNTP time in seconds
underOptional destination for underflow value (given time is before EPOCH). Must be reset before calling this method
rfc2030Handle time extension as specified in RFC 2030 Section 3
Returns
System time value in seconds. 0 if underflow
static u_int64_t fromTimeval ( const struct timeval *  tv)
static

Convert time in a timeval struct to microseconds

Parameters
tvPointer to the timeval structure
Returns
Corresponding time in microseconds or zero if tv is NULL

Referenced by Time::fromTimeval().

static u_int64_t fromTimeval ( const struct timeval &  tv)
inlinestatic

Convert time in a timeval struct to microseconds

Parameters
tvReference of the timeval structure
Returns
Corresponding time in microseconds

References Time::fromTimeval().

static bool isLeap ( unsigned int  year)
inlinestatic

Check if an year is a leap one

Parameters
yearThe year to check
Returns
True if the given year is a leap one
u_int64_t msec ( ) const
inline

Get time in milliseconds

Returns
Time in milliseconds since the Epoch
static u_int64_t msecNow ( )
static

Get the current system time in milliseconds

Returns
Time in milliseconds since the Epoch
static u_int64_t now ( )
static

Get the current system time in microseconds

Returns
Time in microseconds since the Epoch
operator u_int64_t ( ) const
inline

Conversion to microseconds operator

Time& operator+= ( int64_t  delta)
inline

Offsetting operator.

Time& operator-= ( int64_t  delta)
inline

Offsetting operator.

Time& operator= ( u_int64_t  usec)
inline

Assignment operator.

References Time::usec().

u_int32_t sec ( ) const
inline

Get time in seconds

Returns
Time in seconds since the Epoch

Referenced by Time::toNtp().

static u_int32_t secNow ( )
static

Get the current system time in seconds

Returns
Time in seconds since the Epoch
static int timeZone ( u_int32_t  when = secNow())
static

Retrieve the difference between local time and UTC in seconds east of UTC

Parameters
whenUNIX time for which to compute timezone, affects daylight saving
Returns
Difference between local time and UTC in seconds
static bool toDateTime ( unsigned int  epochTimeSec,
int &  year,
unsigned int &  month,
unsigned int &  day,
unsigned int &  hour,
unsigned int &  minute,
unsigned int &  sec,
unsigned int *  wDay = 0 
)
static

Split a given EPOCH time into its date/time components

Parameters
epochTimeSecEPOCH time in seconds
yearThe year component of the date
monthThe month component of the date (1 to 12)
dayThe day component of the date (1 to 31)
hourThe hour component of the time (0 to 23)
minuteThe minute component of the time (0 to 59)
secThe seconds component of the time (0 to 59)
wDayThe day of the week (optional)
Returns
True on succes, false if conversion failed
static unsigned int toEpoch ( int  year,
unsigned int  month,
unsigned int  day,
unsigned int  hour,
unsigned int  minute,
unsigned int  sec,
int  offset = 0 
)
static

Build EPOCH time from date/time components

Parameters
yearThe year component of the date. Must be greater then 1969
monthThe month component of the date (1 to 12)
dayThe day component of the date (1 to 31)
hourThe hour component of the time (0 to 23). The hour can be 24 if minute and sec are 0
minuteThe minute component of the time (0 to 59)
secThe seconds component of the time (0 to 59)
offsetOptional number of seconds to be added/substracted to/from result. It can't exceed the number of seconds in a day
Returns
EPOCH time in seconds, -1 on failure
static uint64_t toEpoch ( const char *  buf,
unsigned int  len,
int  frac = 0 
)
static

Decode string to EPOCH time Decode yyyy-mm-dd{T|t}hh:mm:ss[.SEC-FRAC]{{Z|z}|{+/-hh:mm}} Date seconds may contain leap seconds (value 60). These are ignored (second will be used as 59)

Parameters
bufBuffer to parse
lenBuffer length. 0 to detect
fracHandle second fractions. 0: none, negative: microseconds, positive: milliseconds
Returns
EPOCH time in (milli|micro)seconds, -1 on failure
static uint32_t toNtp ( uint32_t  sec,
uint32_t *  over = 0,
bool  rfc2030 = true 
)
static

Convert system time to 32bit NTP (seconds since 1900)

Parameters
secTime in seconds
overOptional destination for overflow value. Must be reset before calling this method
rfc2030Use time extension as specified in RFC 2030 Section 3
Returns
NTP time value in seconds

Referenced by Time::toNtp().

uint32_t toNtp ( uint32_t *  over = 0,
bool  rfc2030 = true 
)
inline

Convert this time to 32bit NTP (seconds since 1900)

Parameters
overOptional destination for overflow value. Must be reset before calling this method
rfc2030Use time extension as specified in RFC 2030 Section 3
Returns
NTP time value in seconds

References Time::sec(), and Time::toNtp().

static unsigned int toString ( char *  buf,
uint64_t  time,
int  frac = 0 
)
static

Convert EPOCH time to a string representation. Does not add a NUL char at end Minimal representation is yyyy-mm-ddThh:mm:ssZ Destination buffer length must be at least 20 if no fractions are going to be filled For milliseconds fractions extra 4 bytes must be available in buffer For microseconds fractions extra 7 bytes must be available in buffer

Parameters
bufBuffer to be filled
timeSystem time in microseconds to convert
fracAdd second fractions. 0: none, negative: microseconds, positive: milliseconds
Returns
The number of chars written in buffer. 0 on failure

Referenced by Time::appendTo().

void toTimeval ( struct timeval *  tv) const
inline

Fill in a timeval struct from a value in microseconds

Parameters
tvPointer to the timeval structure
static void toTimeval ( struct timeval *  tv,
u_int64_t  usec 
)
static

Fill in a timeval struct from a value in microseconds

Parameters
tvPointer to the timeval structure
usecTime to convert to timeval
u_int64_t usec ( ) const
inline

Get time in microseconds

Returns
Time in microseconds since the Epoch

Referenced by Time::operator=().


The documentation for this class was generated from the following file: