32#ifndef NIDAS_UTIL_UTIME_H
33#define NIDAS_UTIL_UTIME_H
52namespace nidas {
namespace util {
151 UTime(
bool utc,
const struct tm* tmp,
int usecs = 0);
158 UTime(
bool utc,
int year,
int mon,
int day,
int hour,
int min,
double sec);
165 UTime(
bool utc,
int year,
int yday,
int hour,
int min,
double sec);
178 struct tm*
toTm(
bool utc,
struct tm* tmp,
int* usecs = 0)
const;
183 struct tm*
toTm(
struct tm* tmp,
int* usecs = 0)
const;
193 static long long fromTm(
bool utc,
const struct tm* tmp,
int usecs = 0);
229 static UTime parse(
bool utc,
const std::string&
string,
int* nparsed=0);
245 static UTime parse(
bool utc,
const std::string&
string,
246 const std::string&
format,
int* nparsed=0);
253 void set(
bool utc,
const std::string&
string,
int* nparsed=0);
260 void set(
bool utc,
const std::string&
string,
const std::string&
format,
288 std::string
format(
bool utc,
const std::string& fmt)
const;
295 std::string
format(
const std::string& fmt)
const;
301 std::string
format(
bool utc)
const;
308 std::string
format()
const;
345 static int month(std::string monstr);
397 static void setTZ(
const std::string& val);
399 static std::string
getTZ();
403 template<
typename charT>
friend
404 std::basic_ostream<charT, std::char_traits<charT> >&
operator <<
405 (std::basic_ostream<charT, std::char_traits<charT> >& os,
417 static long long pmod(
long long x,
long long y);
434 double xf = floor(x);
458 checkParse(
bool utc,
const std::string& str,
const std::string& fmt,
459 int *ncharp,
bool throwx=
false);
492template<
typename charT>
500 std::basic_ostream<charT,std::char_traits<charT> >& (*_f)(std::basic_ostream<charT,std::char_traits<charT> >&,
const std::string&);
509 std::basic_ostream<charT,std::char_traits<charT> >&,
const std::string&),
const std::string& fmt):
_fmt(fmt),
_f(f)
517 template<
typename charTx>
518 friend std::basic_ostream<charTx, std::char_traits<charTx> >&
operator <<
519 (std::basic_ostream<charTx, std::char_traits<charTx> >& os,
const UTime_stream_manip<charTx>& m);
523template<
typename charT>
524std::basic_ostream<charT, std::char_traits<charT> >&
operator <<
525 (std::basic_ostream<charT, std::char_traits<charT> >& os,
const UTime& x)
527 return os << x.format();
529template<
typename charT>
530std::basic_ostream<charT, std::char_traits<charT> >&
operator <<
531 (std::basic_ostream<charT, std::char_traits<charT> >& os,
const UTime_stream_manip<charT>& m)
533 return m._f(os,m._fmt);
542template<
typename charT>
543std::basic_ostream<charT, std::char_traits<charT> >&
544 setOstreamDefaultFormat(std::basic_ostream<charT, std::char_traits<charT> >& os,
545 const std::string& val)
555template<
typename charT>
556std::basic_ostream<charT, std::char_traits<charT> >&
557 setOstreamTZ(std::basic_ostream<charT, std::char_traits<charT> >& os,
558 const std::string& val)
568template<
typename charT>
571 return UTime_stream_manip<charT>(&setOstreamDefaultFormat,val);
577template<
typename charT>
578UTime_stream_manip<charT>
setTZ(
const std::string& val)
580 return UTime_stream_manip<charT>(&setOstreamTZ,val);
589 ::clock_gettime(CLOCK_REALTIME,&ts);
601 return ((t / delta) + 1) * delta;
609inline long long timeFloor(
long long t,
long long delta) {
610 return (t / delta) * delta;
A C++ wrapper for a POSIX mutex.
Definition ThreadSupport.h:161
class for changing output format of UTime on ostream, in a way like the standard stream manipulator c...
Definition UTime.h:493
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:508
std::string _fmt
Definition UTime.h:495
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:500
A class for parsing, formatting and doing operations on time, based on Unix time conventions,...
Definition UTime.h:95
long long toUsecs() const
Definition UTime.h:347
const std::string & getFormat() const
Get the format used when converting this UTime to a string with format(utc), or format(),...
Definition UTime.h:379
static long long fromSecs(time_t x)
Convert a unsigned value in seconds to a value in the units of UTime.
Definition UTime.h:424
static const std::string & getDefaultFormat()
Definition UTime.cc:583
bool operator>=(const UTime &u) const
Definition UTime.h:337
static std::string _defaultFormat
Definition UTime.h:477
UTime(long long t)
Constructor.
Definition UTime.h:108
std::string format(bool utc, const std::string &fmt) const
Format a UTime into a string.
Definition UTime.cc:462
bool operator<=(const UTime &u) const
Definition UTime.h:333
double toDoubleSecs() const
Definition UTime.h:352
UTime & operator=(const UTime &u)
Definition UTime.h:310
struct tm * toTm(bool utc, struct tm *tmp, int *usecs=0) const
Set values in a struct tm from a UTime.
Definition UTime.cc:193
static int month(std::string monstr)
Definition UTime.cc:610
std::string _fmt
strftime string to use when formatting this UTime.
Definition UTime.h:469
UTime(const UTime &u)
Definition UTime.h:167
UTime(time_t t)
Constructor.
Definition UTime.h:139
bool isMax() const
Return true if this UTime is equivalent to UTime::MAX.
Definition UTime.cc:130
bool _utc
Whether to format this UTime relative to UTC.
Definition UTime.h:475
bool operator>(const UTime &u) const
Definition UTime.h:335
static std::string getTZ()
Definition UTime.cc:600
struct tm tm(bool utc) const
bool isMin() const
Return true if this UTime is equivalent to UTime::MIN.
Definition UTime.cc:125
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:360
bool isUTC() const
Format this UTime relative to UTC, or based on the TZ environment variable.
Definition UTime.h:198
UTime & operator-=(long long u)
Definition UTime.h:329
static double toDoubleSecs(long long x)
Definition UTime.h:439
UTime(double t)
Constructor.
Definition UTime.h:146
static const UTime MAX
Definition UTime.h:111
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:141
time_t toSecs() const
Definition UTime.h:359
static void setDefaultFormat(const std::string &val)
Static method to set the default output format.
Definition UTime.cc:577
static long long fromSecs(double x)
Convert a double value in seconds to a value in the units of UTime.
Definition UTime.h:432
static const UTime ZERO
Definition UTime.h:112
static time_t toSecs(long long x)
Definition UTime.h:445
void setUTC(bool val)
Format this UTime relative to UTC, or the local timezone?
Definition UTime.h:203
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:642
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:210
bool isSet() const
Return true if UTime is neither MIN nor MAX.
Definition UTime.cc:135
long long _utime
non-leap micro-seconds since 1970 Jan 1 00:00 UTC.
Definition UTime.h:464
bool isZero() const
Return true if this UTime is equivalent to UTime::ZERO.
Definition UTime.cc:120
bool operator!=(const UTime &u) const
Definition UTime.h:341
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:336
UTime & operator+=(long long u)
Definition UTime.h:327
bool operator==(const UTime &u) const
Definition UTime.h:339
UTime earlier(long long y) const
Definition UTime.cc:634
static Mutex _fmtMutex
Definition UTime.h:479
UTime operator-(long long u) const
Definition UTime.h:323
static const UTime MIN
Definition UTime.h:110
UTime operator+(long long u) const
Definition UTime.h:321
void setFromSecs(time_t t)
Definition UTime.h:173
UTime()
No-arg constructor initializes to current time, with isUTC() true.
Definition UTime.cc:49
bool operator<(const UTime &u) const
Definition UTime.h:331
UTime & operator=(long long u)
Definition UTime.h:319
UTime & setFormat(const std::string &val)
Set the format used when converting this UTime to a string with format(utc), or format(),...
Definition UTime.h:368
static void setTZ(const std::string &val)
Set the TZ environment variable to val.
Definition UTime.cc:590
UTime_stream_manip< charT > setTZ(const std::string &val)
Function to set the UTime timezone on an ostream.
Definition UTime.h:578
UTime_stream_manip< charT > setDefaultFormat(const std::string &val)
Function to set the default UTime output format on an ostream.
Definition UTime.h:569
long long getSystemTime()
Return the current unix system time, in microseconds since Jan 1, 1970, 00:00 GMT.
Definition UTime.h:587
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:609
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:600
bool sleepUntil(unsigned int periodMsec, unsigned int offsetMsec)
Utility function, sleeps until the next even period + offset.
Definition UTime.cc:648
Root namespace for the NCAR In-Situ Data Acquisition Software.
Definition A2DConverter.h:31
static unsigned int periodMsec
Definition sing.cc:64
#define NSECS_PER_USEC
Definition types.h:111
#define USECS_PER_SEC
Definition ublox.cc:59