1 #include "hmbdc/Copyright.hpp" 11 namespace hmbdc {
namespace time {
12 using namespace hmbdc;
17 SysTime() : nsecSinceEpoch_(0){}
19 explicit SysTime(int64_t sec, int64_t usec = 0, int64_t nsec = 0) {
20 nsecSinceEpoch_ = sec * 1000000000l + usec*1000l + nsec;
25 clock_gettime(CLOCK_REALTIME, &spec);
26 return SysTime(spec.tv_sec, 0, spec.tv_nsec);
29 int64_t usecSinceEpoch()
const {
return nsecSinceEpoch_ / 1000l; }
31 SysTime previousMidnight()
const;
33 bool operator < (
SysTime const& other)
const {
34 return nsecSinceEpoch_ < other.nsecSinceEpoch_;
37 bool operator <= (
SysTime const& other)
const {
38 return nsecSinceEpoch_ <= other.nsecSinceEpoch_;
41 bool operator > (
SysTime const& other)
const {
42 return nsecSinceEpoch_ > other.nsecSinceEpoch_;
45 bool operator >= (
SysTime const& other)
const {
46 return nsecSinceEpoch_ >= other.nsecSinceEpoch_;
48 bool operator == (
SysTime const& other)
const {
49 return nsecSinceEpoch_ == other.nsecSinceEpoch_;
58 fromYYYYMMDDhhmmSSmmmUtc(int64_t v) {
60 v_tm.tm_year =
static_cast<int>(v/10000000000000l);
63 v_tm.tm_mon =
static_cast<int>(v/100000000000l);
66 v_tm.tm_mday =
static_cast<int>(v/1000000000l);
68 v_tm.tm_hour =
static_cast<int>(v/10000000l);
70 v_tm.tm_min =
static_cast<int>(v/100000l);
72 v_tm.tm_sec =
static_cast<int>(v/1000l);
75 return SysTime(timegm(&v_tm), v * 1000l);
80 fromYYYYMMDDhhmmSSmmm(int64_t v) {
82 v_tm.tm_year =
static_cast<int>(v/10000000000000l);
85 v_tm.tm_mon =
static_cast<int>(v/100000000000l);
88 v_tm.tm_mday =
static_cast<int>(v/1000000000l);
90 v_tm.tm_hour =
static_cast<int>(v/10000000l);
92 v_tm.tm_min =
static_cast<int>(v/100000l);
94 v_tm.tm_sec =
static_cast<int>(v/1000l);
97 return SysTime(mktime(&v_tm), v * 1000l);
101 int64_t nsecSinceEpoch_;
102 friend std::ostream& operator << (std::ostream& os,
SysTime const& t);
106 std::ostream& operator << (std::ostream& os,
SysTime const& t) {
110 time_t sec = (time_t)(t.nsecSinceEpoch_ / 1000000000l);
111 localtime_r(&sec, &ts);
112 strftime(buf,
sizeof(buf),
"%Y%m%d%H%M%S.", &ts);
113 sprintf(buf2,
"%09ld", t.nsecSinceEpoch_ % 1000000000l);
126 explicit Duration(int64_t sec, int64_t usec = 0, int64_t nsec = 0) {
127 nsec_ = sec * 1000000000l + usec * 1000l + nsec;}
129 int64_t microseconds()
const {
return nsec_ / 1000l; }
130 int64_t nanoseconds()
const {
return nsec_; }
132 explicit operator bool()
const {
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 bool operator >= (
Duration const& other)
const {
return nsec_ >= other.nsec_; }
140 bool operator <= (
Duration const& other)
const {
return nsec_ <= other.nsec_; }
143 nsec_ += other.nsec_;
148 return nanoseconds(-nsec_);
152 return nanoseconds(nsec_ - other.nsec_);
156 return nanoseconds(nsec_ + other.nsec_);
160 nsec_ -= other.nsec_;
163 double operator / (
Duration const& other)
const {
164 return (
double)nsec_ / other.nsec_;
167 Duration operator * (int64_t m)
const {
171 Duration operator / (int64_t
const& d)
const {
176 return Duration(0, 0, nsec_ % d.nsec_);
181 friend std::ostream&
operator <<
182 (std::ostream& os,
Duration const& d);
183 friend std::istream&
operator >>
192 operator - (
SysTime const& b)
const {
193 return Duration::nanoseconds(nsecSinceEpoch_
194 - b.nsecSinceEpoch_);
200 operator + (
Duration const& d)
const {
201 return SysTime(0, 0, nsecSinceEpoch_ + d.nsec_);
207 operator - (
Duration const& d)
const {
208 return SysTime(0, 0, nsecSinceEpoch_ - d.nsec_);
214 sinceMidnight()
const {
215 return *
this - previousMidnight();
221 previousMidnight()
const {
223 time_t sec = (time_t)(nsecSinceEpoch_ / 1000000000l);
224 localtime_r(&sec, &ts);
225 return SysTime(sec) -
Duration(ts.tm_hour * 3600 + ts.tm_min * 60 + ts.tm_sec);
244 operator << (std::ostream& os,
Duration const& d) {
247 sprintf(buf,
"%09ld", d.nsec_ % 1000000000l);
248 os << d.nsec_ / 1000000000l <<
'.' << buf;
250 sprintf(buf,
"%09ld", (-d.nsec_) % 1000000000l);
251 os <<
'-' << (-d.nsec_) / 1000000000l <<
'.' << buf;
260 operator >> (std::istream& is,
Duration& d) {
263 d.nsec_ = (int64_t)(t * 1000000000l);
272 struct numeric_limits<
Duration> : numeric_limits<int64_t> {
275 {
return Duration::nanoseconds(numeric_limits<int64_t>::min());}
277 {
return 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:19
Definition: Client.hpp:11