My Project
 All Classes Files Functions Variables Enumerations Enumerator Friends Macros Pages
gcp::util::TimeVal Class Reference

#include <TimeVal.h>

Public Types

enum  clockid_t { CLOCK_REALTIME, CLOCK_HIGHRES }
 

Public Member Functions

 TimeVal ()
 
 TimeVal (unsigned int seconds, unsigned int microSeconds, unsigned int nanoSeconds)
 
 TimeVal (unsigned int seconds, unsigned int nanoSeconds)
 
 TimeVal (double seconds)
 
 TimeVal (std::string time_string, std::string format, struct tm &refdate, double &reftime)
 
 TimeVal (std::string time_string, std::string format)
 
 TimeVal (const struct timeval &tVal)
 
 TimeVal (const struct timespec &timeSpec)
 
 TimeVal (const TimeVal &tVal)
 
 TimeVal (TimeVal &tVal)
 
void setToCurrentTime (clockid_t clock=CLOCK_REALTIME)
 
void setMjd (unsigned int days, unsigned int seconds, unsigned int nanoSeconds)
 
void setMjd (unsigned int days, unsigned int milliSeconds)
 
void setMjd (double mjd)
 
void setTime (unsigned int seconds, unsigned int microSeconds, unsigned int nanoSeconds)
 
void setTime (unsigned int seconds, unsigned int nanoSeconds)
 
void setTime (const struct timespec &timeSpec)
 
void setTime (const struct timeval &tVal)
 
void setTime (double seconds)
 
void setTimeInMilliSeconds (unsigned int milliSeconds)
 
void incrementSeconds (double seconds)
 
void incrementMilliSeconds (unsigned milliSeconds)
 
void incrementNanoSeconds (unsigned nanoSeconds)
 
void reset ()
 
struct timeval * timeVal ()
 
struct timespec * timeSpec ()
 
const TimeVal operator+ (const TimeVal &tVal)
 
const TimeVal operator- (const TimeVal &tVal)
 
void operator= (const TimeVal &tVal)
 
void operator= (TimeVal &tVal)
 
bool operator>= (TimeVal &tVal)
 
bool operator>= (const TimeVal &tVal)
 
bool operator< (TimeVal &tVal)
 
bool operator< (const TimeVal &tVal)
 
void operator-= (const TimeVal &tVal)
 
double getTimeInMjdDays ()
 
double getTimeInSeconds () const
 
unsigned int getTimeInMicroSeconds ()
 
unsigned int getTimeInMilliSeconds ()
 
unsigned int getTimeInNanoSeconds ()
 
double getFractionalTimeInSeconds ()
 
unsigned int getSeconds ()
 
unsigned int getMilliSeconds (bool round=false)
 
unsigned int getMicroSeconds ()
 
unsigned int getNanoSeconds ()
 
unsigned int getMjdDays ()
 
unsigned int getMjdSeconds ()
 
unsigned int getMjdMilliSeconds (bool round=false)
 
unsigned int getMjdNanoSeconds ()
 
double getMjd ()
 
unsigned int getMjdId (unsigned nanoSecondInterval)
 
double getElapsedTimeInSeconds ()
 
double getElapsedTimeInMicroSeconds ()
 
unsigned int getElapsedTimeInNanoSeconds ()
 
std::string getUtcString ()
 
std::string dateString ()
 
void setDateTime (std::string time_string, std::string format, struct tm &refdate, double &reftime)
 
void setSeconds (unsigned int seconds)
 
void setMilliSeconds (unsigned int milliSeconds)
 
void setMicroSeconds (unsigned int microSeconds)
 
void setNanoSeconds (unsigned int nanoSeconds)
 

Public Attributes

int ErrorCounter_
 

Friends

std::ostream & operator<< (std::ostream &os, TimeVal &tVal)
 

Detailed Description

A class for managing timeval/timespec structs.

These are defined in sys/time.h as:

struct timeval { int tv_sec; int tv_usec; } and

struct timespec { int tv_sec; int tv_nsec; }

struct timeval (with microsecond resolution) is used by functions like select() as a timeout specifier, while struct timespec (with nanosecond resolution) is used by functions like clock_gettime(). Many system functions which require a time specification user one or the other in an irritatingly inconsistent fashion.

This class is intended as a meta-time specifier, which stores a time with nanosecond granularity, and can present either a timeval or timespec face to the world, via the timeVal() and timeSpec() methods below.

Note that functions like select() can modify their timeval arguments, while most uses of timespec are as a static container. The ***Elapsed() methods below reflect the mutability of a timeval struct when used as a timeout argument to select(), which decrements the timeval struct to reflect elapsed time.

Constructor & Destructor Documentation

TimeVal::TimeVal ( )

Constructors with no initialization.

....................................................................... Constructor with no initialization.

TimeVal::TimeVal ( unsigned int  seconds,
unsigned int  microSeconds,
unsigned int  nanoSeconds 
)

Constructors with initialization.

We make this one have three arguments, otherwise there's no way of distinguishing a constructor with seconds and microseconds from a constructor with seconds and nanoseconds.

Although the internal time representation is always kept either in seconds and microseconds (struct timeval) or seconds and nanoseconds (struct timespec), the time set in the constructor will be the addition of all three arguments.

....................................................................... Constructor with time initialization.

NB: The time will be the sum of the seconds, microseconds and nanoseconds passed.

TimeVal::TimeVal ( unsigned int  seconds,
unsigned int  nanoSeconds 
)

Constructor with seconds and nanoseconds.

....................................................................... Constructor with time initialization.

NB: The time will be the sum of the seconds, microseconds and nanoseconds passed.

TimeVal::TimeVal ( double  seconds)

Set the time as seconds

....................................................................... Constructor with initialization via fractional seconds

gcp::util::TimeVal::TimeVal ( std::string  time_string,
std::string  format,
struct tm &  refdate,
double &  reftime 
)

Initialize from a string and format string Refdate and reftime are cached values of the start of today so that mktime only needs to be called once per day to convert from Gregorian date to seconds since 1/1/1970, since this process is slow

gcp::util::TimeVal::TimeVal ( std::string  time_string,
std::string  format 
)

This version re-calculates the date and time on every call Used for timestamps that are logged less frequently (eg. once a second) to avoid having to keep track of reference date/time

TimeVal::TimeVal ( const struct timeval &  tVal)

Initialize from a timeval struct.

....................................................................... Constructor with initialization via timeval struct.

TimeVal::TimeVal ( const struct timespec &  timeSpec)

Initialize from a timespec struct.

....................................................................... Constructor with initialization via timespec struct.

TimeVal::TimeVal ( const TimeVal tVal)

....................................................................... Constructor with no initialization.

Member Function Documentation

double TimeVal::getElapsedTimeInMicroSeconds ( )

Return the time in our timeVal struct as integer microseconds.

....................................................................... Return the time in our timeVal struct as integer microseconds.

unsigned long TimeVal::getElapsedTimeInNanoSeconds ( )

Return the time in our timeVal struct as integer nanoseconds.

....................................................................... Return the time in our timeVal struct as integer nanoseconds.

double TimeVal::getElapsedTimeInSeconds ( )

Return the time elapsed as fractional seconds. Note that the ***Elapsed() methods only apply to our timeval struct, since a timespec struct is not used as a countdown timer.

....................................................................... Return the time elapsed as fractional seconds. Note that the ***Elapsed() methods only apply to our timeval struct, since a timespec struct is not used as a countdown timer.

double TimeVal::getFractionalTimeInSeconds ( )

Return just the fractional seconds, as a double

....................................................................... Return just the fractional seconds, as a double

unsigned long TimeVal::getMicroSeconds ( )

Return just the integer micro seconds

....................................................................... Return just the integer microseconds

unsigned long TimeVal::getMilliSeconds ( bool  round = false)

Return just the integer milli seconds

....................................................................... Return just the integer milliseconds. If round == true, round up to the

double TimeVal::getMjd ( )

Return the complete mjd day as a double

....................................................................... Return the mjd day number as a double

unsigned long TimeVal::getMjdDays ( )

Return the mjd day number corresponding to this time.

....................................................................... Return the mjd day number corresponding to this time.

unsigned TimeVal::getMjdId ( unsigned  nanoSecondInterval)

Get a unique identifier based on the MJD.

....................................................................... Get a unique identifier constructed as the integral number of seconds since MJD0, rounded to the nearest second.

unsigned long TimeVal::getMjdMilliSeconds ( bool  round = false)

Return the mjd milliseconds corresponding to this time.

....................................................................... Return the number of milliseconds into this MJD day.

unsigned long TimeVal::getMjdNanoSeconds ( )

Return the mjd nanoseconds corresponding to this time.

....................................................................... Return the mjd nanoseconds corresponding to this time.

unsigned long TimeVal::getMjdSeconds ( )

Return the mjd seconds corresponding to this time.

....................................................................... Return the mjd seconds corresponding to this time.

unsigned long TimeVal::getNanoSeconds ( )

Return just the integer nano seconds

....................................................................... Return just the integer nanoseconds

unsigned long TimeVal::getSeconds ( )

Return just the integer seconds

....................................................................... Return just the integer seconds

unsigned long TimeVal::getTimeInMicroSeconds ( )

Return the total time in microseconds.

....................................................................... Return the total time in microseconds.

unsigned long TimeVal::getTimeInMilliSeconds ( )

Return the total time as integer milliseconds

....................................................................... Return the total time as integer nanoseconds.

double TimeVal::getTimeInMjdDays ( )

Return the total time in MJD days.

....................................................................... Return the total time in MJD days.

unsigned long TimeVal::getTimeInNanoSeconds ( )

Return the total time as integer nanoseconds.

....................................................................... Return the total time as integer nanoseconds.

double TimeVal::getTimeInSeconds ( ) const

Return the time in our timeval struct as fractional seconds.

....................................................................... Return the total time in seconds.

std::string TimeVal::getUtcString ( )

....................................................................... Return a human-readable string representation of the UTC time managed by this object

void TimeVal::incrementMilliSeconds ( unsigned  milliSeconds)

Increment the time by milliseconds.

....................................................................... Increment the time, by milliseconds

void TimeVal::incrementNanoSeconds ( unsigned  nanoSeconds)

Increment the time by nanoseconds.

....................................................................... Increment the time, by nanoSeconds.

void TimeVal::incrementSeconds ( double  seconds)

Increment the time by fractional seconds.

....................................................................... Increment the time, by fractional seconds.

const TimeVal TimeVal::operator+ ( const TimeVal tVal)

Add two TimeVal objects

....................................................................... Addition operator for TimeVal. Note that addition only applies to the unchangeable members of this class. Ie, in the returned object, the timeVal_ struct will contain the addition of the preset times, not the addition of timeVal_ structs, which can be different.

const TimeVal TimeVal::operator- ( const TimeVal tVal)

Subtract two TimeVal objects

....................................................................... Subtraction operator for TimeVal. Note that while TimeVal objects should only contain positive time values, the subtraction of the nanoseconds portion can be negative.

void TimeVal::operator-= ( const TimeVal tVal)

....................................................................... Subtraction operator for TimeVal. Note that while TimeVal objects should only contain positive time values, the subtraction of the nanoseconds portion can be negative.

bool TimeVal::operator< ( TimeVal tVal)

....................................................................... Comparison operators

bool TimeVal::operator< ( const TimeVal tVal)

....................................................................... Comparison operators

void TimeVal::operator= ( const TimeVal tVal)

Assignment operators

....................................................................... Assignment operators

bool TimeVal::operator>= ( TimeVal tVal)

....................................................................... Comparison operators

bool TimeVal::operator>= ( const TimeVal tVal)

....................................................................... Comparison operators

void TimeVal::reset ( )

Reset our internal timeval struct to stored values.

....................................................................... Reset internal structs to stored values.

void TimeVal::setDateTime ( std::string  time_string,
std::string  format,
struct tm &  refdate,
double &  reftime 
)

Set from date and time string

void TimeVal::setMicroSeconds ( unsigned int  microSeconds)

Set the microseconds in this struct. Does not increment the seconds portion of the time kept by this class.

Exceptions
Errorif microSeconds is greater than 1 second.

....................................................................... Set the number of microseconds.

void TimeVal::setMilliSeconds ( unsigned int  milliSeconds)

Set the milliseconds in this struct. Does not increment the seconds portion of the time kept by this class.

Exceptions
Errorif milliSeconds is greater than 1 second.

....................................................................... Set the number of milliseconds.

void TimeVal::setMjd ( unsigned int  days,
unsigned int  seconds,
unsigned int  nanoSeconds 
)

Set the time, as an MJD.

....................................................................... Set the time, as an MJD.

void TimeVal::setMjd ( unsigned int  days,
unsigned int  milliSeconds 
)

Set the time, as an MJD.

....................................................................... Set the time, as an MJD.

void TimeVal::setMjd ( double  mjd)

Set the time, as a double MJD.

....................................................................... Set the time, as a double MJD.

void TimeVal::setNanoSeconds ( unsigned int  nanoSeconds)

Set the nanoseconds in this struct. Does not increment the seconds portion of the time kept by this class.

Exceptions
Errorif nanoSeconds is greater than 1 second.

....................................................................... Set the number of nanoseconds.

void TimeVal::setSeconds ( unsigned int  seconds)

Set the seconds in this object.

....................................................................... Set just the number of seconds.

void TimeVal::setTime ( unsigned int  seconds,
unsigned int  microSeconds,
unsigned int  nanoSeconds 
)

Set the time. For the MJD representation, passed times will be interpreted as time since the Epoch.

....................................................................... Set the time.

void TimeVal::setTime ( unsigned int  seconds,
unsigned int  nanoSeconds 
)

Set the time.

....................................................................... Set the time.

void TimeVal::setTime ( const struct timespec &  timeSpec)

Set the time with a timespec struct.

....................................................................... Set the time with a timespec struct.

void TimeVal::setTime ( const struct timeval &  timeval)

Set the time with a timeval struct.

....................................................................... Set the time with a timeval struct.

void TimeVal::setTime ( double  seconds)

Set the time as fractional seconds

....................................................................... Set the time.

void TimeVal::setTimeInMilliSeconds ( unsigned int  milliSeconds)

....................................................................... Set the time in milliseconds

void TimeVal::setToCurrentTime ( clockid_t  clock = CLOCK_REALTIME)

Fill this structure with the current time from the specified clock. Supported clocks include at least:

CLOCK_REALTIME – the realtime clock for the system, relative to an Epoch (specified where?)

CLOCK_HIGHRES – the high-resolution non-adjustable clock, relative to some arbitrary time in the past.

....................................................................... Fill this structure with the current time.

struct timespec * TimeVal::timeSpec ( )

Return a pointer to our internal timespec struct.

....................................................................... Return a timespec pointer suitable for passing to clock_gettime(), etc.

struct timeval * TimeVal::timeVal ( )

Return a pointer to our internal timeval struct.

....................................................................... Return a timeval pointer suitable for passing to select()

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  os,
TimeVal tVal 
)
friend

Allows cout << timeVal


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