191041Sobrien/*
2218822Sdim * Copyright (C) 2004, 2006-2009  Internet Systems Consortium, Inc. ("ISC")
391041Sobrien * Copyright (C) 1998-2001  Internet Software Consortium.
491041Sobrien *
591041Sobrien * Permission to use, copy, modify, and/or distribute this software for any
691041Sobrien * purpose with or without fee is hereby granted, provided that the above
7104834Sobrien * copyright notice and this permission notice appear in all copies.
891041Sobrien *
9104834Sobrien * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10104834Sobrien * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11104834Sobrien * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12104834Sobrien * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
1391041Sobrien * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14104834Sobrien * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15104834Sobrien * PERFORMANCE OF THIS SOFTWARE.
16104834Sobrien */
17104834Sobrien
1891041Sobrien/* $Id: time.h,v 1.35 2009/01/05 23:47:54 tbox Exp $ */
19104834Sobrien
20104834Sobrien#ifndef ISC_TIME_H
21218822Sdim#define ISC_TIME_H 1
2291041Sobrien
23218822Sdim#include <windows.h>
2491041Sobrien
2591041Sobrien#include <isc/lang.h>
2691041Sobrien#include <isc/types.h>
2791041Sobrien
2891041Sobrien/***
2991041Sobrien *** Intervals
3091041Sobrien ***/
3191041Sobrien
3291041Sobrien/*
3391041Sobrien * The contents of this structure are private, and MUST NOT be accessed
3491041Sobrien * directly by callers.
3591041Sobrien *
3691041Sobrien * The contents are exposed only to allow callers to avoid dynamic allocation.
3791041Sobrien */
3891041Sobrienstruct isc_interval {
3991041Sobrien	isc_int64_t interval;
4091041Sobrien};
4191041Sobrien
4291041SobrienLIBISC_EXTERNAL_DATA extern isc_interval_t *isc_interval_zero;
4391041Sobrien
4491041SobrienISC_LANG_BEGINDECLS
4591041Sobrien
4691041Sobrienvoid
4791041Sobrienisc_interval_set(isc_interval_t *i,
4891041Sobrien		 unsigned int seconds, unsigned int nanoseconds);
4991041Sobrien/*
5091041Sobrien * Set 'i' to a value representing an interval of 'seconds' seconds and
5191041Sobrien * 'nanoseconds' nanoseconds, suitable for use in isc_time_add() and
5291041Sobrien * isc_time_subtract().
5391041Sobrien *
5491041Sobrien * Requires:
5591041Sobrien *
5691041Sobrien *	't' is a valid pointer.
5791041Sobrien *	nanoseconds < 1000000000.
5891041Sobrien */
5991041Sobrien
6091041Sobrienisc_boolean_t
6191041Sobrienisc_interval_iszero(const isc_interval_t *i);
6291041Sobrien/*
6391041Sobrien * Returns ISC_TRUE iff. 'i' is the zero interval.
6491041Sobrien *
6591041Sobrien * Requires:
6691041Sobrien *
6791041Sobrien *	'i' is a valid pointer.
6891041Sobrien */
6991041Sobrien
7091041Sobrien/***
7191041Sobrien *** Absolute Times
7291041Sobrien ***/
7391041Sobrien
7491041Sobrien/*
7591041Sobrien * The contents of this structure are private, and MUST NOT be accessed
7691041Sobrien * directly by callers.
7791041Sobrien *
7891041Sobrien * The contents are exposed only to allow callers to avoid dynamic allocation.
7991041Sobrien */
8091041Sobrien
8191041Sobrienstruct isc_time {
8291041Sobrien	FILETIME absolute;
8391041Sobrien};
8491041Sobrien
85130561SobrienLIBISC_EXTERNAL_DATA extern isc_time_t *isc_time_epoch;
86130561Sobrien
87130561Sobrienvoid
88130561Sobrienisc_time_set(isc_time_t *t, unsigned int seconds, unsigned int nanoseconds);
89130561Sobrien/*%<
90104834Sobrien * Set 't' to a value which represents the given number of seconds and
91104834Sobrien * nanoseconds since 00:00:00 January 1, 1970, UTC.
92130561Sobrien *
9391041Sobrien * Requires:
9491041Sobrien *\li   't' is a valid pointer.
95130561Sobrien *\li   nanoseconds < 1000000000.
96130561Sobrien */
97130561Sobrien
98130561Sobrienvoid
9991041Sobrienisc_time_settoepoch(isc_time_t *t);
10091041Sobrien/*
10191041Sobrien * Set 't' to the time of the epoch.
10291041Sobrien *
103130561Sobrien * Notes:
104130561Sobrien * 	The date of the epoch is platform-dependent.
105130561Sobrien *
106130561Sobrien * Requires:
107130561Sobrien *
108130561Sobrien *	't' is a valid pointer.
109130561Sobrien */
110130561Sobrien
111130561Sobrienisc_boolean_t
112130561Sobrienisc_time_isepoch(const isc_time_t *t);
11391041Sobrien/*
11491041Sobrien * Returns ISC_TRUE iff. 't' is the epoch ("time zero").
11591041Sobrien *
11691041Sobrien * Requires:
117130561Sobrien *
118130561Sobrien *	't' is a valid pointer.
119130561Sobrien */
120130561Sobrien
121130561Sobrienisc_result_t
122130561Sobrienisc_time_now(isc_time_t *t);
123130561Sobrien/*
124130561Sobrien * Set 't' to the current absolute time.
125130561Sobrien *
12691041Sobrien * Requires:
127130561Sobrien *
128130561Sobrien *	't' is a valid pointer.
129130561Sobrien *
130218822Sdim * Returns:
131130561Sobrien *
132130561Sobrien *	Success
133130561Sobrien *	Unexpected error
134130561Sobrien *		Getting the time from the system failed.
13591041Sobrien *	Out of range
13691041Sobrien *		The time from the system is too large to be represented
13791041Sobrien *		in the current definition of isc_time_t.
13891041Sobrien */
13991041Sobrien
14091041Sobrienisc_result_t
14191041Sobrienisc_time_nowplusinterval(isc_time_t *t, const isc_interval_t *i);
14291041Sobrien/*
14391041Sobrien * Set *t to the current absolute time + i.
14491041Sobrien *
14591041Sobrien * Note:
14691041Sobrien *	This call is equivalent to:
14791041Sobrien *
14891041Sobrien *		isc_time_now(t);
14991041Sobrien *		isc_time_add(t, i, t);
15091041Sobrien *
151130561Sobrien * Requires:
152130561Sobrien *
153130561Sobrien *	't' and 'i' are valid pointers.
15491041Sobrien *
155104834Sobrien * Returns:
156104834Sobrien *
157130561Sobrien *	Success
158130561Sobrien *	Unexpected error
159130561Sobrien *		Getting the time from the system failed.
160130561Sobrien *	Out of range
161130561Sobrien *		The interval added to the time from the system is too large to
162130561Sobrien *		be represented in the current definition of isc_time_t.
163130561Sobrien */
164218822Sdim
16591041Sobrienint
16691041Sobrienisc_time_compare(const isc_time_t *t1, const isc_time_t *t2);
167130561Sobrien/*
168130561Sobrien * Compare the times referenced by 't1' and 't2'
169130561Sobrien *
170130561Sobrien * Requires:
17191041Sobrien *
17291041Sobrien *	't1' and 't2' are valid pointers.
17391041Sobrien *
17491041Sobrien * Returns:
175130561Sobrien *
176130561Sobrien *	-1		t1 < t2		(comparing times, not pointers)
177130561Sobrien *	0		t1 = t2
178130561Sobrien *	1		t1 > t2
17991041Sobrien */
180104834Sobrien
181130561Sobrienisc_result_t
182130561Sobrienisc_time_add(const isc_time_t *t, const isc_interval_t *i, isc_time_t *result);
18391041Sobrien/*
184130561Sobrien * Add 'i' to 't', storing the result in 'result'.
185104834Sobrien *
186104834Sobrien * Requires:
187130561Sobrien *
188130561Sobrien *	't', 'i', and 'result' are valid pointers.
189130561Sobrien *
190130561Sobrien * Returns:
191104834Sobrien * 	Success
192130561Sobrien *	Out of range
193130561Sobrien * 		The interval added to the time is too large to
194130561Sobrien *		be represented in the current definition of isc_time_t.
195130561Sobrien */
196130561Sobrien
197104834Sobrienisc_result_t
198130561Sobrienisc_time_subtract(const isc_time_t *t, const isc_interval_t *i,
199130561Sobrien		  isc_time_t *result);
200130561Sobrien/*
201130561Sobrien * Subtract 'i' from 't', storing the result in 'result'.
202104834Sobrien *
203130561Sobrien * Requires:
204130561Sobrien *
205130561Sobrien *	't', 'i', and 'result' are valid pointers.
206130561Sobrien *
207104834Sobrien * Returns:
208104834Sobrien *	Success
209130561Sobrien *	Out of range
210104834Sobrien *		The interval is larger than the time since the epoch.
211130561Sobrien */
212130561Sobrien
213104834Sobrienisc_uint64_t
214130561Sobrienisc_time_microdiff(const isc_time_t *t1, const isc_time_t *t2);
215104834Sobrien/*
216104834Sobrien * Find the difference in milliseconds between time t1 and time t2.
217104834Sobrien * t2 is the subtrahend of t1; ie, difference = t1 - t2.
21891041Sobrien *
21991041Sobrien * Requires:
22091041Sobrien *
22191041Sobrien *	't1' and 't2' are valid pointers.
22291041Sobrien *
22391041Sobrien * Returns:
22491041Sobrien *	The difference of t1 - t2, or 0 if t1 <= t2.
22591041Sobrien */
22691041Sobrien
22791041Sobrienisc_uint32_t
22891041Sobrienisc_time_nanoseconds(const isc_time_t *t);
22991041Sobrien/*
23091041Sobrien * Return the number of nanoseconds stored in a time structure.
23191041Sobrien *
23291041Sobrien * Notes:
23391041Sobrien *	This is the number of nanoseconds in excess of the number
23491041Sobrien *	of seconds since the epoch; it will always be less than one
23591041Sobrien *	full second.
23691041Sobrien *
23791041Sobrien * Requires:
23891041Sobrien *	't' is a valid pointer.
23991041Sobrien *
240218822Sdim * Ensures:
24191041Sobrien *	The returned value is less than 1*10^9.
242130561Sobrien */
243130561Sobrien
244130561Sobrienvoid
245130561Sobrienisc_time_formattimestamp(const isc_time_t *t, char *buf, unsigned int len);
246130561Sobrien/*
247130561Sobrien * Format the time 't' into the buffer 'buf' of length 'len',
248130561Sobrien * using a format like "30-Aug-2000 04:06:47.997" and the local time zone.
249130561Sobrien * If the text does not fit in the buffer, the result is indeterminate,
25091041Sobrien * but is always guaranteed to be null terminated.
25191041Sobrien *
25291041Sobrien *  Requires:
25391041Sobrien *      'len' > 0
25491041Sobrien *      'buf' points to an array of at least len chars
25591041Sobrien *
25691041Sobrien */
25791041Sobrien
25891041Sobrienvoid
25991041Sobrienisc_time_formathttptimestamp(const isc_time_t *t, char *buf, unsigned int len);
26091041Sobrien/*
26191041Sobrien * Format the time 't' into the buffer 'buf' of length 'len',
26291041Sobrien * using a format like "Mon, 30 Aug 2000 04:06:47 GMT"
26391041Sobrien * If the text does not fit in the buffer, the result is indeterminate,
26491041Sobrien * but is always guaranteed to be null terminated.
26591041Sobrien *
26691041Sobrien *  Requires:
26791041Sobrien *      'len' > 0
26891041Sobrien *      'buf' points to an array of at least len chars
26991041Sobrien *
270104834Sobrien */
271104834Sobrien
272104834Sobrienvoid
27391041Sobrienisc_time_formatISO8601(const isc_time_t *t, char *buf, unsigned int len);
27491041Sobrien/*%<
27591041Sobrien * Format the time 't' into the buffer 'buf' of length 'len',
27691041Sobrien * using the ISO8601 format: "yyyy-mm-ddThh:mm:ssZ"
27791041Sobrien * If the text does not fit in the buffer, the result is indeterminate,
27891041Sobrien * but is always guaranteed to be null terminated.
27991041Sobrien *
28091041Sobrien *  Requires:
28191041Sobrien *\li      'len' > 0
28291041Sobrien *\li      'buf' points to an array of at least len chars
28391041Sobrien *
28491041Sobrien */
28591041Sobrien
28691041Sobrienisc_uint32_t
28791041Sobrienisc_time_seconds(const isc_time_t *t);
28891041Sobrien
28991041SobrienISC_LANG_ENDDECLS
29091041Sobrien
29191041Sobrien#endif /* ISC_TIME_H */
29291041Sobrien