1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <cstdio>
17 #include <cstdlib>
18 #include <ctime>
19 #include <string>
20 #include <vector>
21
22 #define TOSTRING(x) #x
23
24 struct ClockTime {
25 std::string name;
26 clockid_t clockId;
27 struct timespec ts;
28 };
29
main()30 int main()
31 {
32 std::vector<ClockTime> times = {
33 { TOSTRING(CLOCK_REALTIME), CLOCK_REALTIME, { 0 } },
34 { TOSTRING(CLOCK_REALTIME_ALARM), CLOCK_REALTIME_ALARM, { 0 } },
35 { TOSTRING(CLOCK_REALTIME_COARSE), CLOCK_REALTIME_COARSE, { 0 } },
36 { TOSTRING(CLOCK_MONOTONIC), CLOCK_MONOTONIC, { 0 } },
37 { TOSTRING(CLOCK_MONOTONIC_COARSE), CLOCK_MONOTONIC_COARSE, { 0 } },
38 { TOSTRING(CLOCK_MONOTONIC_RAW), CLOCK_MONOTONIC_RAW, { 0 } },
39 { TOSTRING(CLOCK_BOOTTIME), CLOCK_BOOTTIME, { 0 } },
40 { TOSTRING(CLOCK_BOOTTIME_ALARM), CLOCK_BOOTTIME_ALARM, { 0 } },
41 };
42
43 for (auto& time : times) {
44 clock_gettime(time.clockId, &time.ts);
45 }
46
47 for (auto& time : times) {
48 printf("%-25s: %10lld.%09ld\n", time.name.c_str(),
49 static_cast<long long>(time.ts.tv_sec), time.ts.tv_nsec);
50 }
51 fflush(stdout);
52 return 0;
53 }