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...
|
| 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 tm * | toTm (bool utc, struct tm *tmp, int *usecs=0) const |
| Set values in a struct tm from a UTime.
|
|
struct tm * | toTm (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().
|
|
UTime & | operator= (const UTime &u) |
|
UTime & | operator= (long long u) |
|
UTime | operator+ (long long u) const |
|
UTime | operator- (long long u) const |
|
long long | operator- (const UTime &u) const |
|
UTime & | operator+= (long long u) |
|
UTime & | operator-= (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 |
|
UTime & | setFormat (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 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.
|
|
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.
std::string nidas::util::UTime::format |
( |
bool | utc, |
|
|
const std::string & | fmt ) const |
Format a UTime into a string.
- Parameters
-
utc | if true, use UTC timezone, otherwise the TZ environment variable. |
fmt | a 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().
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
-
nparsed | number of characters parsed. |
- Exceptions
-
References month(), USECS_PER_SEC, and UTime().
Referenced by set(), and set().