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 namespace mira {
61 
63 
68 typedef boost::gregorian::date Date;
69 typedef boost::gregorian::date_duration DateDuration;
70 
71 using boost::date_time::Jan;
72 using boost::date_time::Feb;
73 using boost::date_time::Mar;
74 using boost::date_time::Apr;
75 using boost::date_time::May;
76 using boost::date_time::Jun;
77 using boost::date_time::Jul;
78 using boost::date_time::Aug;
79 using boost::date_time::Sep;
80 using boost::date_time::Oct;
81 using boost::date_time::Nov;
82 using boost::date_time::Dec;
83 
84 using boost::date_time::Monday;
85 using boost::date_time::Tuesday;
86 using boost::date_time::Wednesday;
87 using boost::date_time::Thursday;
88 using boost::date_time::Friday;
89 using boost::date_time::Saturday;
90 using boost::date_time::Sunday;
91 
104 class Duration : public boost::posix_time::time_duration
105 {
106 protected:
107 
108  typedef boost::posix_time::time_duration Base;
109 
110 public:
111 
117  static Duration nanoseconds(int64 v) {
118  Duration d;
119 #if defined(BOOST_DATE_TIME_HAS_NANOSECONDS)
120  d.ticks_ = v;
121 #else
122  d.ticks_ = v / 1000;
123 #endif
124  return d;
125  }
126 
132  static Duration microseconds(int64 v) {
133  Duration d;
134 #if defined(BOOST_DATE_TIME_HAS_NANOSECONDS)
135  d.ticks_ = v * 1000;
136 #else
137  d.ticks_ = v;
138 #endif
139  return d;
140  }
141 
147  static Duration milliseconds(int64 v) {
148  Duration d;
149 #if defined(BOOST_DATE_TIME_HAS_NANOSECONDS)
150  d.ticks_ = v * 1000000;
151 #else
152  d.ticks_ = v * 1000;
153 #endif
154  return d;
155  }
156 
170  static Duration seconds(int32 v) { return boost::posix_time::seconds(v); }
171 
177  static Duration minutes(int32 v) { return boost::posix_time::minutes(v); }
178 
184  static Duration hours(int32 v) { return boost::posix_time::hours(v); }
185 
186 public:
187  Duration(hour_type hour, min_type min, sec_type sec,
188  fractional_seconds_type fs=0) :
189  Base(hour, min, sec, fs)
190  {}
191 
193  Duration(const Base& d) :
194  Base(d)
195  {}
196 
198  Duration(const boost::posix_time::special_values sv) :
199  Base(sv)
200  {}
201 
202 #if !defined(DATE_TIME_NO_DEFAULT_CONSTRUCTOR)
203  Duration()
205  {}
206 #endif // DATE_TIME_NO_DEFAULT_CONSTRUCTOR
207 
208 public:
209 
211 
213  template<typename Reflector>
214  void reflectRead(Reflector& r)
215  {
216  int64 t = totalMilliseconds();
217  r.delegate(t);
218  }
219 
220  template<typename Reflector>
221  void reflectWrite(Reflector& r)
222  {
223  int64 t;
224  r.delegate(t);
225  *this = Duration::milliseconds(t);
226  }
227 
228 public:
229 
234  {
235  return Duration(boost::posix_time::neg_infin);
236  }
241  {
242  return Duration(boost::posix_time::pos_infin);
243  }
247  static Duration invalid()
248  {
249  return Duration(boost::posix_time::not_a_date_time);
250  }
251 
255  bool isValid() const
256  {
257  return !this->is_not_a_date_time();
258  }
262  bool isInfinity() const
263  {
264  return this->is_pos_infinity() || this->is_neg_infinity();
265  }
266 
268  hour_type hours() const
269  {
270  return Base::hours();
271  }
273  min_type minutes() const
274  {
275  return Base::minutes();
276  }
278  sec_type seconds() const
279  {
280  return Base::seconds();
281  }
283  tick_type milliseconds() const
284  {
285  return totalMilliseconds() % 1000;
286  }
288  tick_type microseconds() const
289  {
290  return totalMicroseconds() % 1000;
291  }
293  tick_type nanoseconds() const
294  {
295  return totalNanoseconds() % 1000;
296  }
298  sec_type totalSeconds() const
299  {
300  return Base::total_seconds();
301  }
303  tick_type totalMilliseconds() const
304  {
305  return Base::total_milliseconds();
306  }
308  tick_type totalMicroseconds() const
309  {
310  return Base::total_microseconds();
311  }
313  tick_type totalNanoseconds() const
314  {
315  return Base::total_nanoseconds();
316  }
317 
318 public:
319 
323  {
324  return Base::operator -();
325  }
326  Duration operator-(const Duration& d) const
327  {
328  return Base::operator-(d);
329  }
330  Duration operator+(const Duration& d) const
331  {
332  return Base::operator+(d);
333  }
334  Duration operator/(int divisor) const
335  {
336  assert(divisor != 0);
337  return Base::operator/(divisor);
338  }
339  Duration operator/(float divisor) const
340  {
341  assert(divisor != 0.0f);
342  Duration d;
343  d.ticks_ = (int64)(ticks_.as_number() / divisor);
344  return d;
345  }
346  float operator/(Duration other) const
347  {
348  return (float)totalNanoseconds() / other.totalNanoseconds();
349  }
351  {
352  return Base::operator-=(d);
353  }
355  {
356  return Base::operator+=(d);
357  }
359  Duration operator/=(int divisor)
360  {
361  assert(divisor != 0);
362  return Base::operator/=(divisor);
363  }
364  Duration operator/=(float divisor)
365  {
366  assert(divisor != 0.0f);
367  ticks_ = (int64)(ticks_.as_number() / divisor);
368  return *this;
369  }
371  Duration operator*(int rhs) const
372  {
373  return Base::operator*(rhs);
374  }
375  Duration operator*(float rhs) const
376  {
377  Duration d;
378  d.ticks_ = (int64)(ticks_.as_number() * rhs);
379  return d;
380  }
382  {
383  return Base::operator*=(rhs);
384  }
385  Duration operator*=(float rhs)
386  {
387  ticks_ = (int64)(ticks_.as_number() * rhs);
388  return *this;
389  }
391 };
392 
394 
399 inline Duration abs(const Duration& duration)
400 {
401  if ( duration.is_negative() )
402  return duration.invert_sign();
403  return duration;
404 }
405 
416 class MIRA_BASE_EXPORT Time : public boost::posix_time::ptime
417 {
418 public:
419 
420  typedef boost::posix_time::ptime Base;
421 
422 public:
423 
425  Time(Date d, Duration td) :
426  Base(d, td)
427  {}
428 
430  Time(const Base& p) :
431  Base(p)
432  {}
434  explicit Time(Date d) :
435  Base(d, Duration(0, 0, 0))
436  {}
437 
439  Time(const Base::time_rep_type& rhs) :
440  Base(rhs)
441  {}
443  Time(const boost::posix_time::special_values sv) :
444  Base(sv)
445  {}
446 
447 #if !defined(DATE_TIME_NO_DEFAULT_CONSTRUCTOR)
448  Time()
450  {}
451 #endif // DATE_TIME_NO_DEFAULT_CONSTRUCTOR
452 
453 public:
454 
456 
457  template<typename Reflector>
458  void reflectRead(Reflector& r)
459  {
460  int64 t = toUnixNS();
461  r.delegate(t);
462  }
463 
464  template<typename Reflector>
465  void reflectWrite(Reflector& r)
466  {
467  int64 t;
468  r.delegate(t);
469  *this = Time::fromUnixNS(t);
470  }
471 
472 
473 public:
474 
479  static Time now()
480 #ifdef MIRA_LINUX
481  {
482  return boost::posix_time::microsec_clock::universal_time();
483  }
484 #endif
485 #ifdef MIRA_WINDOWS
486  ; // see Time.C for implementation
487 #endif
488 
492  static Time eternity()
493  {
494  return Time(boost::posix_time::max_date_time);
495  }
496 
500  static Time invalid()
501  {
502  return Time(boost::posix_time::not_a_date_time);
503  }
504 
509  static Time unixEpoch()
510  {
511  static Time t1970 = Time(Date(1970,Jan,1), Duration(0,0,0));
512  return t1970;
513  }
514 
519  uint32 toUnixTimestamp() const
520  {
521  return (uint32)(*this - Time::unixEpoch()).totalSeconds();
522  }
523 
528  static Time fromUnixTimestamp(uint32 seconds)
529  {
530  return Time::unixEpoch() + Duration::seconds(seconds);
531  }
532 
537  uint64 toUnixNS() const
538  {
539  return (uint64)(*this - Time::unixEpoch()).totalNanoseconds();
540  }
541 
546  static Time fromUnixNS(uint64 nanoseconds)
547  {
548  return Time::unixEpoch() + Duration::nanoseconds(nanoseconds);
549  }
550 
555  Time toLocal() const
556  {
557  typedef boost::date_time::c_local_adjustor<Base> l;
558  return l::utc_to_local(*this);
559  }
560 
567  Time toTimeZone(const std::string& zone)
568  {
569  boost::local_time::time_zone_ptr tz(new boost::local_time::posix_time_zone(zone));
570  boost::local_time::local_date_time dt(*this, tz);
571  return dt.local_time();
572  }
573 
578  bool isValid() const
579  {
580  return !this->is_not_a_date_time();
581  }
582 
583 public:
584 
587  Duration operator-(const Time& t) const
588  {
589  return Base::operator-(t);
590  }
591  Time operator+(const DateDuration& d) const
592  {
593  return Base::operator+(d);
594  }
596  {
597  return Base::operator+=(d);
598  }
599  Time operator-(const DateDuration& d) const
600  {
601  return Base::operator-(d);
602  }
604  {
605  return Base::operator-=(d);
606  }
607  Time operator+(const Duration& d) const
608  {
609  return Base::operator+(d);
610  }
612  {
613  return Base::operator+=(d);
614  }
615  Time operator-(const Duration& d) const
616  {
617  return Base::operator-(d);
618  }
620  {
621  return Base::operator-=(d);
622  }
624 };
625 
626 template<typename SerializerTag>
627 class IsTransparentSerializable<Time,SerializerTag> : public std::true_type {};
628 
629 template<typename SerializerTag>
630 class IsTransparentSerializable<Duration,SerializerTag> : public std::true_type {};
631 
633 
634 // non intrusive reflect for Date
635 
636 template<typename Reflector>
637 void reflectRead(Reflector& r, Date& date) {
638  Time t = Time(date);
639  r.delegate(t);
640 }
641 
642 template<typename Reflector>
643 void reflectWrite(Reflector& r, Date& date) {
644  Time t;
645  r.delegate(t);
646  date = t.date();
647 }
649 template<typename SerializerTag>
650 class IsTransparentSerializable<Date,SerializerTag> : public std::true_type {};
651 
653 
654 }
655 
656 #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:247
Duration operator/(float divisor) const
Definition: Time.h:339
Time(const boost::posix_time::special_values sv)
Construct from special value.
Definition: Time.h:443
Typedefs for OS independent basic data types.
Duration operator*=(float rhs)
Definition: Time.h:385
Type trait that indicates whether a type should be serialized "transparently", i.e.
Definition: IsTransparentSerializable.h:81
Duration operator*=(int rhs)
Definition: Time.h:381
static Duration microseconds(int64 v)
Can be used to construct a Duration object that is specified in microseconds.
Definition: Time.h:132
tick_type milliseconds() const
Returns normalized number of milliseconds (0..999)
Definition: Time.h:283
Duration(const Base &d)
Construct from base class.
Definition: Time.h:193
#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:434
static Duration minutes(int32 v)
Can be used to construct a Duration object that is specified in minutes.
Definition: Time.h:177
static Duration nanoseconds(int64 v)
Can be used to construct a Duration object that is specified in nanoseconds.
Definition: Time.h:117
static Time fromUnixNS(uint64 nanoseconds)
Creates a time representation out of a unix timestamp.
Definition: Time.h:546
Time toLocal() const
Converts to local time zone based on the settings of the machine.
Definition: Time.h:555
bool isValid() const
Checks if this duration is invalid.
Definition: Time.h:255
Time operator+=(const DateDuration &d)
Definition: Time.h:595
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:204
Time(const Base &p)
Construct from base class.
Definition: Time.h:430
bool isValid() const
Returns true if this contains a valid time.
Definition: Time.h:578
Duration operator+(const Duration &d) const
Definition: Time.h:330
Time operator-=(const Duration &d)
Definition: Time.h:619
#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:334
Duration operator/=(int divisor)
Division operations on a duration with an integer.
Definition: Time.h:359
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:509
tick_type totalNanoseconds() const
Returns total number of nanoseconds truncating any fractional nanoseconds.
Definition: Time.h:313
Duration(const boost::posix_time::special_values sv)
Construct from special value.
Definition: Time.h:198
boost::posix_time::ptime Base
Definition: Time.h:420
Wrapper class for boost::posix_time::ptime for adding more functionality to it.
Definition: Time.h:416
static Duration hours(int32 v)
Can be used to construct a Duration object that is specified in hours.
Definition: Time.h:184
Duration abs(const Duration &duration)
Get the absolute duration from a duration.
Definition: Time.h:399
Duration operator*(int rhs) const
Multiplication operations an a duration with an integer.
Definition: Time.h:371
boost::posix_time::time_duration Base
Definition: Time.h:108
static Duration seconds(int32 v)
Can be used to construct a Duration object that is specified in seconds.
Definition: Time.h:170
tick_type totalMilliseconds() const
Returns total number of milliseconds truncating any fractional milliseconds.
Definition: Time.h:303
float operator/(Duration other) const
Definition: Time.h:346
Duration operator-=(const Duration &d)
Definition: Time.h:350
min_type minutes() const
Returns normalized number of minutes (0..59)
Definition: Time.h:273
sec_type seconds() const
Returns normalized number of seconds (0..59)
Definition: Time.h:278
Use this class to represent time durations.
Definition: Time.h:104
tick_type totalMicroseconds() const
Returns total number of microseconds truncating any fractional microseconds.
Definition: Time.h:308
boost::gregorian::date_duration DateDuration
Definition: Time.h:69
Time(Date d, Duration td)
Construct from date and timespan.
Definition: Time.h:425
boost::gregorian::date Date
Typedef for the boost Gregorian calendar date.
Definition: Time.h:68
Time operator-(const DateDuration &d) const
Definition: Time.h:599
hour_type hours() const
Returns number of hours in the duration.
Definition: Time.h:268
Duration operator-(const Time &t) const
Definition: Time.h:587
tick_type microseconds() const
Returns normalized number of microseconds (0..999)
Definition: Time.h:288
Time operator+(const DateDuration &d) const
Definition: Time.h:591
MIRA_SPLIT_REFLECT_MEMBER void reflectRead(Reflector &r)
Definition: Time.h:458
static Duration infinity()
Returns a special duration time representing positive infinity.
Definition: Time.h:240
static Duration negativeInfinity()
Returns a special duration time representing negative infinity.
Definition: Time.h:233
Duration operator+=(const Duration &d)
Definition: Time.h:354
Time toTimeZone(const std::string &zone)
Converts to given time zone.
Definition: Time.h:567
Duration(hour_type hour, min_type min, sec_type sec, fractional_seconds_type fs=0)
Definition: Time.h:187
Time(const Base::time_rep_type &rhs)
Construct from time representation.
Definition: Time.h:439
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:262
sec_type totalSeconds() const
Returns total number of seconds truncating any fractional seconds.
Definition: Time.h:298
static Time now() static Time eternity()
Returns the current utc based time.
Definition: Time.h:479
Time operator-(const Duration &d) const
Definition: Time.h:615
static Time invalid()
Returns an invalid time.
Definition: Time.h:500
Time operator+(const Duration &d) const
Definition: Time.h:607
static Duration milliseconds(int64 v)
Can be used to construct a Duration object that is specified in milliseconds.
Definition: Time.h:147
tick_type nanoseconds() const
Returns normalized number of nanoseconds (0..999)
Definition: Time.h:293
Duration operator-(const Duration &d) const
Definition: Time.h:326
#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:611
Duration operator/=(float divisor)
Definition: Time.h:364
void reflectWrite(Reflector &r)
Definition: Time.h:465
void reflectWrite(Reflector &r)
Definition: Time.h:221
static Time fromUnixTimestamp(uint32 seconds)
Creates a time representation out of a unix timestamp.
Definition: Time.h:528
Duration operator-() const
Definition: Time.h:322
uint64 toUnixNS() const
Converts the current time to a unix timestamp in nanoseconds.
Definition: Time.h:537
Time operator-=(const DateDuration &d)
Definition: Time.h:603
Duration operator*(float rhs) const
Definition: Time.h:375
MIRA_SPLIT_REFLECT_MEMBER void reflectRead(Reflector &r)
Reflect.
Definition: Time.h:214
uint32 toUnixTimestamp() const
Converts the current time to a unix timestamp in seconds.
Definition: Time.h:519