1 | /** |
2 | * Copyright 2021 Alibaba, Inc. and its affiliates. 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 | * \author Hechong.xyf |
17 | * \date Dec 2017 |
18 | * \brief Interface of AiLego Utility Time Helper |
19 | */ |
20 | |
21 | #ifndef __AILEGO_UTILITY_TIME_HELPER_H__ |
22 | #define __AILEGO_UTILITY_TIME_HELPER_H__ |
23 | |
24 | #include <string> |
25 | #include <ailego/internal/platform.h> |
26 | |
27 | namespace ailego { |
28 | |
29 | /*! Monotime |
30 | */ |
31 | struct Monotime { |
32 | //! Retrieve monotonic time in nanoseconds |
33 | static uint64_t NanoSeconds(void); |
34 | |
35 | //! Retrieve monotonic time in microseconds |
36 | static uint64_t MicroSeconds(void); |
37 | |
38 | //! Retrieve monotonic time in milliseconds |
39 | static uint64_t MilliSeconds(void); |
40 | |
41 | //! Retrieve monotonic time in seconds |
42 | static uint64_t Seconds(void); |
43 | }; |
44 | |
45 | /*! Realtime |
46 | */ |
47 | struct Realtime { |
48 | //! Retrieve system time in nanoseconds |
49 | static uint64_t NanoSeconds(void); |
50 | |
51 | //! Retrieve system time in microseconds |
52 | static uint64_t MicroSeconds(void); |
53 | |
54 | //! Retrieve system time in milliseconds |
55 | static uint64_t MilliSeconds(void); |
56 | |
57 | //! Retrieve system time in seconds |
58 | static uint64_t Seconds(void); |
59 | |
60 | //! Retrieve a timestamp as a specific local time format |
61 | static size_t Localtime(uint64_t stamp, const char *format, char *buf, |
62 | size_t len); |
63 | |
64 | //! Retrieve a timestamp as a specific GMT time format |
65 | static size_t Gmtime(uint64_t stamp, const char *format, char *buf, |
66 | size_t len); |
67 | |
68 | //! Retrieve local time in string |
69 | static size_t Localtime(const char *format, char *buf, size_t len); |
70 | |
71 | //! Retrieve GMT time in string |
72 | static size_t Gmtime(const char *format, char *buf, size_t len); |
73 | |
74 | //! Retrieve local time in string |
75 | static size_t Localtime(char *buf, size_t len) { |
76 | return Localtime("%Y-%m-%d %H:%M:%S" , buf, len); |
77 | } |
78 | |
79 | //! Retrieve GMT time in string |
80 | static size_t Gmtime(char *buf, size_t len) { |
81 | return Gmtime("%Y-%m-%d %H:%M:%S" , buf, len); |
82 | } |
83 | |
84 | //! Retrieve local time in string |
85 | static std::string Localtime(void) { |
86 | char str[32]; |
87 | Localtime(str, sizeof(str)); |
88 | return std::string(str); |
89 | } |
90 | |
91 | //! Retrieve GMT time in string |
92 | static std::string Gmtime(void) { |
93 | char str[32]; |
94 | Gmtime(str, sizeof(str)); |
95 | return std::string(str); |
96 | } |
97 | |
98 | //! Retrieve a timestamp as a specific local time format |
99 | static size_t Localtime(uint64_t stamp, char *buf, size_t len) { |
100 | return Localtime(stamp, "%Y-%m-%d %H:%M:%S" , buf, len); |
101 | } |
102 | |
103 | //! Retrieve a timestamp as a specific GMT time format |
104 | static size_t Gmtime(uint64_t stamp, char *buf, size_t len) { |
105 | return Gmtime(stamp, "%Y-%m-%d %H:%M:%S" , buf, len); |
106 | } |
107 | |
108 | //! Retrieve a timestamp as a specific local time format |
109 | static std::string Localtime(uint64_t stamp) { |
110 | char str[32]; |
111 | Localtime(stamp, str, sizeof(str)); |
112 | return std::string(str); |
113 | } |
114 | |
115 | //! Retrieve a timestamp as a specific GMT time format |
116 | static std::string Gmtime(uint64_t stamp) { |
117 | char str[32]; |
118 | Gmtime(stamp, str, sizeof(str)); |
119 | return std::string(str); |
120 | } |
121 | }; |
122 | |
123 | /*! Elapsed Time |
124 | */ |
125 | class ElapsedTime { |
126 | public: |
127 | //! Constructor |
128 | ElapsedTime(void) : stamp_(Monotime::NanoSeconds()) {} |
129 | |
130 | //! Retrieve the elapsed time in nanoseconds |
131 | uint64_t nano_seconds(void) const { |
132 | return (Monotime::NanoSeconds() - stamp_); |
133 | } |
134 | |
135 | //! Retrieve the elapsed time in milliseconds |
136 | uint64_t micro_seconds(void) const { |
137 | return (this->nano_seconds() / 1000u); |
138 | } |
139 | |
140 | //! Retrieve the elapsed time in milliseconds |
141 | uint64_t milli_seconds(void) const { |
142 | return (this->nano_seconds() / 1000000u); |
143 | } |
144 | |
145 | //! Retrieve the elapsed time in seconds |
146 | uint64_t seconds(void) const { |
147 | return (this->nano_seconds() / 1000000000u); |
148 | } |
149 | |
150 | //! Update time stamp |
151 | void reset(void) { |
152 | stamp_ = Monotime::NanoSeconds(); |
153 | } |
154 | |
155 | private: |
156 | uint64_t stamp_; |
157 | }; |
158 | |
159 | } // namespace ailego |
160 | |
161 | #endif // __AILEGO_UTILITY_TIME_HELPER_H__ |
162 | |