]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_time.c
release: FRR 3.0-rc1
[mirror_frr.git] / pimd / pim_time.c
1 /*
2 PIM for Quagga
3 Copyright (C) 2008 Everton da Silva Marques
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING; if not, write to the
17 Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
18 MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include <string.h>
24 #include <sys/time.h>
25 #include <time.h>
26
27 #include "log.h"
28 #include "thread.h"
29
30 #include "pim_time.h"
31
32 static int gettime_monotonic(struct timeval *tv)
33 {
34 int result;
35
36 result = gettimeofday(tv, 0);
37 if (result) {
38 zlog_err("%s: gettimeofday() failure: errno=%d: %s",
39 __PRETTY_FUNCTION__, errno, safe_strerror(errno));
40 }
41
42 return result;
43 }
44
45 /*
46 pim_time_monotonic_sec():
47 number of seconds since some unspecified starting point
48 */
49 int64_t pim_time_monotonic_sec()
50 {
51 struct timeval now_tv;
52
53 if (gettime_monotonic(&now_tv)) {
54 zlog_err("%s: gettime_monotonic() failure: errno=%d: %s",
55 __PRETTY_FUNCTION__, errno, safe_strerror(errno));
56 return -1;
57 }
58
59 return now_tv.tv_sec;
60 }
61
62 /*
63 pim_time_monotonic_dsec():
64 number of deciseconds since some unspecified starting point
65 */
66 int64_t pim_time_monotonic_dsec()
67 {
68 struct timeval now_tv;
69 int64_t now_dsec;
70
71 if (gettime_monotonic(&now_tv)) {
72 zlog_err("%s: gettime_monotonic() failure: errno=%d: %s",
73 __PRETTY_FUNCTION__, errno, safe_strerror(errno));
74 return -1;
75 }
76
77 now_dsec = ((int64_t)now_tv.tv_sec) * 10
78 + ((int64_t)now_tv.tv_usec) / 100000;
79
80 return now_dsec;
81 }
82
83 int64_t pim_time_monotonic_usec(void)
84 {
85 struct timeval now_tv;
86 int64_t now_dsec;
87
88 if (gettime_monotonic(&now_tv)) {
89 zlog_err("%s: gettime_monotonic() failure: errno=%d: %s",
90 __PRETTY_FUNCTION__, errno, safe_strerror(errno));
91 return -1;
92 }
93
94 now_dsec =
95 ((int64_t)now_tv.tv_sec) * 1000000 + ((int64_t)now_tv.tv_usec);
96
97 return now_dsec;
98 }
99
100 int pim_time_mmss(char *buf, int buf_size, long sec)
101 {
102 long mm;
103 int wr;
104
105 zassert(buf_size >= 5);
106
107 mm = sec / 60;
108 sec %= 60;
109
110 wr = snprintf(buf, buf_size, "%02ld:%02ld", mm, sec);
111
112 return wr != 8;
113 }
114
115 static int pim_time_hhmmss(char *buf, int buf_size, long sec)
116 {
117 long hh;
118 long mm;
119 int wr;
120
121 zassert(buf_size >= 8);
122
123 hh = sec / 3600;
124 sec %= 3600;
125 mm = sec / 60;
126 sec %= 60;
127
128 wr = snprintf(buf, buf_size, "%02ld:%02ld:%02ld", hh, mm, sec);
129
130 return wr != 8;
131 }
132
133 void pim_time_timer_to_mmss(char *buf, int buf_size, struct thread *t_timer)
134 {
135 if (t_timer) {
136 pim_time_mmss(buf, buf_size,
137 thread_timer_remain_second(t_timer));
138 } else {
139 snprintf(buf, buf_size, "--:--");
140 }
141 }
142
143 void pim_time_timer_to_hhmmss(char *buf, int buf_size, struct thread *t_timer)
144 {
145 if (t_timer) {
146 pim_time_hhmmss(buf, buf_size,
147 thread_timer_remain_second(t_timer));
148 } else {
149 snprintf(buf, buf_size, "--:--:--");
150 }
151 }
152
153 void pim_time_uptime(char *buf, int buf_size, int64_t uptime_sec)
154 {
155 zassert(buf_size >= 8);
156
157 pim_time_hhmmss(buf, buf_size, uptime_sec);
158 }
159
160 void pim_time_uptime_begin(char *buf, int buf_size, int64_t now, int64_t begin)
161 {
162 if (begin > 0)
163 pim_time_uptime(buf, buf_size, now - begin);
164 else
165 snprintf(buf, buf_size, "--:--:--");
166 }
167
168 long pim_time_timer_remain_msec(struct thread *t_timer)
169 {
170 /* FIXME: Actually fetch msec resolution from thread */
171
172 /* no timer thread running means timer has expired: return 0 */
173
174 return t_timer ? 1000 * thread_timer_remain_second(t_timer) : 0;
175 }