MIRA
Time.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_TIME_H_
48 #define _MIRA_TIME_H_
49 
50 #ifndef Q_MOC_RUN
51 #include <boost/date_time/posix_time/posix_time.hpp>
52 #include <boost/date_time/local_time/local_time.hpp>
53 #include <boost/date_time/c_local_time_adjustor.hpp>
54 #endif
55 
56 #include <platform/Types.h>
59 
60 #include <utils/IsCheapToCopy.h>
61 
62 namespace mira {
63 
65 
70 typedef boost::gregorian::date Date;
71 typedef boost::gregorian::date_duration DateDuration;
72 
73 using boost::date_time::Jan;
74 using boost::date_time::Feb;
75 using boost::date_time::Mar;
76 using boost::date_time::Apr;
77 using boost::date_time::May;
78 using boost::date_time::Jun;
79 using boost::date_time::Jul;
80 using boost::date_time::Aug;
81 using boost::date_time::Sep;
82 using boost::date_time::Oct;
83 using boost::date_time::Nov;
84 using boost::date_time::Dec;
85 
86 using boost::date_time::Monday;
87 using boost::date_time::Tuesday;
88 using boost::date_time::Wednesday;
89 using boost::date_time::Thursday;
90 using boost::date_time::Friday;
91 using boost::date_time::Saturday;
92 using boost::date_time::Sunday;
93 
106 class Duration : public boost::posix_time::time_duration
107 {
108 protected:
109 
110  typedef boost::posix_time::time_duration Base;
111 
112 public:
113 
119  static Duration nanoseconds(int64 v) {
120  Duration d;
121 #if defined(BOOST_DATE_TIME_HAS_NANOSECONDS)
122  d.ticks_ = v;
123 #else
124  d.ticks_ = v / 1000;
125 #endif
126  return d;
127  }
128 
134  static Duration microseconds(int64 v) {
135  Duration d;
136 #if defined(BOOST_DATE_TIME_HAS_NANOSECONDS)
137  d.ticks_ = v * 1000;
138 #else
139  d.ticks_ = v;
140 #endif
141  return d;
142  }
143 
149  static Duration milliseconds(int64 v) {
150  Duration d;
151 #if defined(BOOST_DATE_TIME_HAS_NANOSECONDS)
152  d.ticks_ = v * 1000000;
153 #else
154  d.ticks_ = v * 1000;
155 #endif
156  return d;
157  }
158 
172  static Duration seconds(int32 v) { return boost::posix_time::seconds(v); }
173 
179  static Duration minutes(int32 v) { return boost::posix_time::minutes(v); }
180 
186  static Duration hours(int32 v) { return boost::posix_time::hours(v); }
187 
188 public:
189  Duration(hour_type hour, min_type min, sec_type sec,
190  fractional_seconds_type fs=0) :
191  Base(hour, min, sec, fs)
192  {}
193 
195  Duration(const Base& d) :
196  Base(d)
197  {}
198 
200  Duration(const boost::posix_time::special_values sv) :
201  Base(sv)
202  {}
203 
204 #if !defined(DATE_TIME_NO_DEFAULT_CONSTRUCTOR)
205  Duration()
207  {}
208 #endif // DATE_TIME_NO_DEFAULT_CONSTRUCTOR
209 
210 public:
211 
213 
215  template<typename Reflector>
216  void reflectRead(Reflector& r)
217  {
218  int64 t = totalMilliseconds();
219  r.delegate(t);
220  }
221 
222  template<typename Reflector>
223  void reflectWrite(Reflector& r)
224  {
225  int64 t;
226  r.delegate(t);
227  *this = Duration::milliseconds(t);
228  }
229 
230 public:
231 
236  {
237  return Duration(boost::posix_time::neg_infin);
238  }
243  {
244  return Duration(boost::posix_time::pos_infin);
245  }
249  static Duration invalid()
250  {
251  return Duration(boost::posix_time::not_a_date_time);
252  }
253 
257  bool isValid() const
258  {
259  return !this->is_not_a_date_time();
260  }
264  bool isInfinity() const
265  {
266  return this->is_pos_infinity() || this->is_neg_infinity();
267  }
268 
270  hour_type hours() const
271  {
272  return Base::hours();
273  }
275  min_type minutes() const
276  {
277  return Base::minutes();
278  }
280  sec_type seconds() const
281  {
282  return Base::seconds();
283  }
285  tick_type milliseconds() const
286  {
287  return totalMilliseconds() % 1000;
288  }
290  tick_type microseconds() const
291  {
292  return totalMicroseconds() % 1000;
293  }
295  tick_type nanoseconds() const
296  {
297  return totalNanoseconds() % 1000;
298  }
300  sec_type totalSeconds() const
301  {
302  return Base::total_seconds();
303  }
305  tick_type totalMilliseconds() const
306  {
307  return Base::total_milliseconds();
308  }
310  tick_type totalMicroseconds() const
311  {
312  return Base::total_microseconds();
313  }
315  tick_type totalNanoseconds() const
316  {
317  return Base::total_nanoseconds();
318  }
319 
320 public:
321 
325  {
326  return Base::operator -();
327  }
328  Duration operator-(const Duration& d) const
329  {
330  return Base::operator-(d);
331  }
332  Duration operator+(const Duration& d) const
333  {
334  return Base::operator+(d);
335  }
336  Duration operator/(int divisor) const
337  {
338  assert(divisor != 0);
339  return Base::operator/(divisor);
340  }
341  Duration operator/(float divisor) const
342  {
343  assert(divisor != 0.0f);
344  Duration d;
345  d.ticks_ = (int64)(ticks_.as_number() / divisor);
346  return d;
347  }
348  float operator/(Duration other) const
349  {
350  return (float)totalNanoseconds() / other.totalNanoseconds();
351  }
353  {
354  return Base::operator-=(d);
355  }
357  {
358  return Base::operator+=(d);
359  }
361  Duration operator/=(int divisor)
362  {
363  assert(divisor != 0);
364  return Base::operator/=(divisor);
365  }
366  Duration operator/=(float divisor)
367  {
368  assert(divisor != 0.0f);
369  ticks_ = (int64)(ticks_.as_number() / divisor);
370  return *this;
371  }
373  Duration operator*(int rhs) const
374  {
375  return Base::operator*(rhs);
376  }
377  Duration operator*(float rhs) const
378  {
379  Duration d;
380  d.ticks_ = (int64)(ticks_.as_number() * rhs);
381  return d;
382  }
384  {
385  return Base::operator*=(rhs);
386  }
387  Duration operator*=(float rhs)
388  {
389  ticks_ = (int64)(ticks_.as_number() * rhs);
390  return *this;
391  }
393 };
394 
396 
401 inline Duration abs(const Duration& duration)
402 {
403  if ( duration.is_negative() )
404  return duration.invert_sign();
405  return duration;
406 }
407 
418 class MIRA_BASE_EXPORT Time : public boost::posix_time::ptime
419 {
420 public:
421 
422  typedef boost::posix_time::ptime Base;
423 
424 public:
425 
427  Time(Date d, Duration td) :
428  Base(d, td)
429  {}
430 
432  Time(const Base& p) :
433  Base(p)
434  {}
436  explicit Time(Date d) :
437  Base(d, Duration(0, 0, 0))
438  {}
439 
441  Time(const Base::time_rep_type& rhs) :
442  Base(rhs)
443  {}
445  Time(const boost::posix_time::special_values sv) :
446  Base(sv)
447  {}
448 
449 #if !defined(DATE_TIME_NO_DEFAULT_CONSTRUCTOR)
450  Time()
452  {}
453 #endif // DATE_TIME_NO_DEFAULT_CONSTRUCTOR
454 
455 public:
456 
458 
459  template<typename Reflector>
460  void reflectRead(Reflector& r)
461  {
462  int64 t = toUnixNS();
463  r.delegate(t);
464  }
465 
466  template<typename Reflector>
467  void reflectWrite(Reflector& r)
468  {
469  int64 t;
470  r.delegate(t);
471  *this = Time::fromUnixNS(t);
472  }
473 
474 
475 public:
476 
481  static Time now()
482 #ifdef MIRA_LINUX
483  {
484  return boost::posix_time::microsec_clock::universal_time();
485  }
486 #endif
487 #ifdef MIRA_WINDOWS
488  ; // see Time.C for implementation
489 #endif
490 
494  static Time eternity()
495  {
496  return Time(boost::posix_time::max_date_time);
497  }
498 
502  static Time invalid()
503  {
504  return Time(boost::posix_time::not_a_date_time);
505  }
506 
511  static Time unixEpoch()
512  {
513  static Time t1970 = Time(Date(1970,Jan,1), Duration(0,0,0));
514  return t1970;
515  }
516 
521  uint32 toUnixTimestamp() const
522  {
523  return (uint32)(*this - Time::unixEpoch()).totalSeconds();
524  }
525 
530  static Time fromUnixTimestamp(uint32 seconds)
531  {
532  return Time::unixEpoch() + Duration::seconds(seconds);
533  }
534 
539  uint64 toUnixNS() const
540  {
541  return (uint64)(*this - Time::unixEpoch()).totalNanoseconds();
542  }
543 
548  static Time fromUnixNS(uint64 nanoseconds)
549  {
550  return Time::unixEpoch() + Duration::nanoseconds(nanoseconds);
551  }
552 
557  Time toLocal() const
558  {
559  typedef boost::date_time::c_local_adjustor<Base> l;
560  return l::utc_to_local(*this);
561  }
562 
569  Time toTimeZone(const std::string& zone)
570  {
571  boost::local_time::time_zone_ptr tz(new boost::local_time::posix_time_zone(zone));
572  boost::local_time::local_date_time dt(*this, tz);
573  return dt.local_time();
574  }
575 
580  bool isValid() const
581  {
582  return !this->is_not_a_date_time();
583  }
584 
585 public:
586 
589  Duration operator-(const Time& t) const
590  {
591  return Base::operator-(t);
592  }
593  Time operator+(const DateDuration& d) const
594  {
595  return Base::operator+(d);
596  }
598  {
599  return Base::operator+=(d);
600  }
601  Time operator-(const DateDuration& d) const
602  {
603  return Base::operator-(d);
604  }
606  {
607  return Base::operator-=(d);
608  }
609  Time operator+(const Duration& d) const
610  {
611  return Base::operator+(d);
612  }
614  {
615  return Base::operator+=(d);
616  }
617  Time operator-(const Duration& d) const
618  {
619  return Base::operator-(d);
620  }
622  {
623  return Base::operator-=(d);
624  }
626 };
627 
629 
630 template<typename SerializerTag>
631 class IsTransparentSerializable<Time,SerializerTag> : public std::true_type {};
632 
633 template<typename SerializerTag>
634 class IsTransparentSerializable<Duration,SerializerTag> : public std::true_type {};
635 
637 
638 template <>
639 class IsCheapToCopy<Time> : public std::true_type {};
640 
641 template <>
642 class IsCheapToCopy<Duration> : public std::true_type {};
643 
645 
646 // non intrusive reflect for Date
647 
648 template<typename Reflector>
649 void reflectRead(Reflector& r, Date& date) {
650  Time t = Time(date);
651  r.delegate(t);
652 }
653 
654 template<typename Reflector>
655 void reflectWrite(Reflector& r, Date& date) {
656  Time t;
657  r.delegate(t);
658  date = t.date();
659 }
661 template<typename SerializerTag>
662 class IsTransparentSerializable<Date,SerializerTag> : public std::true_type {};
663 
665 
666 }
667 
668 #endif
void reflectWrite(Reflector &r, Buffer< T, Allocator > &c)
Specialization of the non-intrusive reflect for Buffer.
Definition: Buffer.h:581
static Duration invalid()
Returns an invalid duration.
Definition: Time.h:249
Duration operator/(float divisor) const
Definition: Time.h:341
Time(const boost::posix_time::special_values sv)
Construct from special value.
Definition: Time.h:445
Typedefs for OS independent basic data types.
Duration operator*=(float rhs)
Definition: Time.h:387
Type trait that indicates whether a type should be serialized "transparently", i.e.
Definition: IsTransparentSerializable.h:81
Duration operator*=(int rhs)
Definition: Time.h:383
static Duration microseconds(int64 v)
Can be used to construct a Duration object that is specified in microseconds.
Definition: Time.h:134
tick_type milliseconds() const
Returns normalized number of milliseconds (0..999)
Definition: Time.h:285
Duration(const Base &d)
Construct from base class.
Definition: Time.h:195
#define MIRA_SPLIT_REFLECT(Type)
Macro that inserts a reflect() method consisting of just a call to splitReflect() (splitting to refle...
Definition: SplitReflect.h:150
Time(Date d)
Construct a time at start of the given day (midnight)
Definition: Time.h:436
static Duration minutes(int32 v)
Can be used to construct a Duration object that is specified in minutes.
Definition: Time.h:179
static Duration nanoseconds(int64 v)
Can be used to construct a Duration object that is specified in nanoseconds.
Definition: Time.h:119
static Time fromUnixNS(uint64 nanoseconds)
Creates a time representation out of a unix timestamp.
Definition: Time.h:548
Time toLocal() const
Converts to local time zone based on the settings of the machine.
Definition: Time.h:557
bool isValid() const
Checks if this duration is invalid.
Definition: Time.h:257
Time operator+=(const DateDuration &d)
Definition: Time.h:597
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
Duration()
Default constructor constructs to Duration of 0 length.
Definition: Time.h:206
Time(const Base &p)
Construct from base class.
Definition: Time.h:432
bool isValid() const
Returns true if this contains a valid time.
Definition: Time.h:580
Duration operator+(const Duration &d) const
Definition: Time.h:332
Time operator-=(const Duration &d)
Definition: Time.h:621
#define MIRA_SPLIT_REFLECT_MEMBER
Macro that insert a class member reflect() method just splitting reflection into a reflectRead() and ...
Definition: SplitReflect.h:209
Duration operator/(int divisor) const
Definition: Time.h:336
Duration operator/=(int divisor)
Division operations on a duration with an integer.
Definition: Time.h:361
Provides type trait that indicates whether a type should be serialized "transparently".
Provides MIRA_SPLIT_REFLECT macros.
static Time unixEpoch()
Returns the unix epoch 1.1.1970 0:0:0.000.
Definition: Time.h:511
tick_type totalNanoseconds() const
Returns total number of nanoseconds truncating any fractional nanoseconds.
Definition: Time.h:315
Duration(const boost::posix_time::special_values sv)
Construct from special value.
Definition: Time.h:200
boost::posix_time::ptime Base
Definition: Time.h:422
Wrapper class for boost::posix_time::ptime for adding more functionality to it.
Definition: Time.h:418
static Duration hours(int32 v)
Can be used to construct a Duration object that is specified in hours.
Definition: Time.h:186
Duration abs(const Duration &duration)
Get the absolute duration from a duration.
Definition: Time.h:401
Duration operator*(int rhs) const
Multiplication operations an a duration with an integer.
Definition: Time.h:373
boost::posix_time::time_duration Base
Definition: Time.h:110
static Duration seconds(int32 v)
Can be used to construct a Duration object that is specified in seconds.
Definition: Time.h:172
tick_type totalMilliseconds() const
Returns total number of milliseconds truncating any fractional milliseconds.
Definition: Time.h:305
float operator/(Duration other) const
Definition: Time.h:348
Duration operator-=(const Duration &d)
Definition: Time.h:352
By default, IsCheapToCopy<T>::value evaluates to true for fundamental types T, false for all other ty...
Definition: IsCheapToCopy.h:63
min_type minutes() const
Returns normalized number of minutes (0..59)
Definition: Time.h:275
sec_type seconds() const
Returns normalized number of seconds (0..59)
Definition: Time.h:280
Use this class to represent time durations.
Definition: Time.h:106
tick_type totalMicroseconds() const
Returns total number of microseconds truncating any fractional microseconds.
Definition: Time.h:310
boost::gregorian::date_duration DateDuration
Definition: Time.h:71
Time(Date d, Duration td)
Construct from date and timespan.
Definition: Time.h:427
boost::gregorian::date Date
Typedef for the boost Gregorian calendar date.
Definition: Time.h:70
Time operator-(const DateDuration &d) const
Definition: Time.h:601
Type trait to define if a class is cheap to copy.
hour_type hours() const
Returns number of hours in the duration.
Definition: Time.h:270
Duration operator-(const Time &t) const
Definition: Time.h:589
tick_type microseconds() const
Returns normalized number of microseconds (0..999)
Definition: Time.h:290
Time operator+(const DateDuration &d) const
Definition: Time.h:593
MIRA_SPLIT_REFLECT_MEMBER void reflectRead(Reflector &r)
Definition: Time.h:460
static Duration infinity()
Returns a special duration time representing positive infinity.
Definition: Time.h:242
static Duration negativeInfinity()
Returns a special duration time representing negative infinity.
Definition: Time.h:235
Duration operator+=(const Duration &d)
Definition: Time.h:356
Time toTimeZone(const std::string &zone)
Converts to given time zone.
Definition: Time.h:569
Duration(hour_type hour, min_type min, sec_type sec, fractional_seconds_type fs=0)
Definition: Time.h:189
Time(const Base::time_rep_type &rhs)
Construct from time representation.
Definition: Time.h:441
void reflectRead(Reflector &r, Buffer< T, Allocator > &c)
Specialization of the non-intrusive reflect for Buffer.
Definition: Buffer.h:565
bool isInfinity() const
Checks if this duration is infinity.
Definition: Time.h:264
sec_type totalSeconds() const
Returns total number of seconds truncating any fractional seconds.
Definition: Time.h:300
static Time now() static Time eternity()
Returns the current utc based time.
Definition: Time.h:481
Time operator-(const Duration &d) const
Definition: Time.h:617
static Time invalid()
Returns an invalid time.
Definition: Time.h:502
Time operator+(const Duration &d) const
Definition: Time.h:609
static Duration milliseconds(int64 v)
Can be used to construct a Duration object that is specified in milliseconds.
Definition: Time.h:149
tick_type nanoseconds() const
Returns normalized number of nanoseconds (0..999)
Definition: Time.h:295
Duration operator-(const Duration &d) const
Definition: Time.h:328
#define MIRA_BASE_EXPORT
This is required because on windows there is a macro defined called ERROR.
Definition: Platform.h:153
Time operator+=(const Duration &d)
Definition: Time.h:613
Duration operator/=(float divisor)
Definition: Time.h:366
void reflectWrite(Reflector &r)
Definition: Time.h:467
void reflectWrite(Reflector &r)
Definition: Time.h:223
static Time fromUnixTimestamp(uint32 seconds)
Creates a time representation out of a unix timestamp.
Definition: Time.h:530
Duration operator-() const
Definition: Time.h:324
uint64 toUnixNS() const
Converts the current time to a unix timestamp in nanoseconds.
Definition: Time.h:539
Time operator-=(const DateDuration &d)
Definition: Time.h:605
Duration operator*(float rhs) const
Definition: Time.h:377
MIRA_SPLIT_REFLECT_MEMBER void reflectRead(Reflector &r)
Reflect.
Definition: Time.h:216
uint32 toUnixTimestamp() const
Converts the current time to a unix timestamp in seconds.
Definition: Time.h:521