29using std::chrono::high_resolution_clock;
30using std::chrono::steady_clock;
31using std::chrono::system_clock;
36Time::Time(uint64_t nanoseconds) { nanoseconds_ = nanoseconds; }
39 nanoseconds_ =
static_cast<uint64_t
>(nanoseconds);
43 nanoseconds_ =
static_cast<uint64_t
>(seconds * 1000000000UL);
47 nanoseconds_ =
static_cast<uint64_t
>(seconds) * 1000000000UL + nanoseconds;
53 this->nanoseconds_ = other.nanoseconds_;
58 auto now = high_resolution_clock::now();
59 auto nano_time_point =
60 std::chrono::time_point_cast<std::chrono::nanoseconds>(now);
61 auto epoch = nano_time_point.time_since_epoch();
63 std::chrono::duration_cast<std::chrono::nanoseconds>(epoch).count();
64 return Time(now_nano);
68 auto now = steady_clock::now();
69 auto nano_time_point =
70 std::chrono::time_point_cast<std::chrono::nanoseconds>(now);
71 auto epoch = nano_time_point.time_since_epoch();
73 std::chrono::duration_cast<std::chrono::nanoseconds>(epoch).count();
74 return Time(now_nano);
78 return static_cast<double>(nanoseconds_) / 1000000000UL;
86 return static_cast<uint64_t
>(nanoseconds_ / 1000.0);
90 auto nano = std::chrono::nanoseconds(nanoseconds_);
91 system_clock::time_point tp(nano);
92 auto time = system_clock::to_time_t(tp);
94 auto ret = localtime_r(&time, &stm);
96 return std::to_string(
static_cast<double>(nanoseconds_) / 1000000000.0);
101 ss << std::put_time(ret,
"%F %T");
102 ss <<
"." << std::setw(9) << std::setfill(
'0') << nanoseconds_ % 1000000000UL;
105 strftime(date_time,
sizeof(date_time),
"%F %T", ret);
106 ss << std::string(date_time) <<
"." << std::setw(9) << std::setfill(
'0')
107 << nanoseconds_ % 1000000000UL;
113 auto nano = std::chrono::nanoseconds(time.
ToNanosecond());
114 system_clock::time_point tp(nano);
115 std::this_thread::sleep_until(tp);
119 return Duration(
static_cast<int64_t
>(nanoseconds_ - rhs.nanoseconds_));
141 return nanoseconds_ == rhs.nanoseconds_;
145 return nanoseconds_ != rhs.nanoseconds_;
149 return nanoseconds_ > rhs.nanoseconds_;
153 return nanoseconds_ < rhs.nanoseconds_;
157 return nanoseconds_ >= rhs.nanoseconds_;
161 return nanoseconds_ <= rhs.nanoseconds_;
int64_t ToNanosecond() const
Cyber has builtin time type Time.
bool operator>(const Time &rhs) const
uint64_t ToMicrosecond() const
convert time to microsecond (us).
bool operator<(const Time &rhs) const
Time & operator+=(const Duration &rhs)
Time operator+(const Duration &rhs) const
Duration operator-(const Time &rhs) const
bool operator!=(const Time &rhs) const
bool IsZero() const
determine if time is 0
static void SleepUntil(const Time &time)
Sleep Until time.
bool operator<=(const Time &rhs) const
bool operator==(const Time &rhs) const
bool operator>=(const Time &rhs) const
Time & operator-=(const Duration &rhs)
uint64_t ToNanosecond() const
convert time to nanosecond.
static Time Now()
get the current time.
std::string ToString() const
convert time to a string.
Time & operator=(const Time &other)
double ToSecond() const
convert time to second.
std::ostream & operator<<(std::ostream &os, const Duration &rhs)