nidas v1.2.3
Public Member Functions | Static Public Member Functions | Static Public Attributes | Static Protected Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
nidas::util::UTime Class Reference

A class for parsing, formatting and doing operations on time, based on Unix time conventions, where leap seconds are ignored, so that there are always 60 seconds in a minute, 3600 seconds in an hour and 86400 seconds in a day. More...

#include <UTime.h>

Public Member Functions

 UTime ()
 No-arg constructor initializes to current time, with isUTC() true.
 
 UTime (long long t)
 Constructor.
 
bool isZero () const
 Return true if this UTime is equivalent to UTime::ZERO.
 
bool isMin () const
 Return true if this UTime is equivalent to UTime::MIN.
 
bool isMax () const
 Return true if this UTime is equivalent to UTime::MAX.
 
bool isSet () const
 Return true if UTime is neither MIN nor MAX.
 
 UTime (time_t t)
 Constructor.
 
 UTime (double t)
 Constructor.
 
 UTime (bool utc, const struct tm *tmp, int usecs=0)
 Constructor from a struct tm.
 
 UTime (bool utc, int year, int mon, int day, int hour, int min, double sec)
 Constructor.
 
 UTime (bool utc, int year, int yday, int hour, int min, double sec)
 Constructor.
 
 UTime (const UTime &u)
 
void setFromSecs (time_t t)
 
struct tmtoTm (bool utc, struct tm *tmp, int *usecs=0) const
 Set values in a struct tm from a UTime.
 
struct tmtoTm (struct tm *tmp, int *usecs=0) const
 Set values in a struct tm from a UTime, using the isUTC() attribute.
 
bool isUTC () const
 Format this UTime relative to UTC, or based on the TZ environment variable.
 
void setUTC (bool val)
 Format this UTime relative to UTC, or the local timezone?
 
void set (bool utc, const std::string &string, int *nparsed=0)
 Updates the value of a UTime by doing a parse(utc,string,nparsed).
 
void set (bool utc, const std::string &string, const std::string &format, int *nparsed=0)
 Updates the value of a UTime by doing a parse(utc,string,format,nparsed).
 
std::string format (bool utc, const std::string &fmt) const
 Format a UTime into a string.
 
std::string format (const std::string &fmt) const
 Format a UTime into a string.
 
std::string format (bool utc) const
 Format a UTime into a string using the format returned by getFormat().
 
std::string format () const
 Format a UTime into a string using the format returned by getFormat().
 
UTimeoperator= (const UTime &u)
 
UTimeoperator= (long long u)
 
UTime operator+ (long long u) const
 
UTime operator- (long long u) const
 
long long operator- (const UTime &u) const
 
UTimeoperator+= (long long u)
 
UTimeoperator-= (long long u)
 
bool operator< (const UTime &u) const
 
bool operator<= (const UTime &u) const
 
bool operator> (const UTime &u) const
 
bool operator>= (const UTime &u) const
 
bool operator== (const UTime &u) const
 
bool operator!= (const UTime &u) const
 
UTime earlier (long long y) const
 
long long toUsecs () const
 
double toDoubleSecs () const
 
time_t toSecs () const
 
UTimesetFormat (const std::string &val)
 Set the format used when converting this UTime to a string with format(utc), or format(), or on a ostream.
 
const std::string & getFormat () const
 Get the format used when converting this UTime to a string with format(utc), or format(), or on a ostream.
 
struct tm tm (bool utc) const
 

Static Public Member Functions

static long long fromTm (bool utc, const struct tm *tmp, int usecs=0)
 Return number of non-leap micro-seconds since Jan 1970 00:00 UTC computed from time fields in a struct tm.
 
static UTime parse (bool utc, const std::string &string, int *nparsed=0)
 Parse a character string into a UTime, using these formats until success:
 
static UTime parse (bool utc, const std::string &string, const std::string &format, int *nparsed=0)
 Parse a character string into a UTime.
 
static int month (std::string monstr)
 
static void setDefaultFormat (const std::string &val)
 Static method to set the default output format.
 
static const std::string & getDefaultFormat ()
 
static void setTZ (const std::string &val)
 Set the TZ environment variable to val.
 
static std::string getTZ ()
 
static long long pmod (long long x, long long y)
 Positive modulus: if x > 0, returns x % y else y + (x % y) Useful for time calculation on negative times, for example: pmod((long long)ut,USECS_PER_DAY) gives microseconds since 00:00 of day whether date is negative or positive.
 

Static Public Attributes

static const UTime MIN
 
static const UTime MAX
 
static const UTime ZERO
 

Static Protected Member Functions

static long long fromSecs (time_t x)
 Convert a unsigned value in seconds to a value in the units of UTime.
 
static long long fromSecs (double x)
 Convert a double value in seconds to a value in the units of UTime.
 
static double toDoubleSecs (long long x)
 
static time_t toSecs (long long x)
 

Private Member Functions

bool checkParse (bool utc, const std::string &str, const std::string &fmt, int *ncharp, bool throwx=false)
 Parse into this UTime same as parse(), returning true on success.
 

Private Attributes

long long _utime
 non-leap micro-seconds since 1970 Jan 1 00:00 UTC.
 
std::string _fmt
 strftime string to use when formatting this UTime.
 
bool _utc
 Whether to format this UTime relative to UTC.
 

Static Private Attributes

static std::string _defaultFormat
 
static Mutex _fmtMutex
 

Friends

template<typename charT >
std::basic_ostream< charT, std::char_traits< charT > > & operator<< (std::basic_ostream< charT, std::char_traits< charT > > &os, const UTime &x)
 

Detailed Description

A class for parsing, formatting and doing operations on time, based on Unix time conventions, where leap seconds are ignored, so that there are always 60 seconds in a minute, 3600 seconds in an hour and 86400 seconds in a day.

Time values are typically assigned by a computer with an NTP controlled clock, and that time is converted to a Unix time as the number of non-leap seconds since Jan 1970 00:00 GMT. Conversion back to human readable time uses the same no-leap-second convention. Time values around the time that a system's NTP clock are being adjusted for a leap second will be indeterminate by up to a second, depending on how the Unix clock on that system was adjusted.

UTime supports output to the std::basic_ostream<charT> template class, which includes output to std::ostream, which is std::basic_ostream<char>. The general support for basic_ostream<charT> is only useful to support wide character output, which is not likely, but it works.

There is no explicit support for an "unset" or "null" UTime value. The default constructor initializes from the current system time; there is no way to construct an invalid or null time. Instead, historical practice has been to use LONG_LONG_MIN or sometimes just (time_t)0 to indicate an unset UTime. Therefore the static constants MIN, MAX, and ZERO exist to formalize this practice. Use Utime(UTime::ZERO) to construct a single time for which isZero() returns true. Use MIN when the intention is to use the earliest possible UTime value, and similarly for MAX. The meaning of (time_t)0 should be made explicit by replacing it with MIN, MAX, or ZERO. Instead of comparing a UTime to LONG_LONG_MIN or LONG_LONG_MAX, call isMin() or isMax() or isSet(). Typically, MIN is used as the default to indicate an unset start time while MAX is used for a default end time, so calling isSet() works for either start or end times, returning true if the UTime is neither MIN nor MAX.

The stored microseconds since the Posix epoch is a signed long long, so 0 represents the epoch but not the earliest possible UTime. The minimum possible UTime is represented by the negative value LONG_LONG_MIN. ZERO and isZero() are available in case an application really does want the default to be the epoch or otherwise needs a convenient UTime constant for the Posix epoch, but generally it should be avoided since the intention is less clear, either zero as epoch or zero as unset. In particular, ZERO.isSet() returns true.

Constructor & Destructor Documentation

◆ UTime() [1/8]

nidas::util::UTime::UTime ( )

No-arg constructor initializes to current time, with isUTC() true.

References _utime, NSECS_PER_USEC, and USECS_PER_SEC.

Referenced by checkParse(), earlier(), operator+(), operator-(), and parse().

◆ UTime() [2/8]

nidas::util::UTime::UTime ( long long t)
inline

Constructor.

isUTC() will be set to true.

Parameters
tNon-leap microseconds since Jan 1, 1970 00:00 UTC

◆ UTime() [3/8]

nidas::util::UTime::UTime ( time_t t)
inline

Constructor.

isUTC() will be set to true.

Parameters
tNon-leap seconds since Jan 1, 1970 00:00 UTC

◆ UTime() [4/8]

nidas::util::UTime::UTime ( double t)
inline

Constructor.

isUTC() will be set to true.

Parameters
tNon-leap seconds since Jan 1, 1970 00:00 UTC

◆ UTime() [5/8]

nidas::util::UTime::UTime ( bool utc,
const struct tm * tmp,
int usecs = 0 )

Constructor from a struct tm.

See the fromTm() static method.

◆ UTime() [6/8]

nidas::util::UTime::UTime ( bool utc,
int year,
int mon,
int day,
int hour,
int min,
double sec )

Constructor.

mon is 1-12, day is 1-31. Note that mon differs from the definition of tm_mon in struct tm which is in the range 0-11.

References _utime, fromSecs(), fromTm(), and tm().

◆ UTime() [7/8]

nidas::util::UTime::UTime ( bool utc,
int year,
int yday,
int hour,
int min,
double sec )

Constructor.

yday is day of year, 1-366. Note that yday differs from the definition of tm_yday in struct tm which is in the range 0-365.

References _utime, fromSecs(), fromTm(), and tm().

◆ UTime() [8/8]

nidas::util::UTime::UTime ( const UTime & u)
inline

Member Function Documentation

◆ checkParse()

bool nidas::util::UTime::checkParse ( bool utc,
const std::string & str,
const std::string & fmt,
int * ncharp,
bool throwx = false )
private

Parse into this UTime same as parse(), returning true on success.

If parsing fails and throwx is true, then throw an exception. If parsing fails and throwx is false, then return false.

References tm(), UTime(), and VLOG.

◆ earlier()

UTime nidas::util::UTime::earlier ( long long y) const

References _utime, and UTime().

Referenced by format(), and toTm().

◆ format() [1/4]

string nidas::util::UTime::format ( ) const

Format a UTime into a string using the format returned by getFormat().

isUTC() attribute detemines whether it is formatted in UTC or based on the TZ environment variable.

References _utc, format(), and getFormat().

Referenced by format(), format(), format(), and set().

◆ format() [2/4]

string nidas::util::UTime::format ( bool utc) const

Format a UTime into a string using the format returned by getFormat().

Parameters
utcif true, use UTC timezone, otherwise the TZ environment variable.

References format(), and getFormat().

◆ format() [3/4]

std::string nidas::util::UTime::format ( bool utc,
const std::string & fmt ) const

Format a UTime into a string.

Parameters
utcif true, use UTC timezone, otherwise the TZ environment variable.
fmta time format in the form of strftime. All the % format descriptors of strftime are available. In addition one can use "%nf" to print fractional seconds, where n is the precision, a digit from 1 to 9. n defaults to 3, providing millisecond precision, if not specified. For example: ut.format(true,"time is: %Y %m %d %H:%M:%S.%2f");

The "%s" format descriptor will print the number of non-leap seconds since 1970 Jan 01 00:00 UTC. This is the same number returned by toSecs(). Note that s will generate the same value as strftime in the following code: struct tm tm; char timestr[12]; time_t tval = mytime; localtime_r(&tval,&tm); strftime(timestr,sizeof(timestr),"%s",&tm);

Using gmtime_r to fill in struct tm and then call strftime with a s generates the wrong value if the local timezone is other than GMT, since strftime with a s assumes the struct tm is in the local timezone: gmtime_r(&tval,&tm); strftime(timestr,sizeof(timestr),"%s",&tm); // wrong

References _utime, earlier(), tm(), toUsecs(), and USECS_PER_SEC.

Referenced by nidas::dynld::raf::SyncRecordSource::advanceRecord(), nidas::dynld::raf::A2D_Serial::checkCkSum(), PacketReader::checkPacket(), nidas::dynld::StatisticsCruncher::computeStats(), nidas::core::SampleTracer::format_time(), format_time(), nidas::core::SampleTracer::format_time(), ftime(), nidas::dynld::isff::NCAR_TRH::handleRawRH(), nidas::dynld::isff::NCAR_TRH::handleRawT(), DataStats::jsonReport(), main(), nidas::dynld::SampleInputStream::nextSample(), operator<<(), nidas::core::Polynomial::parseFields(), nidas::dynld::RawSampleService::printClock(), nidas::core::SampleArchiver::printStatus(), nidas::dynld::raf::SyncRecordGenerator::printStatus(), nidas::dynld::RawSampleService::printStatus(), nidas::dynld::iss::WICORSensor::process(), nidas::dynld::raf::DSMArincSensor::process(), nidas::dynld::isff::CSI_CRX_Binary::process(), nidas::dynld::GPS_NMEA_Serial::process(), nidas::dynld::GPS_Novatel_Serial::process(), nidas::dynld::raf::UDPArincSensor::process(), nidas::dynld::raf::UHSAS_Serial::process(), nidas::dynld::raf::TwoD32_USB::processImage(), nidas::core::VariableConverter::readCalFile(), nidas::core::CalFile::readCFInclude(), nidas::core::NearestResampler::receive(), nidas::core::NearestResamplerAtRate::receive(), nidas::core::SampleAverager::receive(), DumpClient::receive(), DataPrep::run(), MergeVerifier::run(), NidsMerge::run(), StatsProcess::run(), nidas::core::DSMEngineStat::run(), nidas::core::NidasApp::setOutputClipping(), tformat(), time_format(), nidas::dynld::isff::WisardMote::unpackAccumSec(), nidas::core::MessageStreamScanner::warnBackwardsStepTimeTag(), nidas::core::MessageStreamScanner::warnNonIncrTimeTag(), and nidas::core::Socket::write().

◆ format() [4/4]

string nidas::util::UTime::format ( const std::string & fmt) const

Format a UTime into a string.

isUTC() attribute detemines whether it is formatted in UTC or based on the TZ environment variable.

Parameters
fmtas in format(utc,fmt).

References _utc, and format().

◆ fromSecs() [1/2]

static long long nidas::util::UTime::fromSecs ( double x)
inlinestaticprotected

Convert a double value in seconds to a value in the units of UTime.

References USECS_PER_SEC.

◆ fromSecs() [2/2]

static long long nidas::util::UTime::fromSecs ( time_t x)
inlinestaticprotected

Convert a unsigned value in seconds to a value in the units of UTime.

References USECS_PER_SEC.

Referenced by setFromSecs(), UTime(), and UTime().

◆ fromTm()

long long nidas::util::UTime::fromTm ( bool utc,
const struct tm * tmp,
int usecs = 0 )
static

Return number of non-leap micro-seconds since Jan 1970 00:00 UTC computed from time fields in a struct tm.

See "man mktime". If the value of tmp->tm_yday is greater than or equal to 0, and tmp->tm_mon is less than 0 or tmp->tm_mday is less than 1, then the result is calculated using tm_yday. Otherwise tm_mon and tm_mday are used.

References tm(), and USECS_PER_SEC.

Referenced by UTime(), and UTime().

◆ getDefaultFormat()

const string & nidas::util::UTime::getDefaultFormat ( )
static

References _defaultFormat, and _fmtMutex.

Referenced by getFormat().

◆ getFormat()

const std::string & nidas::util::UTime::getFormat ( ) const
inline

Get the format used when converting this UTime to a string with format(utc), or format(), or on a ostream.

If the user hasn't set the format, the default value is getDefaultFormat().

References _fmt, and getDefaultFormat().

Referenced by format(), and format().

◆ getTZ()

string nidas::util::UTime::getTZ ( )
static

◆ isMax()

bool nidas::util::UTime::isMax ( ) const

Return true if this UTime is equivalent to UTime::MAX.

References _utime.

Referenced by StatsProcess::run().

◆ isMin()

bool nidas::util::UTime::isMin ( ) const

◆ isSet()

bool nidas::util::UTime::isSet ( ) const

◆ isUTC()

bool nidas::util::UTime::isUTC ( ) const
inline

Format this UTime relative to UTC, or based on the TZ environment variable.

References _utc.

◆ isZero()

bool nidas::util::UTime::isZero ( ) const

Return true if this UTime is equivalent to UTime::ZERO.

References _utime.

Referenced by DataStats::receive().

◆ month()

int nidas::util::UTime::month ( std::string monstr)
static

References tm().

Referenced by parse().

◆ operator!=()

bool nidas::util::UTime::operator!= ( const UTime & u) const
inline

References _utime.

◆ operator+()

UTime nidas::util::UTime::operator+ ( long long u) const
inline

References _utime, and UTime().

◆ operator+=()

UTime & nidas::util::UTime::operator+= ( long long u)
inline

References _utime.

◆ operator-() [1/2]

long long nidas::util::UTime::operator- ( const UTime & u) const
inline

References _utime.

◆ operator-() [2/2]

UTime nidas::util::UTime::operator- ( long long u) const
inline

References _utime, and UTime().

◆ operator-=()

UTime & nidas::util::UTime::operator-= ( long long u)
inline

References _utime.

◆ operator<()

bool nidas::util::UTime::operator< ( const UTime & u) const
inline

References _utime.

◆ operator<=()

bool nidas::util::UTime::operator<= ( const UTime & u) const
inline

References _utime.

◆ operator=() [1/2]

UTime & nidas::util::UTime::operator= ( const UTime & u)
inline

References _fmt, and _utime.

◆ operator=() [2/2]

UTime & nidas::util::UTime::operator= ( long long u)
inline

References _utime.

◆ operator==()

bool nidas::util::UTime::operator== ( const UTime & u) const
inline

References _utime.

◆ operator>()

bool nidas::util::UTime::operator> ( const UTime & u) const
inline

References _utime.

◆ operator>=()

bool nidas::util::UTime::operator>= ( const UTime & u) const
inline

References _utime.

◆ parse() [1/2]

UTime nidas::util::UTime::parse ( bool utc,
const std::string & string,
const std::string & format,
int * nparsed = 0 )
static

Parse a character string into a UTime.

Parameters
formata time format in the form of strptime. All the % format descriptors of strptime are available. In addition one can use "%nf" to parse fractional seconds, where n is the number of digits in the fraction to parse. n defaults to 3 if not specified. If the "%s" descriptor is used, then the utc parameter is silently forced to false, since strptime does that conversion in local time.
nparsednumber of characters parsed. Example: UTime ut = UTime::parse(true,timestr,"%Y %m %d %H:%M:%S.%2f");
Exceptions
ParseException

◆ parse() [2/2]

UTime nidas::util::UTime::parse ( bool utc,
const std::string & string,
int * nparsed = 0 )
static

Parse a character string into a UTime, using these formats until success:

[CC]YY [cmon|mon] day h:m[:s.f] h,m and s are one or two digits [CC]YY [cmon|mon] day hhmmss[.f] hh, mm and ss are two digits [CC]YY [cmon|mon] day s.f YYYY-mm-dd[THH:MM[:SS[.f]]] ISO format YYYY-mm-dd[ HH:MM[:SS[.f]]] ISO format with space separator

"cmon" is a character month or abbreviation. "mon" is a numeric month (1-12). "day" is day of month, 1-31. "h" or "hh" are in the range 0-23. "f" is the fractional seconds, one or more digits. The last format, "s.f" is the number of non-leap seconds since 1970 Jan 1 00:00 GMT. For example, 1262304000.0 is 2010 Jan 1 00:00 GMT. Note: one can also use a "%s" descriptor in the format argument to parse(false,str,format,nparsed) to do the same conversion. If all parsing fails, throw ParseException.

Parameters
nparsednumber of characters parsed.
Exceptions
ParseException

References month(), USECS_PER_SEC, and UTime().

Referenced by set(), and set().

◆ pmod()

long long nidas::util::UTime::pmod ( long long x,
long long y )
static

Positive modulus: if x > 0, returns x % y else y + (x % y) Useful for time calculation on negative times, for example: pmod((long long)ut,USECS_PER_DAY) gives microseconds since 00:00 of day whether date is negative or positive.

◆ set() [1/2]

void nidas::util::UTime::set ( bool utc,
const std::string & string,
const std::string & format,
int * nparsed = 0 )

Updates the value of a UTime by doing a parse(utc,string,format,nparsed).

Exceptions
ParseException

References format(), and parse().

◆ set() [2/2]

void nidas::util::UTime::set ( bool utc,
const std::string & string,
int * nparsed = 0 )

Updates the value of a UTime by doing a parse(utc,string,nparsed).

Exceptions
ParseException

References parse().

◆ setDefaultFormat()

void nidas::util::UTime::setDefaultFormat ( const std::string & val)
static

Static method to set the default output format.

If not set by the user, the default default (sic) is "%c".

References _defaultFormat, and _fmtMutex.

◆ setFormat()

UTime & nidas::util::UTime::setFormat ( const std::string & val)
inline

Set the format used when converting this UTime to a string with format(utc), or format(), or on a ostream.

References _fmt.

Referenced by main().

◆ setFromSecs()

void nidas::util::UTime::setFromSecs ( time_t t)
inline

References _utime, and fromSecs().

◆ setTZ()

void nidas::util::UTime::setTZ ( const std::string & val)
static

Set the TZ environment variable to val.

If val is an empty string the TZ is removed from the environment.

References nidas::util::Process::getEnvVar(), and nidas::util::Process::setEnvVar().

◆ setUTC()

void nidas::util::UTime::setUTC ( bool val)
inline

Format this UTime relative to UTC, or the local timezone?

References _utc.

Referenced by nidas::util::Logger::msg().

◆ tm()

struct tm nidas::util::UTime::tm ( bool utc) const

◆ toDoubleSecs() [1/2]

double nidas::util::UTime::toDoubleSecs ( ) const
inline

References _utime, and USECS_PER_SEC.

Referenced by ARLIngest::arl_ingest_one().

◆ toDoubleSecs() [2/2]

static double nidas::util::UTime::toDoubleSecs ( long long x)
inlinestaticprotected

References USECS_PER_SEC.

◆ toSecs() [1/2]

time_t nidas::util::UTime::toSecs ( ) const
inline

References _utime, and USECS_PER_SEC.

◆ toSecs() [2/2]

static time_t nidas::util::UTime::toSecs ( long long x)
inlinestaticprotected

References USECS_PER_SEC.

◆ toTm() [1/2]

struct tm * nidas::util::UTime::toTm ( bool utc,
struct tm * tmp,
int * usecs = 0 ) const

Set values in a struct tm from a UTime.

References _utime, earlier(), toUsecs(), and USECS_PER_SEC.

Referenced by toTm().

◆ toTm() [2/2]

struct tm * nidas::util::UTime::toTm ( struct tm * tmp,
int * usecs = 0 ) const

Set values in a struct tm from a UTime, using the isUTC() attribute.

References _utc, and toTm().

◆ toUsecs()

long long nidas::util::UTime::toUsecs ( ) const
inline

Friends And Related Symbol Documentation

◆ operator<<

template<typename charT >
std::basic_ostream< charT, std::char_traits< charT > > & operator<< ( std::basic_ostream< charT, std::char_traits< charT > > & os,
const UTime & x )
friend

Member Data Documentation

◆ _defaultFormat

string nidas::util::UTime::_defaultFormat
staticprivate

◆ _fmt

std::string nidas::util::UTime::_fmt
private

strftime string to use when formatting this UTime.

Referenced by getFormat(), operator=(), and setFormat().

◆ _fmtMutex

Mutex nidas::util::UTime::_fmtMutex
staticprivate

◆ _utc

bool nidas::util::UTime::_utc
private

Whether to format this UTime relative to UTC.

Can be overridden with format calls that provide a utc argument.

Referenced by format(), format(), isUTC(), setUTC(), and toTm().

◆ _utime

long long nidas::util::UTime::_utime
private

◆ MAX

const UTime nidas::util::UTime::MAX
static

◆ MIN

const UTime nidas::util::UTime::MIN
static

◆ ZERO

const UTime nidas::util::UTime::ZERO
static

Referenced by main().


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