1*a466cc55SCy Schubert /*
2*a466cc55SCy Schubert * Copyright (C) 2004, 2006-2009 Internet Systems Consortium, Inc. ("ISC")
3*a466cc55SCy Schubert * Copyright (C) 1998-2001, 2003 Internet Software Consortium.
4*a466cc55SCy Schubert *
5*a466cc55SCy Schubert * Permission to use, copy, modify, and/or distribute this software for any
6*a466cc55SCy Schubert * purpose with or without fee is hereby granted, provided that the above
7*a466cc55SCy Schubert * copyright notice and this permission notice appear in all copies.
8*a466cc55SCy Schubert *
9*a466cc55SCy Schubert * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*a466cc55SCy Schubert * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*a466cc55SCy Schubert * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*a466cc55SCy Schubert * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*a466cc55SCy Schubert * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*a466cc55SCy Schubert * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*a466cc55SCy Schubert * PERFORMANCE OF THIS SOFTWARE.
16*a466cc55SCy Schubert */
17*a466cc55SCy Schubert
18*a466cc55SCy Schubert /* $Id: time.c,v 1.52 2009/08/14 07:51:08 marka Exp $ */
19*a466cc55SCy Schubert
20*a466cc55SCy Schubert #include <config.h>
21*a466cc55SCy Schubert
22*a466cc55SCy Schubert #include <errno.h>
23*a466cc55SCy Schubert #include <limits.h>
24*a466cc55SCy Schubert #include <stddef.h>
25*a466cc55SCy Schubert #include <stdlib.h>
26*a466cc55SCy Schubert #include <string.h>
27*a466cc55SCy Schubert #include <time.h>
28*a466cc55SCy Schubert
29*a466cc55SCy Schubert #include <windows.h>
30*a466cc55SCy Schubert
31*a466cc55SCy Schubert #include <isc/assertions.h>
32*a466cc55SCy Schubert #include <isc/time.h>
33*a466cc55SCy Schubert #include <isc/util.h>
34*a466cc55SCy Schubert
35*a466cc55SCy Schubert /*
36*a466cc55SCy Schubert * struct FILETIME uses "100-nanoseconds intervals".
37*a466cc55SCy Schubert * NS / S = 1000000000 (10^9).
38*a466cc55SCy Schubert * While it is reasonably obvious that this makes the needed
39*a466cc55SCy Schubert * conversion factor 10^7, it is coded this way for additional clarity.
40*a466cc55SCy Schubert */
41*a466cc55SCy Schubert #define NS_PER_S 1000000000
42*a466cc55SCy Schubert #define NS_INTERVAL 100
43*a466cc55SCy Schubert #define INTERVALS_PER_S (NS_PER_S / NS_INTERVAL)
44*a466cc55SCy Schubert #define UINT64_MAX _UI64_MAX
45*a466cc55SCy Schubert
46*a466cc55SCy Schubert /***
47*a466cc55SCy Schubert *** Absolute Times
48*a466cc55SCy Schubert ***/
49*a466cc55SCy Schubert
50*a466cc55SCy Schubert static isc_time_t epoch = { { 0, 0 } };
51*a466cc55SCy Schubert LIBISC_EXTERNAL_DATA isc_time_t *isc_time_epoch = &epoch;
52*a466cc55SCy Schubert
53*a466cc55SCy Schubert /***
54*a466cc55SCy Schubert *** Intervals
55*a466cc55SCy Schubert ***/
56*a466cc55SCy Schubert
57*a466cc55SCy Schubert static isc_interval_t zero_interval = { 0 };
58*a466cc55SCy Schubert LIBISC_EXTERNAL_DATA isc_interval_t *isc_interval_zero = &zero_interval;
59*a466cc55SCy Schubert
60*a466cc55SCy Schubert void
isc_interval_set(isc_interval_t * i,unsigned int seconds,unsigned int nanoseconds)61*a466cc55SCy Schubert isc_interval_set(isc_interval_t *i, unsigned int seconds,
62*a466cc55SCy Schubert unsigned int nanoseconds)
63*a466cc55SCy Schubert {
64*a466cc55SCy Schubert REQUIRE(i != NULL);
65*a466cc55SCy Schubert REQUIRE(nanoseconds < NS_PER_S);
66*a466cc55SCy Schubert
67*a466cc55SCy Schubert /*
68*a466cc55SCy Schubert * This rounds nanoseconds up not down.
69*a466cc55SCy Schubert */
70*a466cc55SCy Schubert i->interval = (LONGLONG)seconds * INTERVALS_PER_S
71*a466cc55SCy Schubert + (nanoseconds + NS_INTERVAL - 1) / NS_INTERVAL;
72*a466cc55SCy Schubert }
73*a466cc55SCy Schubert
74*a466cc55SCy Schubert isc_boolean_t
isc_interval_iszero(const isc_interval_t * i)75*a466cc55SCy Schubert isc_interval_iszero(const isc_interval_t *i) {
76*a466cc55SCy Schubert REQUIRE(i != NULL);
77*a466cc55SCy Schubert if (i->interval == 0)
78*a466cc55SCy Schubert return (ISC_TRUE);
79*a466cc55SCy Schubert
80*a466cc55SCy Schubert return (ISC_FALSE);
81*a466cc55SCy Schubert }
82*a466cc55SCy Schubert
83*a466cc55SCy Schubert void
isc_time_set(isc_time_t * t,unsigned int seconds,unsigned int nanoseconds)84*a466cc55SCy Schubert isc_time_set(isc_time_t *t, unsigned int seconds, unsigned int nanoseconds) {
85*a466cc55SCy Schubert SYSTEMTIME epoch = { 1970, 1, 4, 1, 0, 0, 0, 0 };
86*a466cc55SCy Schubert FILETIME temp;
87*a466cc55SCy Schubert ULARGE_INTEGER i1;
88*a466cc55SCy Schubert
89*a466cc55SCy Schubert REQUIRE(t != NULL);
90*a466cc55SCy Schubert REQUIRE(nanoseconds < NS_PER_S);
91*a466cc55SCy Schubert
92*a466cc55SCy Schubert SystemTimeToFileTime(&epoch, &temp);
93*a466cc55SCy Schubert
94*a466cc55SCy Schubert i1.LowPart = t->absolute.dwLowDateTime;
95*a466cc55SCy Schubert i1.HighPart = t->absolute.dwHighDateTime;
96*a466cc55SCy Schubert
97*a466cc55SCy Schubert i1.QuadPart += (unsigned __int64)nanoseconds/100;
98*a466cc55SCy Schubert i1.QuadPart += (unsigned __int64)seconds*10000000;
99*a466cc55SCy Schubert
100*a466cc55SCy Schubert t->absolute.dwLowDateTime = i1.LowPart;
101*a466cc55SCy Schubert t->absolute.dwHighDateTime = i1.HighPart;
102*a466cc55SCy Schubert }
103*a466cc55SCy Schubert
104*a466cc55SCy Schubert void
isc_time_settoepoch(isc_time_t * t)105*a466cc55SCy Schubert isc_time_settoepoch(isc_time_t *t) {
106*a466cc55SCy Schubert REQUIRE(t != NULL);
107*a466cc55SCy Schubert
108*a466cc55SCy Schubert t->absolute.dwLowDateTime = 0;
109*a466cc55SCy Schubert t->absolute.dwHighDateTime = 0;
110*a466cc55SCy Schubert }
111*a466cc55SCy Schubert
112*a466cc55SCy Schubert isc_boolean_t
isc_time_isepoch(const isc_time_t * t)113*a466cc55SCy Schubert isc_time_isepoch(const isc_time_t *t) {
114*a466cc55SCy Schubert REQUIRE(t != NULL);
115*a466cc55SCy Schubert
116*a466cc55SCy Schubert if (t->absolute.dwLowDateTime == 0 &&
117*a466cc55SCy Schubert t->absolute.dwHighDateTime == 0)
118*a466cc55SCy Schubert return (ISC_TRUE);
119*a466cc55SCy Schubert
120*a466cc55SCy Schubert return (ISC_FALSE);
121*a466cc55SCy Schubert }
122*a466cc55SCy Schubert
123*a466cc55SCy Schubert isc_result_t
isc_time_now(isc_time_t * t)124*a466cc55SCy Schubert isc_time_now(isc_time_t *t) {
125*a466cc55SCy Schubert REQUIRE(t != NULL);
126*a466cc55SCy Schubert
127*a466cc55SCy Schubert GetSystemTimeAsFileTime(&t->absolute);
128*a466cc55SCy Schubert
129*a466cc55SCy Schubert return (ISC_R_SUCCESS);
130*a466cc55SCy Schubert }
131*a466cc55SCy Schubert
132*a466cc55SCy Schubert isc_result_t
isc_time_nowplusinterval(isc_time_t * t,const isc_interval_t * i)133*a466cc55SCy Schubert isc_time_nowplusinterval(isc_time_t *t, const isc_interval_t *i) {
134*a466cc55SCy Schubert ULARGE_INTEGER i1;
135*a466cc55SCy Schubert
136*a466cc55SCy Schubert REQUIRE(t != NULL);
137*a466cc55SCy Schubert REQUIRE(i != NULL);
138*a466cc55SCy Schubert
139*a466cc55SCy Schubert GetSystemTimeAsFileTime(&t->absolute);
140*a466cc55SCy Schubert
141*a466cc55SCy Schubert i1.LowPart = t->absolute.dwLowDateTime;
142*a466cc55SCy Schubert i1.HighPart = t->absolute.dwHighDateTime;
143*a466cc55SCy Schubert
144*a466cc55SCy Schubert if (UINT64_MAX - i1.QuadPart < (unsigned __int64)i->interval)
145*a466cc55SCy Schubert return (ISC_R_RANGE);
146*a466cc55SCy Schubert
147*a466cc55SCy Schubert i1.QuadPart += i->interval;
148*a466cc55SCy Schubert
149*a466cc55SCy Schubert t->absolute.dwLowDateTime = i1.LowPart;
150*a466cc55SCy Schubert t->absolute.dwHighDateTime = i1.HighPart;
151*a466cc55SCy Schubert
152*a466cc55SCy Schubert return (ISC_R_SUCCESS);
153*a466cc55SCy Schubert }
154*a466cc55SCy Schubert
155*a466cc55SCy Schubert int
isc_time_compare(const isc_time_t * t1,const isc_time_t * t2)156*a466cc55SCy Schubert isc_time_compare(const isc_time_t *t1, const isc_time_t *t2) {
157*a466cc55SCy Schubert REQUIRE(t1 != NULL && t2 != NULL);
158*a466cc55SCy Schubert
159*a466cc55SCy Schubert return ((int)CompareFileTime(&t1->absolute, &t2->absolute));
160*a466cc55SCy Schubert }
161*a466cc55SCy Schubert
162*a466cc55SCy Schubert isc_result_t
isc_time_add(const isc_time_t * t,const isc_interval_t * i,isc_time_t * result)163*a466cc55SCy Schubert isc_time_add(const isc_time_t *t, const isc_interval_t *i, isc_time_t *result)
164*a466cc55SCy Schubert {
165*a466cc55SCy Schubert ULARGE_INTEGER i1;
166*a466cc55SCy Schubert
167*a466cc55SCy Schubert REQUIRE(t != NULL && i != NULL && result != NULL);
168*a466cc55SCy Schubert
169*a466cc55SCy Schubert i1.LowPart = t->absolute.dwLowDateTime;
170*a466cc55SCy Schubert i1.HighPart = t->absolute.dwHighDateTime;
171*a466cc55SCy Schubert
172*a466cc55SCy Schubert if (UINT64_MAX - i1.QuadPart < (unsigned __int64)i->interval)
173*a466cc55SCy Schubert return (ISC_R_RANGE);
174*a466cc55SCy Schubert
175*a466cc55SCy Schubert i1.QuadPart += i->interval;
176*a466cc55SCy Schubert
177*a466cc55SCy Schubert result->absolute.dwLowDateTime = i1.LowPart;
178*a466cc55SCy Schubert result->absolute.dwHighDateTime = i1.HighPart;
179*a466cc55SCy Schubert
180*a466cc55SCy Schubert return (ISC_R_SUCCESS);
181*a466cc55SCy Schubert }
182*a466cc55SCy Schubert
183*a466cc55SCy Schubert isc_result_t
isc_time_subtract(const isc_time_t * t,const isc_interval_t * i,isc_time_t * result)184*a466cc55SCy Schubert isc_time_subtract(const isc_time_t *t, const isc_interval_t *i,
185*a466cc55SCy Schubert isc_time_t *result) {
186*a466cc55SCy Schubert ULARGE_INTEGER i1;
187*a466cc55SCy Schubert
188*a466cc55SCy Schubert REQUIRE(t != NULL && i != NULL && result != NULL);
189*a466cc55SCy Schubert
190*a466cc55SCy Schubert i1.LowPart = t->absolute.dwLowDateTime;
191*a466cc55SCy Schubert i1.HighPart = t->absolute.dwHighDateTime;
192*a466cc55SCy Schubert
193*a466cc55SCy Schubert if (i1.QuadPart < (unsigned __int64) i->interval)
194*a466cc55SCy Schubert return (ISC_R_RANGE);
195*a466cc55SCy Schubert
196*a466cc55SCy Schubert i1.QuadPart -= i->interval;
197*a466cc55SCy Schubert
198*a466cc55SCy Schubert result->absolute.dwLowDateTime = i1.LowPart;
199*a466cc55SCy Schubert result->absolute.dwHighDateTime = i1.HighPart;
200*a466cc55SCy Schubert
201*a466cc55SCy Schubert return (ISC_R_SUCCESS);
202*a466cc55SCy Schubert }
203*a466cc55SCy Schubert
204*a466cc55SCy Schubert isc_uint64_t
isc_time_microdiff(const isc_time_t * t1,const isc_time_t * t2)205*a466cc55SCy Schubert isc_time_microdiff(const isc_time_t *t1, const isc_time_t *t2) {
206*a466cc55SCy Schubert ULARGE_INTEGER i1, i2;
207*a466cc55SCy Schubert LONGLONG i3;
208*a466cc55SCy Schubert
209*a466cc55SCy Schubert REQUIRE(t1 != NULL && t2 != NULL);
210*a466cc55SCy Schubert
211*a466cc55SCy Schubert i1.LowPart = t1->absolute.dwLowDateTime;
212*a466cc55SCy Schubert i1.HighPart = t1->absolute.dwHighDateTime;
213*a466cc55SCy Schubert i2.LowPart = t2->absolute.dwLowDateTime;
214*a466cc55SCy Schubert i2.HighPart = t2->absolute.dwHighDateTime;
215*a466cc55SCy Schubert
216*a466cc55SCy Schubert if (i1.QuadPart <= i2.QuadPart)
217*a466cc55SCy Schubert return (0);
218*a466cc55SCy Schubert
219*a466cc55SCy Schubert /*
220*a466cc55SCy Schubert * Convert to microseconds.
221*a466cc55SCy Schubert */
222*a466cc55SCy Schubert i3 = (i1.QuadPart - i2.QuadPart) / 10;
223*a466cc55SCy Schubert
224*a466cc55SCy Schubert return (i3);
225*a466cc55SCy Schubert }
226*a466cc55SCy Schubert
227*a466cc55SCy Schubert isc_uint32_t
isc_time_seconds(const isc_time_t * t)228*a466cc55SCy Schubert isc_time_seconds(const isc_time_t *t) {
229*a466cc55SCy Schubert SYSTEMTIME epoch = { 1970, 1, 4, 1, 0, 0, 0, 0 };
230*a466cc55SCy Schubert FILETIME temp;
231*a466cc55SCy Schubert ULARGE_INTEGER i1, i2;
232*a466cc55SCy Schubert LONGLONG i3;
233*a466cc55SCy Schubert
234*a466cc55SCy Schubert SystemTimeToFileTime(&epoch, &temp);
235*a466cc55SCy Schubert
236*a466cc55SCy Schubert i1.LowPart = t->absolute.dwLowDateTime;
237*a466cc55SCy Schubert i1.HighPart = t->absolute.dwHighDateTime;
238*a466cc55SCy Schubert i2.LowPart = temp.dwLowDateTime;
239*a466cc55SCy Schubert i2.HighPart = temp.dwHighDateTime;
240*a466cc55SCy Schubert
241*a466cc55SCy Schubert i3 = (i1.QuadPart - i2.QuadPart) / 10000000;
242*a466cc55SCy Schubert
243*a466cc55SCy Schubert return ((isc_uint32_t)i3);
244*a466cc55SCy Schubert }
245*a466cc55SCy Schubert
246*a466cc55SCy Schubert isc_uint32_t
isc_time_nanoseconds(const isc_time_t * t)247*a466cc55SCy Schubert isc_time_nanoseconds(const isc_time_t *t) {
248*a466cc55SCy Schubert ULARGE_INTEGER i;
249*a466cc55SCy Schubert
250*a466cc55SCy Schubert i.LowPart = t->absolute.dwLowDateTime;
251*a466cc55SCy Schubert i.HighPart = t->absolute.dwHighDateTime;
252*a466cc55SCy Schubert return ((isc_uint32_t)(i.QuadPart % 10000000) * 100);
253*a466cc55SCy Schubert }
254*a466cc55SCy Schubert
255*a466cc55SCy Schubert void
isc_time_formattimestamp(const isc_time_t * t,char * buf,unsigned int len)256*a466cc55SCy Schubert isc_time_formattimestamp(const isc_time_t *t, char *buf, unsigned int len) {
257*a466cc55SCy Schubert FILETIME localft;
258*a466cc55SCy Schubert SYSTEMTIME st;
259*a466cc55SCy Schubert char DateBuf[50];
260*a466cc55SCy Schubert char TimeBuf[50];
261*a466cc55SCy Schubert
262*a466cc55SCy Schubert static const char badtime[] = "99-Bad-9999 99:99:99.999";
263*a466cc55SCy Schubert
264*a466cc55SCy Schubert REQUIRE(len > 0);
265*a466cc55SCy Schubert if (FileTimeToLocalFileTime(&t->absolute, &localft) &&
266*a466cc55SCy Schubert FileTimeToSystemTime(&localft, &st)) {
267*a466cc55SCy Schubert GetDateFormat(LOCALE_USER_DEFAULT, 0, &st, "dd-MMM-yyyy",
268*a466cc55SCy Schubert DateBuf, 50);
269*a466cc55SCy Schubert GetTimeFormat(LOCALE_USER_DEFAULT, TIME_NOTIMEMARKER|
270*a466cc55SCy Schubert TIME_FORCE24HOURFORMAT, &st, NULL, TimeBuf, 50);
271*a466cc55SCy Schubert
272*a466cc55SCy Schubert snprintf(buf, len, "%s %s.%03u", DateBuf, TimeBuf,
273*a466cc55SCy Schubert st.wMilliseconds);
274*a466cc55SCy Schubert
275*a466cc55SCy Schubert } else
276*a466cc55SCy Schubert snprintf(buf, len, badtime);
277*a466cc55SCy Schubert }
278*a466cc55SCy Schubert
279*a466cc55SCy Schubert void
isc_time_formathttptimestamp(const isc_time_t * t,char * buf,unsigned int len)280*a466cc55SCy Schubert isc_time_formathttptimestamp(const isc_time_t *t, char *buf, unsigned int len) {
281*a466cc55SCy Schubert SYSTEMTIME st;
282*a466cc55SCy Schubert char DateBuf[50];
283*a466cc55SCy Schubert char TimeBuf[50];
284*a466cc55SCy Schubert
285*a466cc55SCy Schubert /* strftime() format: "%a, %d %b %Y %H:%M:%S GMT" */
286*a466cc55SCy Schubert
287*a466cc55SCy Schubert REQUIRE(len > 0);
288*a466cc55SCy Schubert if (FileTimeToSystemTime(&t->absolute, &st)) {
289*a466cc55SCy Schubert GetDateFormat(LOCALE_USER_DEFAULT, 0, &st,
290*a466cc55SCy Schubert "ddd',', dd-MMM-yyyy", DateBuf, 50);
291*a466cc55SCy Schubert GetTimeFormat(LOCALE_USER_DEFAULT,
292*a466cc55SCy Schubert TIME_NOTIMEMARKER | TIME_FORCE24HOURFORMAT,
293*a466cc55SCy Schubert &st, "hh':'mm':'ss", TimeBuf, 50);
294*a466cc55SCy Schubert
295*a466cc55SCy Schubert snprintf(buf, len, "%s %s GMT", DateBuf, TimeBuf);
296*a466cc55SCy Schubert } else {
297*a466cc55SCy Schubert buf[0] = 0;
298*a466cc55SCy Schubert }
299*a466cc55SCy Schubert }
300*a466cc55SCy Schubert
301*a466cc55SCy Schubert void
isc_time_formatISO8601(const isc_time_t * t,char * buf,unsigned int len)302*a466cc55SCy Schubert isc_time_formatISO8601(const isc_time_t *t, char *buf, unsigned int len) {
303*a466cc55SCy Schubert SYSTEMTIME st;
304*a466cc55SCy Schubert char DateBuf[50];
305*a466cc55SCy Schubert char TimeBuf[50];
306*a466cc55SCy Schubert
307*a466cc55SCy Schubert /* strtime() format: "%Y-%m-%dT%H:%M:%SZ" */
308*a466cc55SCy Schubert
309*a466cc55SCy Schubert REQUIRE(len > 0);
310*a466cc55SCy Schubert if (FileTimeToSystemTime(&t->absolute, &st)) {
311*a466cc55SCy Schubert GetDateFormat(LOCALE_NEUTRAL, 0, &st, "yyyy-MM-dd",
312*a466cc55SCy Schubert DateBuf, 50);
313*a466cc55SCy Schubert GetTimeFormat(LOCALE_NEUTRAL,
314*a466cc55SCy Schubert TIME_NOTIMEMARKER | TIME_FORCE24HOURFORMAT,
315*a466cc55SCy Schubert &st, "hh':'mm':'ss", TimeBuf, 50);
316*a466cc55SCy Schubert snprintf(buf, len, "%s%sZ", DateBuf, TimeBuf);
317*a466cc55SCy Schubert } else {
318*a466cc55SCy Schubert buf[0] = 0;
319*a466cc55SCy Schubert }
320*a466cc55SCy Schubert }
321