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);
133 explicit Duration(int64_t sec, int64_t usec = 0, int64_t nsec = 0) {
134 nsec_ = sec * 1000000000l + usec * 1000l + nsec;}
136 int64_t microseconds()
const {
return nsec_ / 1000l; }
137 int64_t nanoseconds()
const {
return nsec_; }
139 explicit operator bool()
const {
143 explicit operator double()
const {
144 return (
double)nsec_ / 1000000000.0;
147 bool operator < (
Duration const& other)
const {
return nsec_ < other.nsec_; }
148 bool operator > (
Duration const& other)
const {
return nsec_ > other.nsec_; }
149 bool operator == (
Duration const& other)
const {
return nsec_ == other.nsec_; }
150 bool operator >= (
Duration const& other)
const {
return nsec_ >= other.nsec_; }
151 bool operator <= (
Duration const& other)
const {
return nsec_ <= other.nsec_; }
154 nsec_ += other.nsec_;
159 return nanoseconds(-nsec_);
163 return nanoseconds(nsec_ - other.nsec_);
167 return nanoseconds(nsec_ + other.nsec_);
171 nsec_ -= other.nsec_;
174 double operator / (
Duration const& other)
const {
175 return (
double)nsec_ / other.nsec_;
178 Duration operator * (int64_t m)
const {
182 Duration operator / (int64_t
const& d)
const {
187 return Duration(0, 0, nsec_ % d.nsec_);
190 void toXmitEndian() {
191 nsec_ = Endian::toXmit(nsec_);
194 void toNativeEndian() {
200 friend std::ostream&
operator <<
201 (std::ostream& os,
Duration const& d);
202 friend std::istream&
operator >>
211 operator - (
SysTime const& b)
const {
212 return Duration::nanoseconds(nsecSinceEpoch_
213 - b.nsecSinceEpoch_);
219 operator + (
Duration const& d)
const {
220 return SysTime(0, 0, nsecSinceEpoch_ + d.nsec_);
226 operator - (
Duration const& d)
const {
227 return SysTime(0, 0, nsecSinceEpoch_ - d.nsec_);
233 sinceMidnight()
const {
234 return *
this - previousMidnight();
240 previousMidnight()
const {
242 time_t sec = (time_t)(nsecSinceEpoch_ / 1000000000l);
243 localtime_r(&sec, &ts);
244 return SysTime(sec) -
Duration(ts.tm_hour * 3600 + ts.tm_min * 60 + ts.tm_sec);
263 operator << (std::ostream& os,
Duration const& d) {
266 sprintf(buf,
"%09lld", d.nsec_ % 1000000000ll);
267 os << d.nsec_ / 1000000000l <<
'.' << buf;
269 sprintf(buf,
"%09lld", (-d.nsec_) % 1000000000ll);
270 os <<
'-' << (-d.nsec_) / 1000000000l <<
'.' << buf;
279 operator >> (std::istream& is,
Duration& d) {
282 d.nsec_ = (int64_t)(t * 1000000000l);
290 struct numeric_limits<
hmbdc::time::Duration> : numeric_limits<int64_t> {
293 {
return hmbdc::time::Duration::nanoseconds(numeric_limits<int64_t>::min());}
295 {
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