mn_timestamp.hpp
Go to the documentation of this file.
1 
21 #ifndef _MINLIB_BASIC_TIMESTAMP_H__
22 #define _MINLIB_BASIC_TIMESTAMP_H__
23 
24 #include "mn_config.hpp"
25 
26 #include "mn_limits.hpp"
27 #include "mn_timespan.hpp"
28 
29 #define MINILIB_TIMESTAMP_RESELUTION 1000000
30 
31 #define MINILIB_TIMESTAMP_TIME_TYPE typename basic_timespan::time_type
32 #define MINILIB_TIMESTAMP_MIN mn::numeric_limits<MINILIB_TIMESTAMP_TIME_TYPE>::min()
33 #define MINILIB_TIMESTAMP_MAX mn::numeric_limits<MINILIB_TIMESTAMP_TIME_TYPE>::max()
34 
35 namespace mn {
41  public:
44 
49 
54 
58  basic_timestamp(const self_type& other);
59 
60  void update();
61  void swap(self_type& time);
62 
66  static self_type from_epoch(const mn::time_t t);
67 
72  static self_type from_utc(const time_type val);
73 
74  mn::time_t get_epoch() const
75  { return mn::time_t(m_time / MINILIB_TIMESTAMP_RESELUTION); }
76 
81  { return m_time * 10 + (time_type(0x01b21dd2) << 32) + 0x13814000; }
82 
88  { return m_time; }
89 
95  { self_type now; return now - *this; }
96 
97 
102  bool is_elapsed(time_type interval) const
103  { return get_elapsed() >= interval; }
104 
105  bool operator == (const self_type& ts) const
106  { return m_time == ts.m_time; }
107 
108  bool operator != (const self_type& ts) const
109  { return m_time != ts.m_time; }
110 
111  bool operator >= (const self_type& ts) const
112  { return m_time >= ts.m_time; }
113 
114  bool operator <= (const self_type& ts) const
115  { return m_time <= ts.m_time; }
116 
117  bool operator < (const self_type& ts) const
118  { return m_time < ts.m_time; }
119 
120  bool operator > (const self_type& ts) const
121  { return m_time > ts.m_time; }
122 
123  time_type operator - (const self_type& ts) const
124  { return m_time - ts.m_time; }
125 
126  time_type operator + (const self_type& ts) const
127  { return m_time + ts.m_time; }
128 
130  { return self_type(m_time + d); }
131 
133  { return self_type(m_time - d); }
134 
136  { m_time += d; return *this; }
137 
139  { m_time -= d; return *this; }
140 
141  self_type operator + (const basic_timespan& span) const {
142  return self_type(m_time + span.get_total_microseconds());
143  }
144  self_type operator - (const basic_timespan& span) const {
145  return self_type(m_time - span.get_total_microseconds());
146  }
148  m_time += span.get_total_microseconds(); return *this;
149  }
151  m_time -= span.get_total_microseconds(); return *this;
152  }
153  self_type& operator = (const self_type& other) {
154  m_time = other.m_time; return *this;
155  }
157  m_time = tv; return *this;
158  }
159 
160  private:
162  };
163 
165  a.swap(b);
166  }
167 
169 }
170 #endif // _MINLIB_BASIC_TIMESTAMP_H__
This class represents a time span (using microseconds)
Definition: mn_timespan.hpp:35
int_type get_total_microseconds() const
Get the total number of microseconds.
Definition: mn_timespan.cpp:191
A Timestamp stores a monotonic* time value with microseconds resolution.
Definition: mn_timestamp.hpp:40
static self_type from_utc(const time_type val)
Creates a timestamp from a UTC time value.
Definition: mn_timestamp.cpp:54
time_type get_elapsed() const
Get the time elapsed since the time denoted by the timestamp.
Definition: mn_timestamp.hpp:94
void update()
Definition: mn_timestamp.cpp:39
basic_timestamp self_type
Definition: mn_timestamp.hpp:43
time_type get_microseconds() const
Get the timestamp expressed in microseconds.
Definition: mn_timestamp.hpp:87
bool operator!=(const self_type &ts) const
Definition: mn_timestamp.hpp:108
mn::time_t get_epoch() const
Definition: mn_timestamp.hpp:74
self_type & operator=(const self_type &other)
Definition: mn_timestamp.hpp:153
MINILIB_TIMESTAMP_TIME_TYPE time_type
Definition: mn_timestamp.hpp:42
bool is_elapsed(time_type interval) const
if the given interval has passed since the time denoted by the timestamp.
Definition: mn_timestamp.hpp:102
bool operator<(const self_type &ts) const
Definition: mn_timestamp.hpp:117
bool operator==(const self_type &ts) const
Definition: mn_timestamp.hpp:105
self_type & operator-=(time_type d)
Definition: mn_timestamp.hpp:138
bool operator<=(const self_type &ts) const
Definition: mn_timestamp.hpp:114
static self_type from_epoch(const mn::time_t t)
Creates a timestamp from a mn::time_t.
Definition: mn_timestamp.cpp:49
basic_timestamp()
Consruct a basic_timestamp with the current time.
Definition: mn_timestamp.cpp:29
bool operator>=(const self_type &ts) const
Definition: mn_timestamp.hpp:111
self_type & operator+=(time_type d)
Definition: mn_timestamp.hpp:135
time_type m_time
Definition: mn_timestamp.hpp:161
time_type operator+(const self_type &ts) const
Definition: mn_timestamp.hpp:126
void swap(self_type &time)
Definition: mn_timestamp.cpp:45
time_type get_utc() const
Get the timestamp expressed in UTC-based time.
Definition: mn_timestamp.hpp:80
time_type operator-(const self_type &ts) const
Definition: mn_timestamp.hpp:123
bool operator>(const self_type &ts) const
Definition: mn_timestamp.hpp:120
The template and inlines for the numeric_limits classes. -*- C++ -*- This file is part of the Mini Th...
#define MINILIB_TIMESTAMP_RESELUTION
Definition: mn_timestamp.hpp:29
#define MINILIB_TIMESTAMP_TIME_TYPE
Definition: mn_timestamp.hpp:31
Definition: mn_allocator_typetraits.hpp:25
void swap(TAssignable &a, TAssignable &b)
Definition: mn_algorithm.hpp:312