hmbdc
simplify-high-performance-messaging-programming
Time.hpp
1 #include "hmbdc/Copyright.hpp"
2 #pragma once
3 
4 #include <sys/time.h>
5 #include <iostream>
6 #include <stdio.h>
7 #include <stdint.h>
8 #include <time.h>
9 #include <limits>
10 
11 namespace hmbdc { namespace time {
12 struct Duration;
13 struct SysTime
14 {
15  SysTime() : nsecSinceEpoch_(0){}
16  ///UTC as input
17  explicit SysTime(int64_t sec, int64_t usec = 0, int64_t nsec = 0) {
18  nsecSinceEpoch_ = sec * 1000000000l + usec*1000l + nsec;
19  }
20 
21  static SysTime now() {
22  struct timespec spec;
23  clock_gettime(CLOCK_REALTIME, &spec);
24  return SysTime(spec.tv_sec, 0, spec.tv_nsec);
25  }
26 
27  int64_t usecSinceEpoch() const { return nsecSinceEpoch_ / 1000l; }
28  Duration sinceMidnight() const;
29  SysTime previousMidnight() const;
30 
31  bool operator < (SysTime const& other) const {
32  return nsecSinceEpoch_ < other.nsecSinceEpoch_;
33  }
34 
35  bool operator <= (SysTime const& other) const {
36  return nsecSinceEpoch_ <= other.nsecSinceEpoch_;
37  }
38 
39  bool operator > (SysTime const& other) const {
40  return nsecSinceEpoch_ > other.nsecSinceEpoch_;
41  }
42 
43  bool operator >= (SysTime const& other) const {
44  return nsecSinceEpoch_ >= other.nsecSinceEpoch_;
45  }
46  bool operator == (SysTime const& other) const {
47  return nsecSinceEpoch_ == other.nsecSinceEpoch_;
48  }
49 
50  Duration operator - (SysTime const&) const;
51  SysTime operator + (Duration const&) const;
52  SysTime operator - (Duration const&) const;
53 
54  static
55  SysTime
56  fromYYYYMMDDhhmmSSmmmUtc(int64_t v) {
57  struct tm v_tm = {0};
58  v_tm.tm_year = static_cast<int>(v/10000000000000l);
59  v %= 10000000000000l;
60  v_tm.tm_year -= 1900;
61  v_tm.tm_mon = static_cast<int>(v/100000000000l);
62  v %= 100000000000l;
63  v_tm.tm_mon -= 1;
64  v_tm.tm_mday = static_cast<int>(v/1000000000l);
65  v %= 1000000000l;
66  v_tm.tm_hour = static_cast<int>(v/10000000l);
67  v %= 10000000l;
68  v_tm.tm_min = static_cast<int>(v/100000l);
69  v %= 100000l;
70  v_tm.tm_sec = static_cast<int>(v/1000l);
71  v %= 1000l;
72  v_tm.tm_isdst = -1;
73  return SysTime(timegm(&v_tm), v * 1000l);
74  }
75 
76  static
77  SysTime
78  fromYYYYMMDDhhmmSSmmm(int64_t v) {
79  struct tm v_tm = {0};
80  v_tm.tm_year = static_cast<int>(v/10000000000000l);
81  v %= 10000000000000l;
82  v_tm.tm_year -= 1900;
83  v_tm.tm_mon = static_cast<int>(v/100000000000l);
84  v %= 100000000000l;
85  v_tm.tm_mon -= 1;
86  v_tm.tm_mday = static_cast<int>(v/1000000000l);
87  v %= 1000000000l;
88  v_tm.tm_hour = static_cast<int>(v/10000000l);
89  v %= 10000000l;
90  v_tm.tm_min = static_cast<int>(v/100000l);
91  v %= 100000l;
92  v_tm.tm_sec = static_cast<int>(v/1000l);
93  v %= 1000l;
94  v_tm.tm_isdst = -1;
95  return SysTime(mktime(&v_tm), v * 1000l);
96  }
97 
98 private:
99  int64_t nsecSinceEpoch_;
100  friend std::ostream& operator << (std::ostream& os, SysTime const& t);
101 };
102 
103 inline
104 std::ostream& operator << (std::ostream& os, SysTime const& t) {
105  char buf[32];
106  char buf2[32];
107  tm ts;
108  time_t sec = (time_t)(t.nsecSinceEpoch_ / 1000000000l);
109  localtime_r(&sec, &ts);
110  strftime(buf, sizeof(buf), "%Y%m%d%H%M%S.", &ts);
111  sprintf(buf2, "%09ld", t.nsecSinceEpoch_ % 1000000000l);
112  os << buf << buf2;
113  return os;
114 }
115 
116 struct Duration {
117  Duration() : nsec_(0){}
118  static Duration seconds(int64_t sec) {return Duration(sec);}
119 
120  static Duration microseconds(int64_t usec) { return Duration(0, usec); }
121 
122  static Duration nanoseconds(int64_t nsec) { return Duration(0, 0, nsec); }
123 
124  explicit Duration(int64_t sec, int64_t usec = 0, int64_t nsec = 0) {
125  nsec_ = sec * 1000000000l + usec * 1000l + nsec;}
126 
127  int64_t microseconds() const { return nsec_ / 1000l; }
128  int64_t nanoseconds() const { return nsec_; }
129 
130  explicit operator bool() const {
131  return nsec_;
132  }
133 
134  bool operator < (Duration const& other) const { return nsec_ < other.nsec_; }
135  bool operator > (Duration const& other) const { return nsec_ > other.nsec_; }
136  bool operator == (Duration const& other) const { return nsec_ == other.nsec_; }
137  bool operator >= (Duration const& other) const { return nsec_ >= other.nsec_; }
138  bool operator <= (Duration const& other) const { return nsec_ <= other.nsec_; }
139 
140  Duration& operator += (Duration const& other) {
141  nsec_ += other.nsec_;
142  return *this;
143  }
144 
145  Duration operator - () const {
146  return nanoseconds(-nsec_);
147  }
148 
149  Duration operator - (Duration const& other) const {
150  return nanoseconds(nsec_ - other.nsec_);
151  }
152 
153  Duration operator + (Duration const& other) const {
154  return nanoseconds(nsec_ + other.nsec_);
155  }
156 
157  Duration& operator -= (Duration const& other) {
158  nsec_ -= other.nsec_;
159  return *this;
160  }
161  double operator / (Duration const& other) const {
162  return (double)nsec_ / other.nsec_;
163  }
164 
165  Duration operator * (int64_t m) const {
166  return Duration(0, 0, nsec_ * m);
167  }
168 
169  Duration operator / (int64_t const& d) const {
170  return Duration(0, 0, nsec_ / d);
171  }
172 
173  Duration operator % (Duration const& d) const {
174  return Duration(0, 0, nsec_ % d.nsec_);
175  }
176 
177 private:
178  friend struct SysTime;
179  friend std::ostream& operator <<
180  (std::ostream& os, Duration const& d);
181  friend std::istream& operator >>
182  (std::istream& is, Duration& d);
183 
184  int64_t nsec_;
185 };
186 
187 inline
188 Duration
189 SysTime::
190 operator - (SysTime const& b) const {
191  return Duration::nanoseconds(nsecSinceEpoch_
192  - b.nsecSinceEpoch_);
193 }
194 
195 inline
196 SysTime
197 SysTime::
198 operator + (Duration const& d) const {
199  return SysTime(0, 0, nsecSinceEpoch_ + d.nsec_);
200 }
201 
202 inline
203 SysTime
204 SysTime::
205 operator - (Duration const& d) const {
206  return SysTime(0, 0, nsecSinceEpoch_ - d.nsec_);
207 }
208 
209 inline
210 Duration
211 SysTime::
212 sinceMidnight() const {
213  return *this - previousMidnight();
214 }
215 
216 inline
217 SysTime
218 SysTime::
219 previousMidnight() const {
220  tm ts;
221  time_t sec = (time_t)(nsecSinceEpoch_ / 1000000000l);
222  localtime_r(&sec, &ts);
223  return SysTime(sec) - Duration(ts.tm_hour * 3600 + ts.tm_min * 60 + ts.tm_sec);
224 }
225 
226 inline
227 SysTime&
228 operator += (SysTime & t, Duration const& d) {
229  t = t + d;
230  return t;
231 }
232 
233 inline
234 SysTime&
235 operator -= (SysTime & t, Duration const& d) {
236  t = t - d;
237  return t;
238 }
239 
240 inline
241 std::ostream&
242 operator << (std::ostream& os, Duration const& d) {
243  char buf[32];
244  if (d.nsec_ >= 0) {
245  sprintf(buf, "%09ld", d.nsec_ % 1000000000l);
246  os << d.nsec_ / 1000000000l << '.' << buf;
247  } else {
248  sprintf(buf, "%09ld", (-d.nsec_) % 1000000000l);
249  os << '-' << (-d.nsec_) / 1000000000l << '.' << buf;
250  }
251 
252  return os;
253 }
254 
255 
256 inline
257 std::istream&
258 operator >> (std::istream& is, Duration& d) {
259  double t;
260  is >> t;
261  d.nsec_ = (int64_t)(t * 1000000000l);
262  return is;
263 }
264 
265 }}
266 
267 namespace std {
268 template <>
269 struct numeric_limits<hmbdc::time::Duration> : numeric_limits<int64_t> {
270 public:
271  static hmbdc::time::Duration min() throw()
272  {return hmbdc::time::Duration::nanoseconds(numeric_limits<int64_t>::min());}
273  static hmbdc::time::Duration max() throw()
274  {return hmbdc::time::Duration::nanoseconds(numeric_limits<int64_t>::max());}
275 };
276 }
Definition: TypedString.hpp:74
SysTime(int64_t sec, int64_t usec=0, int64_t nsec=0)
UTC as input.
Definition: Time.hpp:17
Definition: Time.hpp:13
Definition: Time.hpp:116
Definition: Base.hpp:12