]>
git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_time.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 2008 Everton da Silva Marques
15 #include "lib_errors.h"
19 static int gettime_monotonic(struct timeval
*tv
)
23 result
= gettimeofday(tv
, 0);
25 flog_err_sys(EC_LIB_SYSTEM_CALL
,
26 "%s: gettimeofday() failure: errno=%d: %s",
27 __func__
, errno
, safe_strerror(errno
));
34 pim_time_monotonic_sec():
35 number of seconds since some unspecified starting point
37 int64_t pim_time_monotonic_sec(void)
39 struct timeval now_tv
;
41 if (gettime_monotonic(&now_tv
)) {
42 flog_err_sys(EC_LIB_SYSTEM_CALL
,
43 "%s: gettime_monotonic() failure: errno=%d: %s",
44 __func__
, errno
, safe_strerror(errno
));
52 pim_time_monotonic_dsec():
53 number of deciseconds since some unspecified starting point
55 int64_t pim_time_monotonic_dsec(void)
57 struct timeval now_tv
;
60 if (gettime_monotonic(&now_tv
)) {
61 flog_err_sys(EC_LIB_SYSTEM_CALL
,
62 "%s: gettime_monotonic() failure: errno=%d: %s",
63 __func__
, errno
, safe_strerror(errno
));
67 now_dsec
= ((int64_t)now_tv
.tv_sec
) * 10
68 + ((int64_t)now_tv
.tv_usec
) / 100000;
73 int64_t pim_time_monotonic_usec(void)
75 struct timeval now_tv
;
78 if (gettime_monotonic(&now_tv
)) {
79 flog_err_sys(EC_LIB_SYSTEM_CALL
,
80 "%s: gettime_monotonic() failure: errno=%d: %s",
81 __func__
, errno
, safe_strerror(errno
));
86 ((int64_t)now_tv
.tv_sec
) * 1000000 + ((int64_t)now_tv
.tv_usec
);
91 int pim_time_mmss(char *buf
, int buf_size
, long sec
)
96 assert(buf_size
>= 5);
101 wr
= snprintf(buf
, buf_size
, "%02ld:%02ld", mm
, sec
);
106 static int pim_time_hhmmss(char *buf
, int buf_size
, long sec
)
112 assert(buf_size
>= 8);
119 wr
= snprintf(buf
, buf_size
, "%02ld:%02ld:%02ld", hh
, mm
, sec
);
124 void pim_time_timer_to_mmss(char *buf
, int buf_size
, struct thread
*t_timer
)
127 pim_time_mmss(buf
, buf_size
,
128 thread_timer_remain_second(t_timer
));
130 snprintf(buf
, buf_size
, "--:--");
134 void pim_time_timer_to_hhmmss(char *buf
, int buf_size
, struct thread
*t_timer
)
137 pim_time_hhmmss(buf
, buf_size
,
138 thread_timer_remain_second(t_timer
));
140 snprintf(buf
, buf_size
, "--:--:--");
144 void pim_time_uptime(char *buf
, int buf_size
, int64_t uptime_sec
)
146 assert(buf_size
>= 8);
148 pim_time_hhmmss(buf
, buf_size
, uptime_sec
);
151 void pim_time_uptime_begin(char *buf
, int buf_size
, int64_t now
, int64_t begin
)
154 pim_time_uptime(buf
, buf_size
, now
- begin
);
156 snprintf(buf
, buf_size
, "--:--:--");
159 long pim_time_timer_remain_msec(struct thread
*t_timer
)
161 /* no timer thread running means timer has expired: return 0 */
163 return t_timer
? thread_timer_remain_msec(t_timer
) : 0;