]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_time.c
Merge pull request #1685 from LabNConsulting/working/vpn-doc
[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__, errno, safe_strerror(errno));
39 }
40
41 return result;
42 }
43
44 /*
45 pim_time_monotonic_sec():
46 number of seconds since some unspecified starting point
47 */
48 int64_t pim_time_monotonic_sec()
49 {
50 struct timeval now_tv;
51
52 if (gettime_monotonic(&now_tv)) {
53 zlog_err("%s: gettime_monotonic() failure: errno=%d: %s",
54 __PRETTY_FUNCTION__, errno, safe_strerror(errno));
55 return -1;
56 }
57
58 return now_tv.tv_sec;
59 }
60
61 /*
62 pim_time_monotonic_dsec():
63 number of deciseconds since some unspecified starting point
64 */
65 int64_t pim_time_monotonic_dsec()
66 {
67 struct timeval now_tv;
68 int64_t now_dsec;
69
70 if (gettime_monotonic(&now_tv)) {
71 zlog_err("%s: gettime_monotonic() failure: errno=%d: %s",
72 __PRETTY_FUNCTION__, errno, safe_strerror(errno));
73 return -1;
74 }
75
76 now_dsec = ((int64_t)now_tv.tv_sec) * 10
77 + ((int64_t)now_tv.tv_usec) / 100000;
78
79 return now_dsec;
80 }
81
82 int64_t pim_time_monotonic_usec(void)
83 {
84 struct timeval now_tv;
85 int64_t now_dsec;
86
87 if (gettime_monotonic(&now_tv)) {
88 zlog_err("%s: gettime_monotonic() failure: errno=%d: %s",
89 __PRETTY_FUNCTION__, errno, safe_strerror(errno));
90 return -1;
91 }
92
93 now_dsec =
94 ((int64_t)now_tv.tv_sec) * 1000000 + ((int64_t)now_tv.tv_usec);
95
96 return now_dsec;
97 }
98
99 int pim_time_mmss(char *buf, int buf_size, long sec)
100 {
101 long mm;
102 int wr;
103
104 zassert(buf_size >= 5);
105
106 mm = sec / 60;
107 sec %= 60;
108
109 wr = snprintf(buf, buf_size, "%02ld:%02ld", mm, sec);
110
111 return wr != 8;
112 }
113
114 static int pim_time_hhmmss(char *buf, int buf_size, long sec)
115 {
116 long hh;
117 long mm;
118 int wr;
119
120 zassert(buf_size >= 8);
121
122 hh = sec / 3600;
123 sec %= 3600;
124 mm = sec / 60;
125 sec %= 60;
126
127 wr = snprintf(buf, buf_size, "%02ld:%02ld:%02ld", hh, mm, sec);
128
129 return wr != 8;
130 }
131
132 void pim_time_timer_to_mmss(char *buf, int buf_size, struct thread *t_timer)
133 {
134 if (t_timer) {
135 pim_time_mmss(buf, buf_size,
136 thread_timer_remain_second(t_timer));
137 } else {
138 snprintf(buf, buf_size, "--:--");
139 }
140 }
141
142 void pim_time_timer_to_hhmmss(char *buf, int buf_size, struct thread *t_timer)
143 {
144 if (t_timer) {
145 pim_time_hhmmss(buf, buf_size,
146 thread_timer_remain_second(t_timer));
147 } else {
148 snprintf(buf, buf_size, "--:--:--");
149 }
150 }
151
152 void pim_time_uptime(char *buf, int buf_size, int64_t uptime_sec)
153 {
154 zassert(buf_size >= 8);
155
156 pim_time_hhmmss(buf, buf_size, uptime_sec);
157 }
158
159 void pim_time_uptime_begin(char *buf, int buf_size, int64_t now, int64_t begin)
160 {
161 if (begin > 0)
162 pim_time_uptime(buf, buf_size, now - begin);
163 else
164 snprintf(buf, buf_size, "--:--:--");
165 }
166
167 long pim_time_timer_remain_msec(struct thread *t_timer)
168 {
169 /* FIXME: Actually fetch msec resolution from thread */
170
171 /* no timer thread running means timer has expired: return 0 */
172
173 return t_timer ? 1000 * thread_timer_remain_second(t_timer) : 0;
174 }