1224006Shrs/*	$FreeBSD$	*/
2224006Shrs/*	$KAME: timer.c,v 1.9 2002/06/10 19:59:47 itojun Exp $	*/
3224006Shrs
4331722Seadler/*
5224006Shrs * Copyright (C) 1998 WIDE Project.
6224006Shrs * All rights reserved.
7224006Shrs *
8224006Shrs * Redistribution and use in source and binary forms, with or without
9224006Shrs * modification, are permitted provided that the following conditions
10224006Shrs * are met:
11224006Shrs * 1. Redistributions of source code must retain the above copyright
12224006Shrs *    notice, this list of conditions and the following disclaimer.
13224006Shrs * 2. Redistributions in binary form must reproduce the above copyright
14224006Shrs *    notice, this list of conditions and the following disclaimer in the
15224006Shrs *    documentation and/or other materials provided with the distribution.
16224006Shrs * 3. Neither the name of the project nor the names of its contributors
17224006Shrs *    may be used to endorse or promote products derived from this software
18224006Shrs *    without specific prior written permission.
19224006Shrs *
20224006Shrs * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21224006Shrs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22224006Shrs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23224006Shrs * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24224006Shrs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25224006Shrs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26224006Shrs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27224006Shrs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28224006Shrs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29224006Shrs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30224006Shrs * SUCH DAMAGE.
31224006Shrs */
32224006Shrs
33224006Shrs#include <sys/queue.h>
34224006Shrs#include <sys/socket.h>
35224006Shrs#include <syslog.h>
36224006Shrs#include <stdio.h>
37224144Shrs#include <inttypes.h>
38253970Shrs#include <time.h>
39224006Shrs
40224006Shrs#include "timer.h"
41224006Shrs#include "timer_subr.h"
42224006Shrs
43253970Shrsstruct timespec *
44224006Shrsrtadvd_timer_rest(struct rtadvd_timer *rat)
45224006Shrs{
46253970Shrs	static struct timespec returnval, now;
47224006Shrs
48253970Shrs	clock_gettime(CLOCK_MONOTONIC_FAST, &now);
49253970Shrs	if (TS_CMP(&rat->rat_tm, &now, <=)) {
50224006Shrs		syslog(LOG_DEBUG,
51224006Shrs		    "<%s> a timer must be expired, but not yet",
52224006Shrs		    __func__);
53253970Shrs		returnval.tv_sec = returnval.tv_nsec = 0;
54224006Shrs	}
55224006Shrs	else
56253970Shrs		TS_SUB(&rat->rat_tm, &now, &returnval);
57224006Shrs
58224006Shrs	return (&returnval);
59224006Shrs}
60224006Shrs
61224006Shrschar *
62224144Shrssec2str(uint32_t s, char *buf)
63224006Shrs{
64224144Shrs	uint32_t day;
65224144Shrs	uint32_t hour;
66224144Shrs	uint32_t min;
67224144Shrs	uint32_t sec;
68224006Shrs	char *p;
69224144Shrs
70224006Shrs	min = s / 60;
71224006Shrs	sec = s % 60;
72224006Shrs
73224006Shrs	hour = min / 60;
74224006Shrs	min = min % 60;
75224006Shrs
76224006Shrs	day = hour / 24;
77224006Shrs	hour = hour % 24;
78224006Shrs
79224006Shrs	p = buf;
80224006Shrs	if (day > 0)
81224144Shrs		p += sprintf(p, "%" PRIu32 "d", day);
82224006Shrs	if (hour > 0)
83224144Shrs		p += sprintf(p, "%" PRIu32 "h", hour);
84224006Shrs	if (min > 0)
85224144Shrs		p += sprintf(p, "%" PRIu32 "m", min);
86224006Shrs
87224144Shrs	if ((p == buf) || (sec > 0 && p > buf))
88224144Shrs		sprintf(p, "%" PRIu32 "s", sec);
89224006Shrs
90224006Shrs	return (buf);
91224006Shrs}
92