1 #include "hmbdc/Copyright.hpp" 11 namespace hmbdc {
namespace time {
15 SysTime() : nsecSinceEpoch_(0){}
17 explicit SysTime(int64_t sec, int64_t usec = 0, int64_t nsec = 0) {
18 nsecSinceEpoch_ = sec * 1000000000l + usec*1000l + nsec;
23 clock_gettime(CLOCK_REALTIME, &spec);
24 return SysTime(spec.tv_sec, 0, spec.tv_nsec);
27 int64_t usecSinceEpoch()
const {
return nsecSinceEpoch_ / 1000l; }
29 SysTime previousMidnight()
const;
31 bool operator < (
SysTime const& other)
const {
32 return nsecSinceEpoch_ < other.nsecSinceEpoch_;
35 bool operator <= (
SysTime const& other)
const {
36 return nsecSinceEpoch_ <= other.nsecSinceEpoch_;
39 bool operator > (
SysTime const& other)
const {
40 return nsecSinceEpoch_ > other.nsecSinceEpoch_;
43 bool operator >= (
SysTime const& other)
const {
44 return nsecSinceEpoch_ >= other.nsecSinceEpoch_;
46 bool operator == (
SysTime const& other)
const {
47 return nsecSinceEpoch_ == other.nsecSinceEpoch_;
56 fromYYYYMMDDhhmmSSmmmUtc(int64_t v) {
58 v_tm.tm_year =
static_cast<int>(v/10000000000000l);
61 v_tm.tm_mon =
static_cast<int>(v/100000000000l);
64 v_tm.tm_mday =
static_cast<int>(v/1000000000l);
66 v_tm.tm_hour =
static_cast<int>(v/10000000l);
68 v_tm.tm_min =
static_cast<int>(v/100000l);
70 v_tm.tm_sec =
static_cast<int>(v/1000l);
73 return SysTime(timegm(&v_tm), v * 1000l);
78 fromYYYYMMDDhhmmSSmmm(int64_t v) {
80 v_tm.tm_year =
static_cast<int>(v/10000000000000l);
83 v_tm.tm_mon =
static_cast<int>(v/100000000000l);
86 v_tm.tm_mday =
static_cast<int>(v/1000000000l);
88 v_tm.tm_hour =
static_cast<int>(v/10000000l);
90 v_tm.tm_min =
static_cast<int>(v/100000l);
92 v_tm.tm_sec =
static_cast<int>(v/1000l);
95 return SysTime(mktime(&v_tm), v * 1000l);
99 int64_t nsecSinceEpoch_;
100 friend std::ostream& operator << (std::ostream& os,
SysTime const& t);
104 std::ostream& operator << (std::ostream& os,
SysTime const& t) {
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);
124 explicit Duration(int64_t sec, int64_t usec = 0, int64_t nsec = 0) {
125 nsec_ = sec * 1000000000l + usec * 1000l + nsec;}
127 int64_t microseconds()
const {
return nsec_ / 1000l; }
128 int64_t nanoseconds()
const {
return nsec_; }
130 explicit operator bool()
const {
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_; }
141 nsec_ += other.nsec_;
146 return nanoseconds(-nsec_);
150 return nanoseconds(nsec_ - other.nsec_);
154 return nanoseconds(nsec_ + other.nsec_);
158 nsec_ -= other.nsec_;
161 double operator / (
Duration const& other)
const {
162 return (
double)nsec_ / other.nsec_;
165 Duration operator * (int64_t m)
const {
169 Duration operator / (int64_t
const& d)
const {
174 return Duration(0, 0, nsec_ % d.nsec_);
179 friend std::ostream&
operator <<
180 (std::ostream& os,
Duration const& d);
181 friend std::istream&
operator >>
190 operator - (
SysTime const& b)
const {
191 return Duration::nanoseconds(nsecSinceEpoch_
192 - b.nsecSinceEpoch_);
198 operator + (
Duration const& d)
const {
199 return SysTime(0, 0, nsecSinceEpoch_ + d.nsec_);
205 operator - (
Duration const& d)
const {
206 return SysTime(0, 0, nsecSinceEpoch_ - d.nsec_);
212 sinceMidnight()
const {
213 return *
this - previousMidnight();
219 previousMidnight()
const {
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);
242 operator << (std::ostream& os,
Duration const& d) {
245 sprintf(buf,
"%09ld", d.nsec_ % 1000000000l);
246 os << d.nsec_ / 1000000000l <<
'.' << buf;
248 sprintf(buf,
"%09ld", (-d.nsec_) % 1000000000l);
249 os <<
'-' << (-d.nsec_) / 1000000000l <<
'.' << buf;
258 operator >> (std::istream& is,
Duration& d) {
261 d.nsec_ = (int64_t)(t * 1000000000l);
269 struct numeric_limits<
hmbdc::time::Duration> : numeric_limits<int64_t> {
272 {
return hmbdc::time::Duration::nanoseconds(numeric_limits<int64_t>::min());}
274 {
return hmbdc::time::Duration::nanoseconds(numeric_limits<int64_t>::max());}
Definition: TypedString.hpp:74
SysTime(int64_t sec, int64_t usec=0, int64_t nsec=0)
UTC as input.
Definition: Time.hpp:17