1 #include "hmbdc/Copyright.hpp"
4 #include "hmbdc/Endian.hpp"
12 namespace hmbdc {
namespace time {
16 SysTime() : nsecSinceEpoch_(0){}
18 explicit SysTime(int64_t sec, int64_t usec = 0, int64_t nsec = 0) {
19 nsecSinceEpoch_ = sec * 1000000000l + usec*1000l + nsec;
24 clock_gettime(CLOCK_REALTIME, &spec);
25 return SysTime(spec.tv_sec, 0, spec.tv_nsec);
28 int64_t usecSinceEpoch()
const {
return nsecSinceEpoch_ / 1000l; }
29 Duration sinceMidnight()
const;
30 SysTime previousMidnight()
const;
32 bool operator < (SysTime
const& other)
const {
33 return nsecSinceEpoch_ < other.nsecSinceEpoch_;
36 bool operator <= (SysTime
const& other)
const {
37 return nsecSinceEpoch_ <= other.nsecSinceEpoch_;
40 bool operator > (SysTime
const& other)
const {
41 return nsecSinceEpoch_ > other.nsecSinceEpoch_;
44 bool operator >= (SysTime
const& other)
const {
45 return nsecSinceEpoch_ >= other.nsecSinceEpoch_;
47 bool operator == (SysTime
const& other)
const {
48 return nsecSinceEpoch_ == other.nsecSinceEpoch_;
51 bool operator != (SysTime
const& other)
const {
52 return nsecSinceEpoch_ != other.nsecSinceEpoch_;
55 Duration operator - (SysTime
const&)
const;
56 SysTime operator + (Duration
const&)
const;
57 SysTime operator - (Duration
const&)
const;
59 explicit operator bool()
const {
60 return nsecSinceEpoch_;
65 fromYYYYMMDDhhmmSSmmmUtc(int64_t v) {
67 v_tm.tm_year =
static_cast<int>(v/10000000000000l);
70 v_tm.tm_mon =
static_cast<int>(v/100000000000l);
73 v_tm.tm_mday =
static_cast<int>(v/1000000000l);
75 v_tm.tm_hour =
static_cast<int>(v/10000000l);
77 v_tm.tm_min =
static_cast<int>(v/100000l);
79 v_tm.tm_sec =
static_cast<int>(v/1000l);
82 return SysTime(timegm(&v_tm), v * 1000l);
87 fromYYYYMMDDhhmmSSmmm(int64_t v) {
89 v_tm.tm_year =
static_cast<int>(v/10000000000000l);
92 v_tm.tm_mon =
static_cast<int>(v/100000000000l);
95 v_tm.tm_mday =
static_cast<int>(v/1000000000l);
97 v_tm.tm_hour =
static_cast<int>(v/10000000l);
99 v_tm.tm_min =
static_cast<int>(v/100000l);
101 v_tm.tm_sec =
static_cast<int>(v/1000l);
104 return SysTime(mktime(&v_tm), v * 1000l);
107 void toXmitEndian() {
108 nsecSinceEpoch_ = Endian::toXmit(nsecSinceEpoch_);
111 void toNativeEndian() {
116 int64_t nsecSinceEpoch_;
117 friend std::ostream& operator << (std::ostream& os, SysTime
const& t);
121 std::ostream& operator << (std::ostream& os, SysTime
const& t) {
125 time_t sec = (time_t)(t.nsecSinceEpoch_ / 1000000000ll);
126 localtime_r(&sec, &ts);
127 strftime(buf,
sizeof(buf),
"%Y%m%d%H%M%S.", &ts);
128 sprintf(buf2,
"%09lld", t.nsecSinceEpoch_ % 1000000000ll);
137 static Duration milliseconds(int64_t msec) {
return Duration(0, msec * 1000);}
143 explicit Duration(int64_t sec, int64_t usec = 0, int64_t nsec = 0) {
144 nsec_ = sec * 1000000000l + usec * 1000l + nsec;}
146 int64_t milliseconds()
const {
return nsec_ / 1000000l; }
147 int64_t microseconds()
const {
return nsec_ / 1000l; }
148 int64_t nanoseconds()
const {
return nsec_; }
150 explicit operator bool()
const {
154 explicit operator double()
const {
155 return (
double)nsec_ / 1000000000.0;
158 bool operator < (
Duration const& other)
const {
return nsec_ < other.nsec_; }
159 bool operator > (
Duration const& other)
const {
return nsec_ > other.nsec_; }
160 bool operator == (
Duration const& other)
const {
return nsec_ == other.nsec_; }
161 bool operator != (
Duration const& other)
const {
return nsec_ != other.nsec_; }
162 bool operator >= (
Duration const& other)
const {
return nsec_ >= other.nsec_; }
163 bool operator <= (
Duration const& other)
const {
return nsec_ <= other.nsec_; }
166 nsec_ += other.nsec_;
171 return nanoseconds(-nsec_);
175 return nanoseconds(nsec_ - other.nsec_);
179 return nanoseconds(nsec_ + other.nsec_);
183 nsec_ -= other.nsec_;
186 double operator / (
Duration const& other)
const {
187 return (
double)nsec_ / other.nsec_;
190 Duration operator * (int64_t m)
const {
194 Duration operator / (int64_t
const& d)
const {
199 return Duration(0, 0, nsec_ % d.nsec_);
202 void toXmitEndian() {
203 nsec_ = Endian::toXmit(nsec_);
206 void toNativeEndian() {
212 friend std::ostream&
operator <<
213 (std::ostream& os,
Duration const& d);
214 friend std::istream&
operator >>
223 operator - (
SysTime const& b)
const {
224 return Duration::nanoseconds(nsecSinceEpoch_
225 - b.nsecSinceEpoch_);
231 operator + (Duration
const& d)
const {
232 return SysTime(0, 0, nsecSinceEpoch_ + d.nsec_);
238 operator - (Duration
const& d)
const {
239 return SysTime(0, 0, nsecSinceEpoch_ - d.nsec_);
245 sinceMidnight()
const {
246 return *
this - previousMidnight();
252 previousMidnight()
const {
254 time_t sec = (time_t)(nsecSinceEpoch_ / 1000000000l);
255 localtime_r(&sec, &ts);
256 return SysTime(sec) - Duration(ts.tm_hour * 3600 + ts.tm_min * 60 + ts.tm_sec);
261 operator += (SysTime & t, Duration
const& d) {
268 operator -= (SysTime & t, Duration
const& d) {
275 operator << (std::ostream& os, Duration
const& d) {
278 sprintf(buf,
"%09lld", d.nsec_ % 1000000000ll);
279 os << d.nsec_ / 1000000000l <<
'.' << buf;
281 sprintf(buf,
"%09lld", (-d.nsec_) % 1000000000ll);
282 os <<
'-' << (-d.nsec_) / 1000000000l <<
'.' << buf;
291 operator >> (std::istream& is, Duration& d) {
294 d.nsec_ = (int64_t)(t * 1000000000l);
302 struct numeric_limits<hmbdc::time::Duration> : numeric_limits<int64_t> {
305 {
return hmbdc::time::Duration::nanoseconds(numeric_limits<int64_t>::min());}
307 {
return hmbdc::time::Duration::nanoseconds(numeric_limits<int64_t>::max());}
SysTime(int64_t sec, int64_t usec=0, int64_t nsec=0)
UTC as input.
Definition: Time.hpp:18