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