Apollo 10.0
自动驾驶开放平台
time.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17#include "cyber/time/time.h"
18
19#include <chrono>
20#include <ctime>
21#include <iomanip>
22#include <limits>
23#include <sstream>
24#include <thread>
25
26namespace apollo {
27namespace cyber {
28
29using std::chrono::high_resolution_clock;
30using std::chrono::steady_clock;
31using std::chrono::system_clock;
32
33const Time Time::MAX = Time(std::numeric_limits<uint64_t>::max());
34const Time Time::MIN = Time(0);
35
36Time::Time(uint64_t nanoseconds) { nanoseconds_ = nanoseconds; }
37
38Time::Time(int nanoseconds) {
39 nanoseconds_ = static_cast<uint64_t>(nanoseconds);
40}
41
42Time::Time(double seconds) {
43 nanoseconds_ = static_cast<uint64_t>(seconds * 1000000000UL);
44}
45
46Time::Time(uint32_t seconds, uint32_t nanoseconds) {
47 nanoseconds_ = static_cast<uint64_t>(seconds) * 1000000000UL + nanoseconds;
48}
49
50Time::Time(const Time& other) { nanoseconds_ = other.nanoseconds_; }
51
52Time& Time::operator=(const Time& other) {
53 this->nanoseconds_ = other.nanoseconds_;
54 return *this;
55}
56
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();
62 uint64_t now_nano =
63 std::chrono::duration_cast<std::chrono::nanoseconds>(epoch).count();
64 return Time(now_nano);
65}
66
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();
72 uint64_t now_nano =
73 std::chrono::duration_cast<std::chrono::nanoseconds>(epoch).count();
74 return Time(now_nano);
75}
76
77double Time::ToSecond() const {
78 return static_cast<double>(nanoseconds_) / 1000000000UL;
79}
80
81bool Time::IsZero() const { return nanoseconds_ == 0; }
82
83uint64_t Time::ToNanosecond() const { return nanoseconds_; }
84
85uint64_t Time::ToMicrosecond() const {
86 return static_cast<uint64_t>(nanoseconds_ / 1000.0);
87}
88
89std::string Time::ToString() const {
90 auto nano = std::chrono::nanoseconds(nanoseconds_);
91 system_clock::time_point tp(nano);
92 auto time = system_clock::to_time_t(tp);
93 struct tm stm;
94 auto ret = localtime_r(&time, &stm);
95 if (ret == nullptr) {
96 return std::to_string(static_cast<double>(nanoseconds_) / 1000000000.0);
97 }
98
99 std::stringstream ss;
100#if __GNUC__ >= 5
101 ss << std::put_time(ret, "%F %T");
102 ss << "." << std::setw(9) << std::setfill('0') << nanoseconds_ % 1000000000UL;
103#else
104 char date_time[128];
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;
108#endif
109 return ss.str();
110}
111
112void Time::SleepUntil(const Time& time) {
113 auto nano = std::chrono::nanoseconds(time.ToNanosecond());
114 system_clock::time_point tp(nano);
115 std::this_thread::sleep_until(tp);
116}
117
118Duration Time::operator-(const Time& rhs) const {
119 return Duration(static_cast<int64_t>(nanoseconds_ - rhs.nanoseconds_));
120}
121
122Time Time::operator+(const Duration& rhs) const {
123 return Time(nanoseconds_ + rhs.ToNanosecond());
124}
125
126Time Time::operator-(const Duration& rhs) const {
127 return Time(nanoseconds_ - rhs.ToNanosecond());
128}
129
131 *this = *this + rhs;
132 return *this;
133}
134
136 *this = *this - rhs;
137 return *this;
138}
139
140bool Time::operator==(const Time& rhs) const {
141 return nanoseconds_ == rhs.nanoseconds_;
142}
143
144bool Time::operator!=(const Time& rhs) const {
145 return nanoseconds_ != rhs.nanoseconds_;
146}
147
148bool Time::operator>(const Time& rhs) const {
149 return nanoseconds_ > rhs.nanoseconds_;
150}
151
152bool Time::operator<(const Time& rhs) const {
153 return nanoseconds_ < rhs.nanoseconds_;
154}
155
156bool Time::operator>=(const Time& rhs) const {
157 return nanoseconds_ >= rhs.nanoseconds_;
158}
159
160bool Time::operator<=(const Time& rhs) const {
161 return nanoseconds_ <= rhs.nanoseconds_;
162}
163
164std::ostream& operator<<(std::ostream& os, const Time& rhs) {
165 os << rhs.ToString();
166 return os;
167}
168
169} // namespace cyber
170} // namespace apollo
int64_t ToNanosecond() const
Definition duration.cc:52
Cyber has builtin time type Time.
Definition time.h:31
bool operator>(const Time &rhs) const
Definition time.cc:148
uint64_t ToMicrosecond() const
convert time to microsecond (us).
Definition time.cc:85
bool operator<(const Time &rhs) const
Definition time.cc:152
Time & operator+=(const Duration &rhs)
Definition time.cc:130
Time operator+(const Duration &rhs) const
Definition time.cc:122
Duration operator-(const Time &rhs) const
Definition time.cc:118
static const Time MAX
Definition time.h:33
bool operator!=(const Time &rhs) const
Definition time.cc:144
bool IsZero() const
determine if time is 0
Definition time.cc:81
static void SleepUntil(const Time &time)
Sleep Until time.
Definition time.cc:112
bool operator<=(const Time &rhs) const
Definition time.cc:160
bool operator==(const Time &rhs) const
Definition time.cc:140
bool operator>=(const Time &rhs) const
Definition time.cc:156
Time & operator-=(const Duration &rhs)
Definition time.cc:135
static Time MonoTime()
Definition time.cc:67
static const Time MIN
Definition time.h:34
uint64_t ToNanosecond() const
convert time to nanosecond.
Definition time.cc:83
static Time Now()
get the current time.
Definition time.cc:57
std::string ToString() const
convert time to a string.
Definition time.cc:89
Time & operator=(const Time &other)
Definition time.cc:52
double ToSecond() const
convert time to second.
Definition time.cc:77
std::ostream & operator<<(std::ostream &os, const Duration &rhs)
Definition duration.cc:114
class register implement
Definition arena_queue.h:37