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; }
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_;
57 fromYYYYMMDDhhmmSSmmmUtc(int64_t v) {
59 v_tm.tm_year =
static_cast<int>(v/10000000000000l);
62 v_tm.tm_mon =
static_cast<int>(v/100000000000l);
65 v_tm.tm_mday =
static_cast<int>(v/1000000000l);
67 v_tm.tm_hour =
static_cast<int>(v/10000000l);
69 v_tm.tm_min =
static_cast<int>(v/100000l);
71 v_tm.tm_sec =
static_cast<int>(v/1000l);
74 return SysTime(timegm(&v_tm), v * 1000l);
79 fromYYYYMMDDhhmmSSmmm(int64_t v) {
81 v_tm.tm_year =
static_cast<int>(v/10000000000000l);
84 v_tm.tm_mon =
static_cast<int>(v/100000000000l);
87 v_tm.tm_mday =
static_cast<int>(v/1000000000l);
89 v_tm.tm_hour =
static_cast<int>(v/10000000l);
91 v_tm.tm_min =
static_cast<int>(v/100000l);
93 v_tm.tm_sec =
static_cast<int>(v/1000l);
96 return SysTime(mktime(&v_tm), v * 1000l);
100 nsecSinceEpoch_ = Endian::toXmit(nsecSinceEpoch_);
103 void toNativeEndian() {
108 int64_t nsecSinceEpoch_;
109 friend std::ostream& operator << (std::ostream& os,
SysTime const& t);
113 std::ostream& operator << (std::ostream& os,
SysTime const& t) {
117 time_t sec = (time_t)(t.nsecSinceEpoch_ / 1000000000ll);
118 localtime_r(&sec, &ts);
119 strftime(buf,
sizeof(buf),
"%Y%m%d%H%M%S.", &ts);
120 sprintf(buf2,
"%09lld", t.nsecSinceEpoch_ % 1000000000ll);
129 static Duration milliseconds(int64_t msec) {
return Duration(0, msec * 1000);}
135 explicit Duration(int64_t sec, int64_t usec = 0, int64_t nsec = 0) {
136 nsec_ = sec * 1000000000l + usec * 1000l + nsec;}
138 int64_t milliseconds()
const {
return nsec_ / 1000000l; }
139 int64_t microseconds()
const {
return nsec_ / 1000l; }
140 int64_t nanoseconds()
const {
return nsec_; }
142 explicit operator bool()
const {
146 explicit operator double()
const {
147 return (
double)nsec_ / 1000000000.0;
150 bool operator < (
Duration const& other)
const {
return nsec_ < other.nsec_; }
151 bool operator > (
Duration const& other)
const {
return nsec_ > other.nsec_; }
152 bool operator == (
Duration const& other)
const {
return nsec_ == other.nsec_; }
153 bool operator >= (
Duration const& other)
const {
return nsec_ >= other.nsec_; }
154 bool operator <= (
Duration const& other)
const {
return nsec_ <= other.nsec_; }
157 nsec_ += other.nsec_;
162 return nanoseconds(-nsec_);
166 return nanoseconds(nsec_ - other.nsec_);
170 return nanoseconds(nsec_ + other.nsec_);
174 nsec_ -= other.nsec_;
177 double operator / (
Duration const& other)
const {
178 return (
double)nsec_ / other.nsec_;
181 Duration operator * (int64_t m)
const {
185 Duration operator / (int64_t
const& d)
const {
190 return Duration(0, 0, nsec_ % d.nsec_);
193 void toXmitEndian() {
194 nsec_ = Endian::toXmit(nsec_);
197 void toNativeEndian() {
203 friend std::ostream&
operator <<
204 (std::ostream& os,
Duration const& d);
205 friend std::istream&
operator >>
214 operator - (
SysTime const& b)
const {
215 return Duration::nanoseconds(nsecSinceEpoch_
216 - b.nsecSinceEpoch_);
222 operator + (
Duration const& d)
const {
223 return SysTime(0, 0, nsecSinceEpoch_ + d.nsec_);
229 operator - (
Duration const& d)
const {
230 return SysTime(0, 0, nsecSinceEpoch_ - d.nsec_);
236 sinceMidnight()
const {
237 return *
this - previousMidnight();
243 previousMidnight()
const {
245 time_t sec = (time_t)(nsecSinceEpoch_ / 1000000000l);
246 localtime_r(&sec, &ts);
247 return SysTime(sec) -
Duration(ts.tm_hour * 3600 + ts.tm_min * 60 + ts.tm_sec);
266 operator << (std::ostream& os,
Duration const& d) {
269 sprintf(buf,
"%09lld", d.nsec_ % 1000000000ll);
270 os << d.nsec_ / 1000000000l <<
'.' << buf;
272 sprintf(buf,
"%09lld", (-d.nsec_) % 1000000000ll);
273 os <<
'-' << (-d.nsec_) / 1000000000l <<
'.' << buf;
282 operator >> (std::istream& is,
Duration& d) {
285 d.nsec_ = (int64_t)(t * 1000000000l);
293 struct numeric_limits<
hmbdc::time::Duration> : numeric_limits<int64_t> {
296 {
return hmbdc::time::Duration::nanoseconds(numeric_limits<int64_t>::min());}
298 {
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:18