]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_time.c
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / pimd / pim_time.c
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 "thread.h"
15 #include "lib_errors.h"
16
17 #include "pim_time.h"
18
19 static int gettime_monotonic(struct timeval *tv)
20 {
21 int result;
22
23 result = gettimeofday(tv, 0);
24 if (result) {
25 flog_err_sys(EC_LIB_SYSTEM_CALL,
26 "%s: gettimeofday() failure: errno=%d: %s",
27 __func__, errno, safe_strerror(errno));
28 }
29
30 return result;
31 }
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 {
39 struct timeval now_tv;
40
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));
45 return -1;
46 }
47
48 return now_tv.tv_sec;
49 }
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 {
57 struct timeval now_tv;
58 int64_t now_dsec;
59
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));
64 return -1;
65 }
66
67 now_dsec = ((int64_t)now_tv.tv_sec) * 10
68 + ((int64_t)now_tv.tv_usec) / 100000;
69
70 return now_dsec;
71 }
72
73 int64_t pim_time_monotonic_usec(void)
74 {
75 struct timeval now_tv;
76 int64_t now_dsec;
77
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));
82 return -1;
83 }
84
85 now_dsec =
86 ((int64_t)now_tv.tv_sec) * 1000000 + ((int64_t)now_tv.tv_usec);
87
88 return now_dsec;
89 }
90
91 int pim_time_mmss(char *buf, int buf_size, long sec)
92 {
93 long mm;
94 int wr;
95
96 assert(buf_size >= 5);
97
98 mm = sec / 60;
99 sec %= 60;
100
101 wr = snprintf(buf, buf_size, "%02ld:%02ld", mm, sec);
102
103 return wr != 8;
104 }
105
106 static int pim_time_hhmmss(char *buf, int buf_size, long sec)
107 {
108 long hh;
109 long mm;
110 int wr;
111
112 assert(buf_size >= 8);
113
114 hh = sec / 3600;
115 sec %= 3600;
116 mm = sec / 60;
117 sec %= 60;
118
119 wr = snprintf(buf, buf_size, "%02ld:%02ld:%02ld", hh, mm, sec);
120
121 return wr != 8;
122 }
123
124 void pim_time_timer_to_mmss(char *buf, int buf_size, struct thread *t_timer)
125 {
126 if (t_timer) {
127 pim_time_mmss(buf, buf_size,
128 thread_timer_remain_second(t_timer));
129 } else {
130 snprintf(buf, buf_size, "--:--");
131 }
132 }
133
134 void pim_time_timer_to_hhmmss(char *buf, int buf_size, struct thread *t_timer)
135 {
136 if (t_timer) {
137 pim_time_hhmmss(buf, buf_size,
138 thread_timer_remain_second(t_timer));
139 } else {
140 snprintf(buf, buf_size, "--:--:--");
141 }
142 }
143
144 void pim_time_uptime(char *buf, int buf_size, int64_t uptime_sec)
145 {
146 assert(buf_size >= 8);
147
148 pim_time_hhmmss(buf, buf_size, uptime_sec);
149 }
150
151 void pim_time_uptime_begin(char *buf, int buf_size, int64_t now, int64_t begin)
152 {
153 if (begin > 0)
154 pim_time_uptime(buf, buf_size, now - begin);
155 else
156 snprintf(buf, buf_size, "--:--:--");
157 }
158
159 long pim_time_timer_remain_msec(struct thread *t_timer)
160 {
161 /* no timer thread running means timer has expired: return 0 */
162
163 return t_timer ? thread_timer_remain_msec(t_timer) : 0;
164 }