32 #ifndef NIDAS_UTIL_UTIME_H
33 #define NIDAS_UTIL_UTIME_H
35 #include <sys/types.h>
60 #define UTIME_BASIC_STREAM_IO
62 namespace nidas {
namespace util {
108 UTime(
bool utc,
const struct tm* tmp,
int usecs = 0);
115 UTime(
bool utc,
int year,
int mon,
int day,
int hour,
int min,
double sec);
122 UTime(
bool utc,
int year,
int yday,
int hour,
int min,
double sec);
135 struct tm*
toTm(
bool utc,
struct tm* tmp,
int* usecs = 0)
const;
140 struct tm*
toTm(
struct tm* tmp,
int* usecs = 0)
const;
150 static long long fromTm(
bool utc,
const struct tm* tmp,
int usecs = 0);
184 static UTime parse(
bool utc,
const std::string&
string,
int* nparsed=0)
199 static
UTime parse(
bool utc, const std::
string&
string,
200 const std::
string&
format,
int* nparsed=0)
201 throw(ParseException);
206 void set(
bool utc,const std::
string&
string,
int* nparsed=0)
207 throw(ParseException);
212 void set(
bool utc,const std::
string&
string,const std::
string& format,
int* nparsed=0)
213 throw(ParseException);
240 std::
string format(
bool utc,const std::
string& fmt) const;
247 std::
string format(const std::
string& fmt) const;
253 std::
string format(
bool utc) const;
260 std::
string format() const;
297 static int month(std::string monstr);
352 static void setTZ(
const std::string& val);
354 static std::string
getTZ();
356 struct tm tm(bool utc) const;
358 #ifdef UTIME_BASIC_STREAM_IO
359 template<
typename charT>
friend
360 std::basic_ostream<charT, std::char_traits<charT> >&
operator <<
361 (std::basic_ostream<charT, std::char_traits<charT> >& os,
376 static long long pmod(
long long x,
long long y);
393 double xf = floor(x);
417 checkParse(
bool utc,
const std::string& str,
const std::string& fmt,
418 int *ncharp,
bool throwx=
false);
451 #ifdef UTIME_BASIC_STREAM_IO
452 template<
typename charT>
457 #ifdef UTIME_BASIC_STREAM_IO
462 std::basic_ostream<charT,std::char_traits<charT> >& (*_f)(std::basic_ostream<charT,std::char_traits<charT> >&,
const std::string&);
464 std::ostream& (*_f)(std::ostream&,
const std::string&);
473 #ifdef UTIME_BASIC_STREAM_IO
475 std::basic_ostream<charT,std::char_traits<charT> >&,
const std::string&),
const std::string& fmt):
_fmt(fmt),
_f(f)
480 std::ostream&,
const std::string&),
const std::string& fmt):
_fmt(fmt),
_f(f)
489 #ifdef UTIME_BASIC_STREAM_IO
490 template<
typename charTx>
491 friend std::basic_ostream<charTx, std::char_traits<charTx> >&
operator <<
492 (std::basic_ostream<charTx, std::char_traits<charTx> >& os,
const UTime_stream_manip<charTx>& m);
494 friend std::ostream&
operator<<(std::ostream& os,
499 #ifdef UTIME_BASIC_STREAM_IO
502 template<
typename charT>
503 std::basic_ostream<charT, std::char_traits<charT> >&
operator <<
504 (std::basic_ostream<charT, std::char_traits<charT> >& os,
const UTime& x)
506 return os << x.format();
508 template<
typename charT>
509 std::basic_ostream<charT, std::char_traits<charT> >&
operator <<
512 return m._f(os,m._fmt);
521 template<
typename charT>
522 std::basic_ostream<charT, std::char_traits<charT> >&
523 setOstreamDefaultFormat(std::basic_ostream<charT, std::char_traits<charT> >& os,
524 const std::string& val)
534 template<
typename charT>
535 std::basic_ostream<charT, std::char_traits<charT> >&
536 setOstreamTZ(std::basic_ostream<charT, std::char_traits<charT> >& os,
537 const std::string& val)
547 template<
typename charT>
556 template<
typename charT>
564 std::ostream&
operator<<(std::ostream& os,
const UTime &x)
566 return os << x.
format(
false);
570 const UTime_stream_manip& m)
572 return m._f(os,m._fmt);
577 std::ostream& setOstreamDefaultFormat(std::ostream& os,
const std::string& val)
583 std::ostream& setOstreamTZ(std::ostream& os,
const std::string& val)
592 return UTime_stream_manip(&setOstreamDefaultFormat,val);
595 UTime_stream_manip
setTZ(
const std::string& val)
597 return UTime_stream_manip(&setOstreamTZ,val);
608 ::clock_gettime(CLOCK_REALTIME,&ts);
620 return ((t / delta) + 1) * delta;
628 inline long long timeFloor(
long long t,
long long delta) {
629 return (t / delta) * delta;
bool isUTC() const
Format this UTime relative to UTC, or based on the TZ environment variable.
Definition: UTime.h:155
UTime_stream_manip(std::basic_ostream< charT, std::char_traits< charT > > &(*f)(std::basic_ostream< charT, std::char_traits< charT > > &, const std::string &), const std::string &fmt)
Constructor of manipulator.
Definition: UTime.h:474
friend std::basic_ostream< charTx, std::char_traits< charTx > > & operator<<(std::basic_ostream< charTx, std::char_traits< charTx > > &os, const UTime_stream_manip< charTx > &m)
<< operator of this manipulator on an ostream.
static std::string getTZ()
Definition: UTime.cc:571
static void setTZ(const std::string &val)
Set the TZ environment variable to val.
Definition: UTime.cc:561
static time_t toSecs(long long x)
Definition: UTime.h:404
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.
Definition: UTime.h:76
#define NSECS_PER_USEC
Definition: types.h:111
std::string _fmt
strftime string to use when formatting this UTime.
Definition: UTime.h:428
static double toDoubleSecs(long long x)
Definition: UTime.h:398
static std::string _defaultFormat
Definition: UTime.h:436
bool operator<=(const UTime &u) const
Definition: UTime.h:285
UTime_stream_manip< charT > setTZ(const std::string &val)
Function to set the UTime timezone on an ostream.
Definition: UTime.h:557
long long getSystemTime()
Return the current unix system time, in microseconds since Jan 1, 1970, 00:00 GMT.
Definition: UTime.h:606
static int month(std::string monstr)
Definition: UTime.cc:581
bool sleepUntil(unsigned int periodMsec, unsigned int offsetMsec=0)
Utility function, sleeps until the next even period + offset.
Definition: UTime.cc:690
bool operator==(const UTime &u) const
Definition: UTime.h:291
void set(bool utc, const std::string &string, int *nparsed=0)
Updates the value of a UTime by doing a parse(utc,string,nparsed).
Definition: UTime.cc:307
long long timeFloor(long long t, long long delta)
Return largest time that is an integral multiple of delta, that isn't greater than argument t...
Definition: UTime.h:628
UTime(time_t t)
Constructor.
Definition: UTime.h:96
static Mutex _fmtMutex
Definition: UTime.h:438
UTime()
No-arg constructor initializes to current time, with isUTC() true.
Definition: UTime.cc:46
friend std::basic_ostream< charT, std::char_traits< charT > > & operator<<(std::basic_ostream< charT, std::char_traits< charT > > &os, const UTime &x)
Definition: UTime.h:504
static long long fromSecs(time_t x)
Convert a unsigned value in seconds to a value in the units of UTime.
Definition: UTime.h:383
bool operator!=(const UTime &u) const
Definition: UTime.h:293
LogMessage & operator<<(LogMessage &logmsg, LogMessage &(*op)(LogMessage &))
Template to call LogMessage manipulators like endlog when streamed to a LogMessage.
Definition: Logger.h:1062
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.
Definition: UTime.h:334
void setFromSecs(time_t t)
Definition: UTime.h:130
time_t toSecs() const
Definition: UTime.h:314
std::basic_ostream< charT, std::char_traits< charT > > &(* _f)(std::basic_ostream< charT, std::char_traits< charT > > &, const std::string &)
Pointer to function that does a manipulation on a ostream with a string argument. ...
Definition: UTime.h:462
bool operator<(const UTime &u) const
Definition: UTime.h:283
static const std::string & getDefaultFormat()
Definition: UTime.cc:554
UTime & operator-=(long long u)
Definition: UTime.h:281
static long long fromSecs(double x)
Convert a double value in seconds to a value in the units of UTime.
Definition: UTime.h:391
static UTime parse(bool utc, const std::string &string, int *nparsed=0)
Parse a character string into a UTime, using these formats until success:
Definition: UTime.cc:180
std::string format() const
Format a UTime into a string using the format returned by getFormat().
Definition: UTime.cc:427
double toDoubleSecs() const
Definition: UTime.h:307
bool operator>(const UTime &u) const
Definition: UTime.h:287
struct tm tm(bool utc) const
long long toUsecs() const
Definition: UTime.h:302
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 ti...
Definition: UTime.cc:613
static unsigned int periodMsec
Definition: sing.cc:64
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.
Definition: UTime.h:323
UTime earlier(long long y) const
Definition: UTime.cc:605
UTime operator-(long long u) const
Definition: UTime.h:275
UTime(double t)
Constructor.
Definition: UTime.h:103
struct tm * toTm(bool utc, struct tm *tmp, int *usecs=0) const
Set values in a struct tm from a UTime.
Definition: UTime.cc:163
UTime operator+(long long u) const
Definition: UTime.h:273
UTime(const UTime &u)
Definition: UTime.h:124
UTime & operator+=(long long u)
Definition: UTime.h:279
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.
Definition: UTime.cc:331
bool _utc
Whether to format this UTime relative to UTC.
Definition: UTime.h:434
long long _utime
non-leap micro-seconds since 1970 Jan 1 00:00 UTC.
Definition: UTime.h:423
class for changing output format of UTime on ostream, in a way like the standard stream manipulator c...
Definition: UTime.h:454
#define USECS_PER_SEC
Definition: ublox.cc:59
long long timeCeiling(long long t, long long delta)
Return smallest time that is an integral multiple of delta, that isn't less than or equal to argument...
Definition: UTime.h:619
bool operator>=(const UTime &u) const
Definition: UTime.h:289
UTime_stream_manip< charT > setDefaultFormat(const std::string &val)
Function to set the default UTime output format on an ostream.
Definition: UTime.h:548
void setUTC(bool val)
Format this UTime relative to UTC, or the local timezone?
Definition: UTime.h:160
UTime(long long t)
Constructor.
Definition: UTime.h:89
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 struc...
Definition: UTime.cc:111
A C++ wrapper for a POSIX mutex.
Definition: ThreadSupport.h:154
static void setDefaultFormat(const std::string &val)
Static method to set the default output format.
Definition: UTime.cc:548
LogMessage & format(const char *fmt,...)
Definition: Logger.cc:561
std::string _fmt
Definition: UTime.h:456
UTime & operator=(long long u)
Definition: UTime.h:271
Definition: ParseException.h:36