]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_time.c
Merge remote-tracking branch 'origin/stable/3.0'
[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 along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include <zebra.h>
21
22 #include <string.h>
23 #include <sys/time.h>
24 #include <time.h>
25
26 #include "log.h"
27 #include "thread.h"
28
29 #include "pim_time.h"
30
31 static int gettime_monotonic(struct timeval *tv)
32 {
33 int result;
34
35 result = gettimeofday(tv, 0);
36 if (result) {
37 zlog_err("%s: gettimeofday() failure: errno=%d: %s",
38 __PRETTY_FUNCTION__,
39 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__,
56 errno, safe_strerror(errno));
57 return -1;
58 }
59
60 return now_tv.tv_sec;
61 }
62
63 /*
64 pim_time_monotonic_dsec():
65 number of deciseconds since some unspecified starting point
66 */
67 int64_t pim_time_monotonic_dsec()
68 {
69 struct timeval now_tv;
70 int64_t now_dsec;
71
72 if (gettime_monotonic(&now_tv)) {
73 zlog_err("%s: gettime_monotonic() failure: errno=%d: %s",
74 __PRETTY_FUNCTION__,
75 errno, safe_strerror(errno));
76 return -1;
77 }
78
79 now_dsec = ((int64_t) now_tv.tv_sec) * 10 + ((int64_t) now_tv.tv_usec) / 100000;
80
81 return now_dsec;
82 }
83
84 int64_t
85 pim_time_monotonic_usec (void)
86 {
87 struct timeval now_tv;
88 int64_t now_dsec;
89
90 if (gettime_monotonic(&now_tv)) {
91 zlog_err("%s: gettime_monotonic() failure: errno=%d: %s",
92 __PRETTY_FUNCTION__,
93 errno, safe_strerror(errno));
94 return -1;
95 }
96
97 now_dsec = ((int64_t) now_tv.tv_sec) * 1000000 + ((int64_t) now_tv.tv_usec);
98
99 return now_dsec;
100
101 }
102
103 int pim_time_mmss(char *buf, int buf_size, long sec)
104 {
105 long mm;
106 int wr;
107
108 zassert(buf_size >= 5);
109
110 mm = sec / 60;
111 sec %= 60;
112
113 wr = snprintf(buf, buf_size, "%02ld:%02ld", mm, sec);
114
115 return wr != 8;
116 }
117
118 static int pim_time_hhmmss(char *buf, int buf_size, long sec)
119 {
120 long hh;
121 long mm;
122 int wr;
123
124 zassert(buf_size >= 8);
125
126 hh = sec / 3600;
127 sec %= 3600;
128 mm = sec / 60;
129 sec %= 60;
130
131 wr = snprintf(buf, buf_size, "%02ld:%02ld:%02ld", hh, mm, sec);
132
133 return wr != 8;
134 }
135
136 void pim_time_timer_to_mmss(char *buf, int buf_size, struct thread *t_timer)
137 {
138 if (t_timer) {
139 pim_time_mmss(buf, buf_size,
140 thread_timer_remain_second(t_timer));
141 }
142 else {
143 snprintf(buf, buf_size, "--:--");
144 }
145 }
146
147 void pim_time_timer_to_hhmmss(char *buf, int buf_size, struct thread *t_timer)
148 {
149 if (t_timer) {
150 pim_time_hhmmss(buf, buf_size,
151 thread_timer_remain_second(t_timer));
152 }
153 else {
154 snprintf(buf, buf_size, "--:--:--");
155 }
156 }
157
158 void pim_time_uptime(char *buf, int buf_size, int64_t uptime_sec)
159 {
160 zassert(buf_size >= 8);
161
162 pim_time_hhmmss(buf, buf_size, uptime_sec);
163 }
164
165 void pim_time_uptime_begin(char *buf, int buf_size, int64_t now, int64_t begin)
166 {
167 if (begin > 0)
168 pim_time_uptime(buf, buf_size, now - begin);
169 else
170 snprintf(buf, buf_size, "--:--:--");
171 }
172
173 long pim_time_timer_remain_msec(struct thread *t_timer)
174 {
175 /* FIXME: Actually fetch msec resolution from thread */
176
177 /* no timer thread running means timer has expired: return 0 */
178
179 return t_timer ?
180 1000 * thread_timer_remain_second(t_timer) :
181 0;
182 }