blob: 4e5832cc657bab652fef61d1ea372ba2fd6d39b3 [file] [log] [blame]
Everton Marques871dbcf2009-08-11 15:43:05 -03001/*
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 $QuaggaId: $Format:%an, %ai, %h$ $
21*/
22
David Lamparter821df2c2015-09-15 01:53:09 -070023#include <zebra.h>
24
Everton Marques871dbcf2009-08-11 15:43:05 -030025#include <string.h>
26#include <sys/time.h>
27#include <time.h>
28
Everton Marques871dbcf2009-08-11 15:43:05 -030029#include "log.h"
30#include "thread.h"
31
32#include "pim_time.h"
33
Everton Marquesf9e05e52010-03-11 11:17:33 -030034static int gettime_monotonic(struct timeval *tv)
35{
36 int result;
37
Everton Marques0ef36d82010-03-11 14:47:42 -030038 result = gettimeofday(tv, 0);
39 if (result) {
40 zlog_err("%s: gettimeofday() failure: errno=%d: %s",
41 __PRETTY_FUNCTION__,
42 errno, safe_strerror(errno));
43 }
Everton Marquesf9e05e52010-03-11 11:17:33 -030044
45 return result;
Everton Marques871dbcf2009-08-11 15:43:05 -030046}
47
48/*
49 pim_time_monotonic_sec():
50 number of seconds since some unspecified starting point
51*/
52int64_t pim_time_monotonic_sec()
53{
54 struct timeval now_tv;
55
Everton Marquesf9e05e52010-03-11 11:17:33 -030056 if (gettime_monotonic(&now_tv)) {
57 zlog_err("%s: gettime_monotonic() failure: errno=%d: %s",
Everton Marques871dbcf2009-08-11 15:43:05 -030058 __PRETTY_FUNCTION__,
Everton Marquese96f0af2009-08-11 15:48:02 -030059 errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -030060 return -1;
61 }
62
63 return now_tv.tv_sec;
64}
65
66/*
67 pim_time_monotonic_dsec():
68 number of deciseconds since some unspecified starting point
69*/
70int64_t pim_time_monotonic_dsec()
71{
72 struct timeval now_tv;
73 int64_t now_dsec;
74
Everton Marquesf9e05e52010-03-11 11:17:33 -030075 if (gettime_monotonic(&now_tv)) {
76 zlog_err("%s: gettime_monotonic() failure: errno=%d: %s",
Everton Marques871dbcf2009-08-11 15:43:05 -030077 __PRETTY_FUNCTION__,
Everton Marquese96f0af2009-08-11 15:48:02 -030078 errno, safe_strerror(errno));
Everton Marques871dbcf2009-08-11 15:43:05 -030079 return -1;
80 }
81
82 now_dsec = ((int64_t) now_tv.tv_sec) * 10 + ((int64_t) now_tv.tv_usec) / 100000;
83
84 return now_dsec;
85}
86
87int pim_time_mmss(char *buf, int buf_size, long sec)
88{
89 long mm;
90 int wr;
91
92 zassert(buf_size >= 5);
93
94 mm = sec / 60;
95 sec %= 60;
96
97 wr = snprintf(buf, buf_size, "%02ld:%02ld", mm, sec);
98
99 return wr != 8;
100}
101
102static int pim_time_hhmmss(char *buf, int buf_size, long sec)
103{
104 long hh;
105 long mm;
106 int wr;
107
108 zassert(buf_size >= 8);
109
110 hh = sec / 3600;
111 sec %= 3600;
112 mm = sec / 60;
113 sec %= 60;
114
115 wr = snprintf(buf, buf_size, "%02ld:%02ld:%02ld", hh, mm, sec);
116
117 return wr != 8;
118}
119
120void pim_time_timer_to_mmss(char *buf, int buf_size, struct thread *t_timer)
121{
122 if (t_timer) {
123 pim_time_mmss(buf, buf_size,
124 thread_timer_remain_second(t_timer));
125 }
126 else {
127 snprintf(buf, buf_size, "--:--");
128 }
129}
130
131void pim_time_timer_to_hhmmss(char *buf, int buf_size, struct thread *t_timer)
132{
133 if (t_timer) {
134 pim_time_hhmmss(buf, buf_size,
135 thread_timer_remain_second(t_timer));
136 }
137 else {
138 snprintf(buf, buf_size, "--:--:--");
139 }
140}
141
142void pim_time_uptime(char *buf, int buf_size, int64_t uptime_sec)
143{
144 zassert(buf_size >= 8);
145
146 pim_time_hhmmss(buf, buf_size, uptime_sec);
147}
148
Everton Marquesff752d42010-03-17 10:34:24 -0300149void pim_time_uptime_begin(char *buf, int buf_size, int64_t now, int64_t begin)
150{
151 if (begin > 0)
152 pim_time_uptime(buf, buf_size, now - begin);
153 else
154 snprintf(buf, buf_size, "--:--:--");
155}
156
Everton Marques871dbcf2009-08-11 15:43:05 -0300157long pim_time_timer_remain_msec(struct thread *t_timer)
158{
159 /* FIXME: Actually fetch msec resolution from thread */
160
161 /* no timer thread running means timer has expired: return 0 */
162
163 return t_timer ?
164 1000 * thread_timer_remain_second(t_timer) :
165 0;
166}