]>
git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_time.c
3 * Copyright (C) 2008 Everton da Silva Marques
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.
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.
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
31 static int gettime_monotonic(struct timeval
*tv
)
35 result
= gettimeofday(tv
, 0);
37 zlog_err("%s: gettimeofday() failure: errno=%d: %s",
38 __PRETTY_FUNCTION__
, errno
, safe_strerror(errno
));
45 pim_time_monotonic_sec():
46 number of seconds since some unspecified starting point
48 int64_t pim_time_monotonic_sec()
50 struct timeval now_tv
;
52 if (gettime_monotonic(&now_tv
)) {
53 zlog_err("%s: gettime_monotonic() failure: errno=%d: %s",
54 __PRETTY_FUNCTION__
, errno
, safe_strerror(errno
));
62 pim_time_monotonic_dsec():
63 number of deciseconds since some unspecified starting point
65 int64_t pim_time_monotonic_dsec()
67 struct timeval now_tv
;
70 if (gettime_monotonic(&now_tv
)) {
71 zlog_err("%s: gettime_monotonic() failure: errno=%d: %s",
72 __PRETTY_FUNCTION__
, errno
, safe_strerror(errno
));
76 now_dsec
= ((int64_t)now_tv
.tv_sec
) * 10
77 + ((int64_t)now_tv
.tv_usec
) / 100000;
82 int64_t pim_time_monotonic_usec(void)
84 struct timeval now_tv
;
87 if (gettime_monotonic(&now_tv
)) {
88 zlog_err("%s: gettime_monotonic() failure: errno=%d: %s",
89 __PRETTY_FUNCTION__
, errno
, safe_strerror(errno
));
94 ((int64_t)now_tv
.tv_sec
) * 1000000 + ((int64_t)now_tv
.tv_usec
);
99 int pim_time_mmss(char *buf
, int buf_size
, long sec
)
104 zassert(buf_size
>= 5);
109 wr
= snprintf(buf
, buf_size
, "%02ld:%02ld", mm
, sec
);
114 static int pim_time_hhmmss(char *buf
, int buf_size
, long sec
)
120 zassert(buf_size
>= 8);
127 wr
= snprintf(buf
, buf_size
, "%02ld:%02ld:%02ld", hh
, mm
, sec
);
132 void pim_time_timer_to_mmss(char *buf
, int buf_size
, struct thread
*t_timer
)
135 pim_time_mmss(buf
, buf_size
,
136 thread_timer_remain_second(t_timer
));
138 snprintf(buf
, buf_size
, "--:--");
142 void pim_time_timer_to_hhmmss(char *buf
, int buf_size
, struct thread
*t_timer
)
145 pim_time_hhmmss(buf
, buf_size
,
146 thread_timer_remain_second(t_timer
));
148 snprintf(buf
, buf_size
, "--:--:--");
152 void pim_time_uptime(char *buf
, int buf_size
, int64_t uptime_sec
)
154 zassert(buf_size
>= 8);
156 pim_time_hhmmss(buf
, buf_size
, uptime_sec
);
159 void pim_time_uptime_begin(char *buf
, int buf_size
, int64_t now
, int64_t begin
)
162 pim_time_uptime(buf
, buf_size
, now
- begin
);
164 snprintf(buf
, buf_size
, "--:--:--");
167 long pim_time_timer_remain_msec(struct thread
*t_timer
)
169 /* FIXME: Actually fetch msec resolution from thread */
171 /* no timer thread running means timer has expired: return 0 */
173 return t_timer
? 1000 * thread_timer_remain_second(t_timer
) : 0;