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