nidas v1.2.3
UTime.h
Go to the documentation of this file.
1// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4; -*-
2// vim: set shiftwidth=4 softtabstop=4 expandtab:
3/*
4 ********************************************************************
5 ** NIDAS: NCAR In-situ Data Acquistion Software
6 **
7 ** 2006, Copyright University Corporation for Atmospheric Research
8 **
9 ** This program is free software; you can redistribute it and/or modify
10 ** it under the terms of the GNU General Public License as published by
11 ** the Free Software Foundation; either version 2 of the License, or
12 ** (at your option) any later version.
13 **
14 ** This program is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ** GNU General Public License for more details.
18 **
19 ** The LICENSE.txt file accompanying this software contains
20 ** a copy of the GNU General Public License. If it is not found,
21 ** write to the Free Software Foundation, Inc.,
22 ** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 **
24 ********************************************************************
25*/
26/*
27
28 C++ class for time handling.
29
30*/
31
32#ifndef NIDAS_UTIL_UTIME_H
33#define NIDAS_UTIL_UTIME_H
34
35#include <sys/types.h>
36#include <sys/time.h>
37#include <ctime>
38#include <iostream>
39#include <cctype>
40#include <cmath>
41#include <climits>
42
43#include <locale>
44#include <string>
45
46#include "ThreadSupport.h"
47#include "ParseException.h"
48#include "time_constants.h"
49#include "IOException.h"
50
51
52namespace nidas { namespace util {
53
95class UTime {
96public:
97
101 UTime( );
102
108 UTime(long long t): _utime(t),_fmt(),_utc(true) {}
109
110 static const UTime MIN;
111 static const UTime MAX;
112 static const UTime ZERO;
113
117 bool isZero() const;
118
122 bool isMin() const;
123
127 bool isMax() const;
128
132 bool isSet() const;
133
139 UTime(time_t t): _utime(fromSecs(t)),_fmt(),_utc(true) { }
140
146 UTime(double t): _utime(fromSecs(t)),_fmt(),_utc(true) { }
147
151 UTime(bool utc,const struct tm* tmp,int usecs = 0);
152
158 UTime(bool utc, int year,int mon, int day,int hour, int min, double sec);
159
165 UTime(bool utc, int year,int yday,int hour, int min, double sec);
166
167 UTime(const UTime& u) :
168 _utime(u._utime),
169 _fmt(u._fmt),
170 _utc(u._utc)
171 { }
172
173 void setFromSecs(time_t t) { _utime = fromSecs(t); }
174
178 struct tm* toTm(bool utc,struct tm* tmp, int* usecs = 0) const;
179
183 struct tm* toTm(struct tm* tmp, int* usecs = 0) const;
184
193 static long long fromTm(bool utc,const struct tm* tmp, int usecs = 0);
194
198 bool isUTC() const { return _utc; }
199
203 void setUTC(bool val) { _utc = val; }
204
229 static UTime parse(bool utc, const std::string& string, int* nparsed=0);
230
245 static UTime parse(bool utc, const std::string& string,
246 const std::string& format, int* nparsed=0);
247
253 void set(bool utc,const std::string& string,int* nparsed=0);
254
260 void set(bool utc,const std::string& string, const std::string& format,
261 int* nparsed=0);
262
288 std::string format(bool utc,const std::string& fmt) const;
289
295 std::string format(const std::string& fmt) const;
296
301 std::string format(bool utc) const;
302
308 std::string format() const;
309
311 {
312 if (this != &u) {
313 _utime = u._utime;
314 _fmt = u._fmt;
315 }
316 return *this;
317 }
318
319 UTime& operator=(long long u) { _utime = u; return *this; }
320
321 UTime operator+ (long long u) const { return UTime(_utime + u); }
322
323 UTime operator- (long long u) const { return UTime(_utime - u); }
324
325 long long operator- (const UTime& u) const { return _utime - u._utime; }
326
327 UTime& operator+=(long long u) { _utime += u; return *this; }
328
329 UTime& operator-=(long long u) { _utime -= u; return *this; }
330
331 bool operator<(const UTime& u) const { return _utime < u._utime; }
332
333 bool operator<=(const UTime& u) const { return _utime <= u._utime; }
334
335 bool operator>(const UTime& u) const { return _utime > u._utime; }
336
337 bool operator>=(const UTime& u) const { return _utime >= u._utime; }
338
339 bool operator==(const UTime& u) const { return _utime == u._utime; }
340
341 bool operator!=(const UTime& u) const { return _utime != u._utime; }
342
343 UTime earlier(long long y) const;
344
345 static int month(std::string monstr);
346
347 long long toUsecs() const
348 {
349 return _utime;
350 }
351
352 double toDoubleSecs() const
353 {
354 // should work for positive and negative.
355 return (time_t)(_utime/USECS_PER_SEC) +
356 (double)(_utime % USECS_PER_SEC) / USECS_PER_SEC;
357 }
358
359 time_t toSecs() const
360 {
361 return (_utime + USECS_PER_SEC / 2) / USECS_PER_SEC;
362 }
363
368 UTime& setFormat(const std::string& val)
369 {
370 _fmt = val;
371 return *this;
372 }
373
379 const std::string& getFormat() const
380 {
381 if (_fmt.length() > 0) return _fmt;
382 return getDefaultFormat();
383 }
384
389 static void setDefaultFormat(const std::string& val);
390
391 static const std::string& getDefaultFormat();
392
397 static void setTZ(const std::string& val);
398
399 static std::string getTZ();
400
401 struct tm tm(bool utc) const;
402
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,
406 const UTime& x);
407
417 static long long pmod(long long x, long long y);
418
419protected:
420
424 static long long fromSecs(time_t x)
425 {
426 return (long long)x * USECS_PER_SEC;
427 }
428
432 static long long fromSecs(double x)
433 {
434 double xf = floor(x);
435 return (long long)xf * USECS_PER_SEC +
436 (int)rint((x-xf) * USECS_PER_SEC) ;
437 }
438
439 static double toDoubleSecs(long long x)
440 {
441 // should work for positive and negative.
442 return (time_t)(x/USECS_PER_SEC) + (double)(x % USECS_PER_SEC) / USECS_PER_SEC;
443 }
444
445 static time_t toSecs(long long x)
446 {
447 return x / USECS_PER_SEC;
448 }
449
450private:
451
457 bool
458 checkParse(bool utc, const std::string& str, const std::string& fmt,
459 int *ncharp, bool throwx=false);
460
464 long long _utime;
465
469 std::string _fmt;
470
475 bool _utc;
476
477 static std::string _defaultFormat;
478
480
481};
482
492template<typename charT>
494
495 std::string _fmt;
500 std::basic_ostream<charT,std::char_traits<charT> >& (*_f)(std::basic_ostream<charT,std::char_traits<charT> >&, const std::string&);
501
502public:
508 UTime_stream_manip(std::basic_ostream<charT,std::char_traits<charT> >& (*f)(
509 std::basic_ostream<charT,std::char_traits<charT> >&, const std::string&), const std::string& fmt): _fmt(fmt),_f(f)
510 {
511 }
512
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);
520};
521
522// format a UTime on an output stream.
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)
526{
527 return os << x.format();
528}
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)
532{
533 return m._f(os,m._fmt);
534}
535
536// anonymous namespace for private, internal functions
537namespace {
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)
546{
548 return os;
549}
550
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)
559{
560 UTime::setTZ(val);
561 return os;
562}
563} // end of anonymous namespace
564
568template<typename charT>
569UTime_stream_manip<charT> setDefaultFormat(const std::string& val)
570{
571 return UTime_stream_manip<charT>(&setOstreamDefaultFormat,val);
572}
573
577template<typename charT>
578UTime_stream_manip<charT> setTZ(const std::string& val)
579{
580 return UTime_stream_manip<charT>(&setOstreamTZ,val);
581}
582
587inline long long getSystemTime() {
588 struct timespec ts;
589 ::clock_gettime(CLOCK_REALTIME,&ts);
590 return (long long)ts.tv_sec * USECS_PER_SEC + ts.tv_nsec / NSECS_PER_USEC;
591}
592
600inline long long timeCeiling(long long t,long long delta) {
601 return ((t / delta) + 1) * delta;
602}
603
609inline long long timeFloor(long long t,long long delta) {
610 return (t / delta) * delta;
611}
618extern bool sleepUntil(unsigned int periodMsec,unsigned int offsetMsec=0);
619
620}} // namespace nidas namespace util
621
622#endif
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