Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-or-later |
2 | | /* |
3 | | * PIM for Quagga |
4 | | * Copyright (C) 2008 Everton da Silva Marques |
5 | | */ |
6 | | |
7 | | #include <zebra.h> |
8 | | |
9 | | #include <string.h> |
10 | | #include <sys/time.h> |
11 | | #include <time.h> |
12 | | |
13 | | #include "log.h" |
14 | | #include "frrevent.h" |
15 | | #include "lib_errors.h" |
16 | | |
17 | | #include "pim_time.h" |
18 | | |
19 | | static int gettime_monotonic(struct timeval *tv) |
20 | 247k | { |
21 | 247k | int result; |
22 | | |
23 | 247k | result = gettimeofday(tv, 0); |
24 | 247k | if (result) { |
25 | 0 | flog_err_sys(EC_LIB_SYSTEM_CALL, |
26 | 0 | "%s: gettimeofday() failure: errno=%d: %s", |
27 | 0 | __func__, errno, safe_strerror(errno)); |
28 | 0 | } |
29 | | |
30 | 247k | return result; |
31 | 247k | } |
32 | | |
33 | | /* |
34 | | pim_time_monotonic_sec(): |
35 | | number of seconds since some unspecified starting point |
36 | | */ |
37 | | int64_t pim_time_monotonic_sec(void) |
38 | 246k | { |
39 | 246k | struct timeval now_tv; |
40 | | |
41 | 246k | if (gettime_monotonic(&now_tv)) { |
42 | 0 | flog_err_sys(EC_LIB_SYSTEM_CALL, |
43 | 0 | "%s: gettime_monotonic() failure: errno=%d: %s", |
44 | 0 | __func__, errno, safe_strerror(errno)); |
45 | 0 | return -1; |
46 | 0 | } |
47 | | |
48 | 246k | return now_tv.tv_sec; |
49 | 246k | } |
50 | | |
51 | | /* |
52 | | pim_time_monotonic_dsec(): |
53 | | number of deciseconds since some unspecified starting point |
54 | | */ |
55 | | int64_t pim_time_monotonic_dsec(void) |
56 | 0 | { |
57 | 0 | struct timeval now_tv; |
58 | 0 | int64_t now_dsec; |
59 | |
|
60 | 0 | if (gettime_monotonic(&now_tv)) { |
61 | 0 | flog_err_sys(EC_LIB_SYSTEM_CALL, |
62 | 0 | "%s: gettime_monotonic() failure: errno=%d: %s", |
63 | 0 | __func__, errno, safe_strerror(errno)); |
64 | 0 | return -1; |
65 | 0 | } |
66 | | |
67 | 0 | now_dsec = ((int64_t)now_tv.tv_sec) * 10 |
68 | 0 | + ((int64_t)now_tv.tv_usec) / 100000; |
69 | |
|
70 | 0 | return now_dsec; |
71 | 0 | } |
72 | | |
73 | | int64_t pim_time_monotonic_usec(void) |
74 | 963 | { |
75 | 963 | struct timeval now_tv; |
76 | 963 | int64_t now_dsec; |
77 | | |
78 | 963 | if (gettime_monotonic(&now_tv)) { |
79 | 0 | flog_err_sys(EC_LIB_SYSTEM_CALL, |
80 | 0 | "%s: gettime_monotonic() failure: errno=%d: %s", |
81 | 0 | __func__, errno, safe_strerror(errno)); |
82 | 0 | return -1; |
83 | 0 | } |
84 | | |
85 | 963 | now_dsec = |
86 | 963 | ((int64_t)now_tv.tv_sec) * 1000000 + ((int64_t)now_tv.tv_usec); |
87 | | |
88 | 963 | return now_dsec; |
89 | 963 | } |
90 | | |
91 | | int pim_time_mmss(char *buf, int buf_size, long sec) |
92 | 0 | { |
93 | 0 | long mm; |
94 | 0 | int wr; |
95 | |
|
96 | 0 | assert(buf_size >= 5); |
97 | |
|
98 | 0 | mm = sec / 60; |
99 | 0 | sec %= 60; |
100 | |
|
101 | 0 | wr = snprintf(buf, buf_size, "%02ld:%02ld", mm, sec); |
102 | |
|
103 | 0 | return wr != 8; |
104 | 0 | } |
105 | | |
106 | | static int pim_time_hhmmss(char *buf, int buf_size, long sec) |
107 | 0 | { |
108 | 0 | long hh; |
109 | 0 | long mm; |
110 | 0 | int wr; |
111 | |
|
112 | 0 | assert(buf_size >= 8); |
113 | |
|
114 | 0 | hh = sec / 3600; |
115 | 0 | sec %= 3600; |
116 | 0 | mm = sec / 60; |
117 | 0 | sec %= 60; |
118 | |
|
119 | 0 | wr = snprintf(buf, buf_size, "%02ld:%02ld:%02ld", hh, mm, sec); |
120 | |
|
121 | 0 | return wr != 8; |
122 | 0 | } |
123 | | |
124 | | void pim_time_timer_to_mmss(char *buf, int buf_size, struct event *t_timer) |
125 | 0 | { |
126 | 0 | if (t_timer) { |
127 | 0 | pim_time_mmss(buf, buf_size, |
128 | 0 | event_timer_remain_second(t_timer)); |
129 | 0 | } else { |
130 | 0 | snprintf(buf, buf_size, "--:--"); |
131 | 0 | } |
132 | 0 | } |
133 | | |
134 | | void pim_time_timer_to_hhmmss(char *buf, int buf_size, struct event *t_timer) |
135 | 0 | { |
136 | 0 | if (t_timer) { |
137 | 0 | pim_time_hhmmss(buf, buf_size, |
138 | 0 | event_timer_remain_second(t_timer)); |
139 | 0 | } else { |
140 | 0 | snprintf(buf, buf_size, "--:--:--"); |
141 | 0 | } |
142 | 0 | } |
143 | | |
144 | | void pim_time_uptime(char *buf, int buf_size, int64_t uptime_sec) |
145 | 0 | { |
146 | 0 | assert(buf_size >= 8); |
147 | |
|
148 | 0 | pim_time_hhmmss(buf, buf_size, uptime_sec); |
149 | 0 | } |
150 | | |
151 | | void pim_time_uptime_begin(char *buf, int buf_size, int64_t now, int64_t begin) |
152 | 0 | { |
153 | 0 | if (begin > 0) |
154 | 0 | pim_time_uptime(buf, buf_size, now - begin); |
155 | 0 | else |
156 | 0 | snprintf(buf, buf_size, "--:--:--"); |
157 | 0 | } |
158 | | |
159 | | long pim_time_timer_remain_msec(struct event *t_timer) |
160 | 0 | { |
161 | | /* no timer thread running means timer has expired: return 0 */ |
162 | |
|
163 | 0 | return t_timer ? event_timer_remain_msec(t_timer) : 0; |
164 | 0 | } |