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 Duration& d) :
194  Base(d)
195  {}
196 
199  Base(d)
200  {}
201 
203  Duration(const boost::posix_time::special_values sv) :
204  Base(sv)
205  {}
206 
207 #if !defined(DATE_TIME_NO_DEFAULT_CONSTRUCTOR)
208  Duration()
210  {}
211 #endif // DATE_TIME_NO_DEFAULT_CONSTRUCTOR
212 
213 public:
214 
216 
218  template<typename Reflector>
219  void reflectRead(Reflector& r)
220  {
221  int64 t = totalMilliseconds();
222  r.delegate(t);
223  }
224 
225  template<typename Reflector>
226  void reflectWrite(Reflector& r)
227  {
228  int64 t;
229  r.delegate(t);
230  *this = Duration::milliseconds(t);
231  }
232 
233 public:
234 
239  {
240  return Duration(boost::posix_time::neg_infin);
241  }
246  {
247  return Duration(boost::posix_time::pos_infin);
248  }
252  static Duration invalid()
253  {
254  return Duration(boost::posix_time::not_a_date_time);
255  }
256 
260  bool isValid() const
261  {
262  return !this->is_not_a_date_time();
263  }
267  bool isInfinity() const
268  {
269  return this->is_pos_infinity() || this->is_neg_infinity();
270  }
271 
273  hour_type hours() const
274  {
275  return Base::hours();
276  }
278  min_type minutes() const
279  {
280  return Base::minutes();
281  }
283  sec_type seconds() const
284  {
285  return Base::seconds();
286  }
288  tick_type milliseconds() const
289  {
290  return totalMilliseconds() % 1000;
291  }
293  tick_type microseconds() const
294  {
295  return totalMicroseconds() % 1000;
296  }
298  tick_type nanoseconds() const
299  {
300  return totalNanoseconds() % 1000;
301  }
303  sec_type totalSeconds() const
304  {
305  return Base::total_seconds();
306  }
308  tick_type totalMilliseconds() const
309  {
310  return Base::total_milliseconds();
311  }
313  tick_type totalMicroseconds() const
314  {
315  return Base::total_microseconds();
316  }
318  tick_type totalNanoseconds() const
319  {
320  return Base::total_nanoseconds();
321  }
322 
323 public:
324 
328  {
329  return Base::operator -();
330  }
331  Duration operator-(const Duration& d) const
332  {
333  return Base::operator-(d);
334  }
335  Duration operator+(const Duration& d) const
336  {
337  return Base::operator+(d);
338  }
339  Duration operator/(int divisor) const
340  {
341  assert(divisor != 0);
342  return Base::operator/(divisor);
343  }
344  Duration operator/(float divisor) const
345  {
346  assert(divisor != 0.0f);
347  Duration d;
348  d.ticks_ = (int64)(ticks_.as_number() / divisor);
349  return d;
350  }
351  float operator/(Duration other) const
352  {
353  return (float)totalNanoseconds() / other.totalNanoseconds();
354  }
356  {
357  return Base::operator-=(d);
358  }
360  {
361  return Base::operator+=(d);
362  }
364  Duration operator/=(int divisor)
365  {
366  assert(divisor != 0);
367  return Base::operator/=(divisor);
368  }
369  Duration operator/=(float divisor)
370  {
371  assert(divisor != 0.0f);
372  ticks_ = (int64)(ticks_.as_number() / divisor);
373  return *this;
374  }
376  Duration operator*(int rhs) const
377  {
378  return Base::operator*(rhs);
379  }
380  Duration operator*(float rhs) const
381  {
382  Duration d;
383  d.ticks_ = (int64)(ticks_.as_number() * rhs);
384  return d;
385  }
387  {
388  return Base::operator*=(rhs);
389  }
390  Duration operator*=(float rhs)
391  {
392  ticks_ = (int64)(ticks_.as_number() * rhs);
393  return *this;
394  }
396 };
397 
399 
404 inline Duration abs(const Duration& duration)
405 {
406  if ( duration.is_negative() )
407  return duration.invert_sign();
408  return duration;
409 }
410 
421 class MIRA_BASE_EXPORT Time : public boost::posix_time::ptime
422 {
423 public:
424 
425  typedef boost::posix_time::ptime Base;
426 
427 public:
428 
430  Time(Date d, Duration td) :
431  Base(d, td)
432  {}
433 
435  Time(Base p) :
436  Base(p)
437  {}
439  explicit Time(Date d) :
440  Base(d, Duration(0, 0, 0))
441  {}
442 
444  Time(const Base::time_rep_type& rhs) :
445  Base(rhs)
446  {}
448  Time(const boost::posix_time::special_values sv) :
449  Base(sv)
450  {}
451 
452 #if !defined(DATE_TIME_NO_DEFAULT_CONSTRUCTOR)
453  Time()
455  {}
456 #endif // DATE_TIME_NO_DEFAULT_CONSTRUCTOR
457 
458 public:
459 
461 
462  template<typename Reflector>
463  void reflectRead(Reflector& r)
464  {
465  int64 t = toUnixNS();
466  r.delegate(t);
467  }
468 
469  template<typename Reflector>
470  void reflectWrite(Reflector& r)
471  {
472  int64 t;
473  r.delegate(t);
474  *this = Time::fromUnixNS(t);
475  }
476 
477 
478 public:
479 
484  static Time now()
485 #ifdef MIRA_LINUX
486  {
487  return boost::posix_time::microsec_clock::universal_time();
488  }
489 #endif
490 #ifdef MIRA_WINDOWS
491  ; // see Time.C for implementation
492 #endif
493 
497  static Time eternity()
498  {
499  return Time(boost::posix_time::max_date_time);
500  }
501 
505  static Time invalid()
506  {
507  return Time(boost::posix_time::not_a_date_time);
508  }
509 
514  static Time unixEpoch()
515  {
516  static Time t1970 = Time(Date(1970,Jan,1), Duration(0,0,0));
517  return t1970;
518  }
519 
524  uint32 toUnixTimestamp() const
525  {
526  return (uint32)(*this - Time::unixEpoch()).totalSeconds();
527  }
528 
533  static Time fromUnixTimestamp(uint32 seconds)
534  {
535  return Time::unixEpoch() + Duration::seconds(seconds);
536  }
537 
542  uint64 toUnixNS() const
543  {
544  return (uint64)(*this - Time::unixEpoch()).totalNanoseconds();
545  }
546 
551  static Time fromUnixNS(uint64 nanoseconds)
552  {
553  return Time::unixEpoch() + Duration::nanoseconds(nanoseconds);
554  }
555 
560  Time toLocal() const
561  {
562  typedef boost::date_time::c_local_adjustor<Base> l;
563  return l::utc_to_local(*this);
564  }
565 
572  Time toTimeZone(const std::string& zone)
573  {
574  boost::local_time::time_zone_ptr tz(new boost::local_time::posix_time_zone(zone));
575  boost::local_time::local_date_time dt(*this, tz);
576  return dt.local_time();
577  }
578 
583  bool isValid() const
584  {
585  return !this->is_not_a_date_time();
586  }
587 
588 public:
589 
592  Duration operator-(const Time& t) const
593  {
594  return Base::operator-(t);
595  }
596  Time operator+(const DateDuration& d) const
597  {
598  return Base::operator+(d);
599  }
601  {
602  return Base::operator+=(d);
603  }
604  Time operator-(const DateDuration& d) const
605  {
606  return Base::operator-(d);
607  }
609  {
610  return Base::operator-=(d);
611  }
612  Time operator+(const Duration& d) const
613  {
614  return Base::operator+(d);
615  }
617  {
618  return Base::operator+=(d);
619  }
620  Time operator-(const Duration& d) const
621  {
622  return Base::operator-(d);
623  }
625  {
626  return Base::operator-=(d);
627  }
629 };
630 
631 template<typename SerializerTag>
632 class IsTransparentSerializable<Time,SerializerTag> : public std::true_type {};
633 
634 template<typename SerializerTag>
635 class IsTransparentSerializable<Duration,SerializerTag> : public std::true_type {};
636 
638 
639 // non intrusive reflect for Date
640 
641 template<typename Reflector>
642 void reflectRead(Reflector& r, Date& date) {
643  Time t = Time(date);
644  r.delegate(t);
645 }
646 
647 template<typename Reflector>
648 void reflectWrite(Reflector& r, Date& date) {
649  Time t;
650  r.delegate(t);
651  date = t.date();
652 }
654 template<typename SerializerTag>
655 class IsTransparentSerializable<Date,SerializerTag> : public std::true_type {};
656 
658 
659 }
660 
661 #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:252
Duration operator/(float divisor) const
Definition: Time.h:344
Time(const boost::posix_time::special_values sv)
Construct from special value.
Definition: Time.h:448
Typedefs for OS independent basic data types.
Duration operator*=(float rhs)
Definition: Time.h:390
Type trait that indicates whether a type should be serialized "transparently", i.e.
Definition: IsTransparentSerializable.h:81
Duration operator*=(int rhs)
Definition: Time.h:386
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:288
#define MIRA_SPLIT_REFLECT(Type)
Macro that inserts a reflect() method consisting of just a call to splitReflect() (splitting to refle...
Definition: SplitReflect.h:154
Time(Date d)
Construct a time at start of the given day (midnight)
Definition: Time.h:439
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
Time(Base p)
Construct from base class.
Definition: Time.h:435
static Time fromUnixNS(uint64 nanoseconds)
Creates a time representation out of a unix timestamp.
Definition: Time.h:551
Time toLocal() const
Converts to local time zone based on the settings of the machine.
Definition: Time.h:560
bool isValid() const
Checks if this duration is invalid.
Definition: Time.h:260
Time operator+=(const DateDuration &d)
Definition: Time.h:600
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:209
bool isValid() const
Returns true if this contains a valid time.
Definition: Time.h:583
Duration operator+(const Duration &d) const
Definition: Time.h:335
Time operator-=(const Duration &d)
Definition: Time.h:624
#define MIRA_SPLIT_REFLECT_MEMBER
Macro that insert a class member reflect() method just splitting reflection into a reflectRead() and ...
Definition: SplitReflect.h:238
Duration(const Duration &d)
Construct from duration.
Definition: Time.h:193
Duration operator/(int divisor) const
Definition: Time.h:339
Duration operator/=(int divisor)
Division operations on a duration with an integer.
Definition: Time.h:364
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:514
tick_type totalNanoseconds() const
Returns total number of nanoseconds truncating any fractional nanoseconds.
Definition: Time.h:318
Duration(const boost::posix_time::special_values sv)
Construct from special value.
Definition: Time.h:203
boost::posix_time::ptime Base
Definition: Time.h:425
Wrapper class for boost::posix_time::ptime for adding more functionality to it.
Definition: Time.h:421
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:404
Duration operator*(int rhs) const
Multiplication operations an a duration with an integer.
Definition: Time.h:376
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:308
float operator/(Duration other) const
Definition: Time.h:351
Duration operator-=(const Duration &d)
Definition: Time.h:355
min_type minutes() const
Returns normalized number of minutes (0..59)
Definition: Time.h:278
sec_type seconds() const
Returns normalized number of seconds (0..59)
Definition: Time.h:283
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:313
boost::gregorian::date_duration DateDuration
Definition: Time.h:69
Time(Date d, Duration td)
Construct from date and timespan.
Definition: Time.h:430
boost::gregorian::date Date
Typedef for the boost Gregorian calendar date.
Definition: Time.h:68
Time operator-(const DateDuration &d) const
Definition: Time.h:604
hour_type hours() const
Returns number of hours in the duration.
Definition: Time.h:273
Duration operator-(const Time &t) const
Definition: Time.h:592
tick_type microseconds() const
Returns normalized number of microseconds (0..999)
Definition: Time.h:293
Time operator+(const DateDuration &d) const
Definition: Time.h:596
MIRA_SPLIT_REFLECT_MEMBER void reflectRead(Reflector &r)
Definition: Time.h:463
static Duration infinity()
Returns a special duration time representing positive infinity.
Definition: Time.h:245
static Duration negativeInfinity()
Returns a special duration time representing negative infinity.
Definition: Time.h:238
Duration operator+=(const Duration &d)
Definition: Time.h:359
Time toTimeZone(const std::string &zone)
Converts to given time zone.
Definition: Time.h:572
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:444
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:267
sec_type totalSeconds() const
Returns total number of seconds truncating any fractional seconds.
Definition: Time.h:303
static Time now() static Time eternity()
Returns the current utc based time.
Definition: Time.h:484
Time operator-(const Duration &d) const
Definition: Time.h:620
static Time invalid()
Returns an invalid time.
Definition: Time.h:505
Duration(Base d)
Construct from base class.
Definition: Time.h:198
Time operator+(const Duration &d) const
Definition: Time.h:612
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:298
Duration operator-(const Duration &d) const
Definition: Time.h:331
#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:616
Duration operator/=(float divisor)
Definition: Time.h:369
void reflectWrite(Reflector &r)
Definition: Time.h:470
void reflectWrite(Reflector &r)
Definition: Time.h:226
static Time fromUnixTimestamp(uint32 seconds)
Creates a time representation out of a unix timestamp.
Definition: Time.h:533
Duration operator-() const
Definition: Time.h:327
uint64 toUnixNS() const
Converts the current time to a unix timestamp in nanoseconds.
Definition: Time.h:542
Time operator-=(const DateDuration &d)
Definition: Time.h:608
Duration operator*(float rhs) const
Definition: Time.h:380
MIRA_SPLIT_REFLECT_MEMBER void reflectRead(Reflector &r)
Reflect.
Definition: Time.h:219
uint32 toUnixTimestamp() const
Converts the current time to a unix timestamp in seconds.
Definition: Time.h:524