]> git.proxmox.com Git - mirror_frr.git/blame - pimd/pim_time.c
bgpd: Auto RD definitions and encoding
[mirror_frr.git] / pimd / pim_time.c
CommitLineData
12e41d03 1/*
896014f4
DL
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 */
12e41d03 19
7a2fbbf0
DL
20#include <zebra.h>
21
12e41d03
DL
22#include <string.h>
23#include <sys/time.h>
24#include <time.h>
25
12e41d03
DL
26#include "log.h"
27#include "thread.h"
28
29#include "pim_time.h"
30
31static int gettime_monotonic(struct timeval *tv)
32{
d62a17ae 33 int result;
12e41d03 34
d62a17ae 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 }
12e41d03 40
d62a17ae 41 return result;
12e41d03
DL
42}
43
44/*
45 pim_time_monotonic_sec():
46 number of seconds since some unspecified starting point
47*/
48int64_t pim_time_monotonic_sec()
49{
d62a17ae 50 struct timeval now_tv;
12e41d03 51
d62a17ae 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 }
12e41d03 57
d62a17ae 58 return now_tv.tv_sec;
12e41d03
DL
59}
60
61/*
62 pim_time_monotonic_dsec():
63 number of deciseconds since some unspecified starting point
64*/
65int64_t pim_time_monotonic_dsec()
66{
d62a17ae 67 struct timeval now_tv;
68 int64_t now_dsec;
12e41d03 69
d62a17ae 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 }
12e41d03 75
d62a17ae 76 now_dsec = ((int64_t)now_tv.tv_sec) * 10
77 + ((int64_t)now_tv.tv_usec) / 100000;
12e41d03 78
d62a17ae 79 return now_dsec;
12e41d03
DL
80}
81
d62a17ae 82int64_t pim_time_monotonic_usec(void)
8a9bd91c 83{
d62a17ae 84 struct timeval now_tv;
85 int64_t now_dsec;
8a9bd91c 86
d62a17ae 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 }
8a9bd91c 92
d62a17ae 93 now_dsec =
94 ((int64_t)now_tv.tv_sec) * 1000000 + ((int64_t)now_tv.tv_usec);
8a9bd91c 95
d62a17ae 96 return now_dsec;
8a9bd91c
DS
97}
98
12e41d03
DL
99int pim_time_mmss(char *buf, int buf_size, long sec)
100{
d62a17ae 101 long mm;
102 int wr;
103
104 zassert(buf_size >= 5);
12e41d03 105
d62a17ae 106 mm = sec / 60;
107 sec %= 60;
12e41d03 108
d62a17ae 109 wr = snprintf(buf, buf_size, "%02ld:%02ld", mm, sec);
12e41d03 110
d62a17ae 111 return wr != 8;
12e41d03
DL
112}
113
114static int pim_time_hhmmss(char *buf, int buf_size, long sec)
115{
d62a17ae 116 long hh;
117 long mm;
118 int wr;
119
120 zassert(buf_size >= 8);
12e41d03 121
d62a17ae 122 hh = sec / 3600;
123 sec %= 3600;
124 mm = sec / 60;
125 sec %= 60;
12e41d03 126
d62a17ae 127 wr = snprintf(buf, buf_size, "%02ld:%02ld:%02ld", hh, mm, sec);
12e41d03 128
d62a17ae 129 return wr != 8;
12e41d03
DL
130}
131
132void pim_time_timer_to_mmss(char *buf, int buf_size, struct thread *t_timer)
133{
d62a17ae 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 }
12e41d03
DL
140}
141
142void pim_time_timer_to_hhmmss(char *buf, int buf_size, struct thread *t_timer)
143{
d62a17ae 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 }
12e41d03
DL
150}
151
152void pim_time_uptime(char *buf, int buf_size, int64_t uptime_sec)
153{
d62a17ae 154 zassert(buf_size >= 8);
12e41d03 155
d62a17ae 156 pim_time_hhmmss(buf, buf_size, uptime_sec);
12e41d03
DL
157}
158
159void pim_time_uptime_begin(char *buf, int buf_size, int64_t now, int64_t begin)
160{
d62a17ae 161 if (begin > 0)
162 pim_time_uptime(buf, buf_size, now - begin);
163 else
164 snprintf(buf, buf_size, "--:--:--");
12e41d03
DL
165}
166
167long pim_time_timer_remain_msec(struct thread *t_timer)
168{
d62a17ae 169 /* FIXME: Actually fetch msec resolution from thread */
12e41d03 170
d62a17ae 171 /* no timer thread running means timer has expired: return 0 */
12e41d03 172
d62a17ae 173 return t_timer ? 1000 * thread_timer_remain_second(t_timer) : 0;
12e41d03 174}