1*a466cc55SCy Schubert /* 2*a466cc55SCy Schubert * Copyright (C) 2004, 2006-2009 Internet Systems Consortium, Inc. ("ISC") 3*a466cc55SCy Schubert * Copyright (C) 1998-2001 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.h,v 1.35 2009/01/05 23:47:54 tbox Exp $ */ 19*a466cc55SCy Schubert 20*a466cc55SCy Schubert #ifndef ISC_TIME_H 21*a466cc55SCy Schubert #define ISC_TIME_H 1 22*a466cc55SCy Schubert 23*a466cc55SCy Schubert #include <windows.h> 24*a466cc55SCy Schubert 25*a466cc55SCy Schubert #include <isc/lang.h> 26*a466cc55SCy Schubert #include <isc/types.h> 27*a466cc55SCy Schubert 28*a466cc55SCy Schubert /*** 29*a466cc55SCy Schubert *** Intervals 30*a466cc55SCy Schubert ***/ 31*a466cc55SCy Schubert 32*a466cc55SCy Schubert /* 33*a466cc55SCy Schubert * The contents of this structure are private, and MUST NOT be accessed 34*a466cc55SCy Schubert * directly by callers. 35*a466cc55SCy Schubert * 36*a466cc55SCy Schubert * The contents are exposed only to allow callers to avoid dynamic allocation. 37*a466cc55SCy Schubert */ 38*a466cc55SCy Schubert struct isc_interval { 39*a466cc55SCy Schubert isc_int64_t interval; 40*a466cc55SCy Schubert }; 41*a466cc55SCy Schubert 42*a466cc55SCy Schubert LIBISC_EXTERNAL_DATA extern isc_interval_t *isc_interval_zero; 43*a466cc55SCy Schubert 44*a466cc55SCy Schubert ISC_LANG_BEGINDECLS 45*a466cc55SCy Schubert 46*a466cc55SCy Schubert void 47*a466cc55SCy Schubert isc_interval_set(isc_interval_t *i, 48*a466cc55SCy Schubert unsigned int seconds, unsigned int nanoseconds); 49*a466cc55SCy Schubert /* 50*a466cc55SCy Schubert * Set 'i' to a value representing an interval of 'seconds' seconds and 51*a466cc55SCy Schubert * 'nanoseconds' nanoseconds, suitable for use in isc_time_add() and 52*a466cc55SCy Schubert * isc_time_subtract(). 53*a466cc55SCy Schubert * 54*a466cc55SCy Schubert * Requires: 55*a466cc55SCy Schubert * 56*a466cc55SCy Schubert * 't' is a valid pointer. 57*a466cc55SCy Schubert * nanoseconds < 1000000000. 58*a466cc55SCy Schubert */ 59*a466cc55SCy Schubert 60*a466cc55SCy Schubert isc_boolean_t 61*a466cc55SCy Schubert isc_interval_iszero(const isc_interval_t *i); 62*a466cc55SCy Schubert /* 63*a466cc55SCy Schubert * Returns ISC_TRUE iff. 'i' is the zero interval. 64*a466cc55SCy Schubert * 65*a466cc55SCy Schubert * Requires: 66*a466cc55SCy Schubert * 67*a466cc55SCy Schubert * 'i' is a valid pointer. 68*a466cc55SCy Schubert */ 69*a466cc55SCy Schubert 70*a466cc55SCy Schubert /*** 71*a466cc55SCy Schubert *** Absolute Times 72*a466cc55SCy Schubert ***/ 73*a466cc55SCy Schubert 74*a466cc55SCy Schubert /* 75*a466cc55SCy Schubert * The contents of this structure are private, and MUST NOT be accessed 76*a466cc55SCy Schubert * directly by callers. 77*a466cc55SCy Schubert * 78*a466cc55SCy Schubert * The contents are exposed only to allow callers to avoid dynamic allocation. 79*a466cc55SCy Schubert */ 80*a466cc55SCy Schubert 81*a466cc55SCy Schubert struct isc_time { 82*a466cc55SCy Schubert FILETIME absolute; 83*a466cc55SCy Schubert }; 84*a466cc55SCy Schubert 85*a466cc55SCy Schubert LIBISC_EXTERNAL_DATA extern isc_time_t *isc_time_epoch; 86*a466cc55SCy Schubert 87*a466cc55SCy Schubert void 88*a466cc55SCy Schubert isc_time_set(isc_time_t *t, unsigned int seconds, unsigned int nanoseconds); 89*a466cc55SCy Schubert /*%< 90*a466cc55SCy Schubert * Set 't' to a value which represents the given number of seconds and 91*a466cc55SCy Schubert * nanoseconds since 00:00:00 January 1, 1970, UTC. 92*a466cc55SCy Schubert * 93*a466cc55SCy Schubert * Requires: 94*a466cc55SCy Schubert *\li 't' is a valid pointer. 95*a466cc55SCy Schubert *\li nanoseconds < 1000000000. 96*a466cc55SCy Schubert */ 97*a466cc55SCy Schubert 98*a466cc55SCy Schubert void 99*a466cc55SCy Schubert isc_time_settoepoch(isc_time_t *t); 100*a466cc55SCy Schubert /* 101*a466cc55SCy Schubert * Set 't' to the time of the epoch. 102*a466cc55SCy Schubert * 103*a466cc55SCy Schubert * Notes: 104*a466cc55SCy Schubert * The date of the epoch is platform-dependent. 105*a466cc55SCy Schubert * 106*a466cc55SCy Schubert * Requires: 107*a466cc55SCy Schubert * 108*a466cc55SCy Schubert * 't' is a valid pointer. 109*a466cc55SCy Schubert */ 110*a466cc55SCy Schubert 111*a466cc55SCy Schubert isc_boolean_t 112*a466cc55SCy Schubert isc_time_isepoch(const isc_time_t *t); 113*a466cc55SCy Schubert /* 114*a466cc55SCy Schubert * Returns ISC_TRUE iff. 't' is the epoch ("time zero"). 115*a466cc55SCy Schubert * 116*a466cc55SCy Schubert * Requires: 117*a466cc55SCy Schubert * 118*a466cc55SCy Schubert * 't' is a valid pointer. 119*a466cc55SCy Schubert */ 120*a466cc55SCy Schubert 121*a466cc55SCy Schubert isc_result_t 122*a466cc55SCy Schubert isc_time_now(isc_time_t *t); 123*a466cc55SCy Schubert /* 124*a466cc55SCy Schubert * Set 't' to the current absolute time. 125*a466cc55SCy Schubert * 126*a466cc55SCy Schubert * Requires: 127*a466cc55SCy Schubert * 128*a466cc55SCy Schubert * 't' is a valid pointer. 129*a466cc55SCy Schubert * 130*a466cc55SCy Schubert * Returns: 131*a466cc55SCy Schubert * 132*a466cc55SCy Schubert * Success 133*a466cc55SCy Schubert * Unexpected error 134*a466cc55SCy Schubert * Getting the time from the system failed. 135*a466cc55SCy Schubert * Out of range 136*a466cc55SCy Schubert * The time from the system is too large to be represented 137*a466cc55SCy Schubert * in the current definition of isc_time_t. 138*a466cc55SCy Schubert */ 139*a466cc55SCy Schubert 140*a466cc55SCy Schubert isc_result_t 141*a466cc55SCy Schubert isc_time_nowplusinterval(isc_time_t *t, const isc_interval_t *i); 142*a466cc55SCy Schubert /* 143*a466cc55SCy Schubert * Set *t to the current absolute time + i. 144*a466cc55SCy Schubert * 145*a466cc55SCy Schubert * Note: 146*a466cc55SCy Schubert * This call is equivalent to: 147*a466cc55SCy Schubert * 148*a466cc55SCy Schubert * isc_time_now(t); 149*a466cc55SCy Schubert * isc_time_add(t, i, t); 150*a466cc55SCy Schubert * 151*a466cc55SCy Schubert * Requires: 152*a466cc55SCy Schubert * 153*a466cc55SCy Schubert * 't' and 'i' are valid pointers. 154*a466cc55SCy Schubert * 155*a466cc55SCy Schubert * Returns: 156*a466cc55SCy Schubert * 157*a466cc55SCy Schubert * Success 158*a466cc55SCy Schubert * Unexpected error 159*a466cc55SCy Schubert * Getting the time from the system failed. 160*a466cc55SCy Schubert * Out of range 161*a466cc55SCy Schubert * The interval added to the time from the system is too large to 162*a466cc55SCy Schubert * be represented in the current definition of isc_time_t. 163*a466cc55SCy Schubert */ 164*a466cc55SCy Schubert 165*a466cc55SCy Schubert int 166*a466cc55SCy Schubert isc_time_compare(const isc_time_t *t1, const isc_time_t *t2); 167*a466cc55SCy Schubert /* 168*a466cc55SCy Schubert * Compare the times referenced by 't1' and 't2' 169*a466cc55SCy Schubert * 170*a466cc55SCy Schubert * Requires: 171*a466cc55SCy Schubert * 172*a466cc55SCy Schubert * 't1' and 't2' are valid pointers. 173*a466cc55SCy Schubert * 174*a466cc55SCy Schubert * Returns: 175*a466cc55SCy Schubert * 176*a466cc55SCy Schubert * -1 t1 < t2 (comparing times, not pointers) 177*a466cc55SCy Schubert * 0 t1 = t2 178*a466cc55SCy Schubert * 1 t1 > t2 179*a466cc55SCy Schubert */ 180*a466cc55SCy Schubert 181*a466cc55SCy Schubert isc_result_t 182*a466cc55SCy Schubert isc_time_add(const isc_time_t *t, const isc_interval_t *i, isc_time_t *result); 183*a466cc55SCy Schubert /* 184*a466cc55SCy Schubert * Add 'i' to 't', storing the result in 'result'. 185*a466cc55SCy Schubert * 186*a466cc55SCy Schubert * Requires: 187*a466cc55SCy Schubert * 188*a466cc55SCy Schubert * 't', 'i', and 'result' are valid pointers. 189*a466cc55SCy Schubert * 190*a466cc55SCy Schubert * Returns: 191*a466cc55SCy Schubert * Success 192*a466cc55SCy Schubert * Out of range 193*a466cc55SCy Schubert * The interval added to the time is too large to 194*a466cc55SCy Schubert * be represented in the current definition of isc_time_t. 195*a466cc55SCy Schubert */ 196*a466cc55SCy Schubert 197*a466cc55SCy Schubert isc_result_t 198*a466cc55SCy Schubert isc_time_subtract(const isc_time_t *t, const isc_interval_t *i, 199*a466cc55SCy Schubert isc_time_t *result); 200*a466cc55SCy Schubert /* 201*a466cc55SCy Schubert * Subtract 'i' from 't', storing the result in 'result'. 202*a466cc55SCy Schubert * 203*a466cc55SCy Schubert * Requires: 204*a466cc55SCy Schubert * 205*a466cc55SCy Schubert * 't', 'i', and 'result' are valid pointers. 206*a466cc55SCy Schubert * 207*a466cc55SCy Schubert * Returns: 208*a466cc55SCy Schubert * Success 209*a466cc55SCy Schubert * Out of range 210*a466cc55SCy Schubert * The interval is larger than the time since the epoch. 211*a466cc55SCy Schubert */ 212*a466cc55SCy Schubert 213*a466cc55SCy Schubert isc_uint64_t 214*a466cc55SCy Schubert isc_time_microdiff(const isc_time_t *t1, const isc_time_t *t2); 215*a466cc55SCy Schubert /* 216*a466cc55SCy Schubert * Find the difference in milliseconds between time t1 and time t2. 217*a466cc55SCy Schubert * t2 is the subtrahend of t1; ie, difference = t1 - t2. 218*a466cc55SCy Schubert * 219*a466cc55SCy Schubert * Requires: 220*a466cc55SCy Schubert * 221*a466cc55SCy Schubert * 't1' and 't2' are valid pointers. 222*a466cc55SCy Schubert * 223*a466cc55SCy Schubert * Returns: 224*a466cc55SCy Schubert * The difference of t1 - t2, or 0 if t1 <= t2. 225*a466cc55SCy Schubert */ 226*a466cc55SCy Schubert 227*a466cc55SCy Schubert isc_uint32_t 228*a466cc55SCy Schubert isc_time_nanoseconds(const isc_time_t *t); 229*a466cc55SCy Schubert /* 230*a466cc55SCy Schubert * Return the number of nanoseconds stored in a time structure. 231*a466cc55SCy Schubert * 232*a466cc55SCy Schubert * Notes: 233*a466cc55SCy Schubert * This is the number of nanoseconds in excess of the number 234*a466cc55SCy Schubert * of seconds since the epoch; it will always be less than one 235*a466cc55SCy Schubert * full second. 236*a466cc55SCy Schubert * 237*a466cc55SCy Schubert * Requires: 238*a466cc55SCy Schubert * 't' is a valid pointer. 239*a466cc55SCy Schubert * 240*a466cc55SCy Schubert * Ensures: 241*a466cc55SCy Schubert * The returned value is less than 1*10^9. 242*a466cc55SCy Schubert */ 243*a466cc55SCy Schubert 244*a466cc55SCy Schubert void 245*a466cc55SCy Schubert isc_time_formattimestamp(const isc_time_t *t, char *buf, unsigned int len); 246*a466cc55SCy Schubert /* 247*a466cc55SCy Schubert * Format the time 't' into the buffer 'buf' of length 'len', 248*a466cc55SCy Schubert * using a format like "30-Aug-2000 04:06:47.997" and the local time zone. 249*a466cc55SCy Schubert * If the text does not fit in the buffer, the result is indeterminate, 250*a466cc55SCy Schubert * but is always guaranteed to be null terminated. 251*a466cc55SCy Schubert * 252*a466cc55SCy Schubert * Requires: 253*a466cc55SCy Schubert * 'len' > 0 254*a466cc55SCy Schubert * 'buf' points to an array of at least len chars 255*a466cc55SCy Schubert * 256*a466cc55SCy Schubert */ 257*a466cc55SCy Schubert 258*a466cc55SCy Schubert void 259*a466cc55SCy Schubert isc_time_formathttptimestamp(const isc_time_t *t, char *buf, unsigned int len); 260*a466cc55SCy Schubert /* 261*a466cc55SCy Schubert * Format the time 't' into the buffer 'buf' of length 'len', 262*a466cc55SCy Schubert * using a format like "Mon, 30 Aug 2000 04:06:47 GMT" 263*a466cc55SCy Schubert * If the text does not fit in the buffer, the result is indeterminate, 264*a466cc55SCy Schubert * but is always guaranteed to be null terminated. 265*a466cc55SCy Schubert * 266*a466cc55SCy Schubert * Requires: 267*a466cc55SCy Schubert * 'len' > 0 268*a466cc55SCy Schubert * 'buf' points to an array of at least len chars 269*a466cc55SCy Schubert * 270*a466cc55SCy Schubert */ 271*a466cc55SCy Schubert 272*a466cc55SCy Schubert void 273*a466cc55SCy Schubert isc_time_formatISO8601(const isc_time_t *t, char *buf, unsigned int len); 274*a466cc55SCy Schubert /*%< 275*a466cc55SCy Schubert * Format the time 't' into the buffer 'buf' of length 'len', 276*a466cc55SCy Schubert * using the ISO8601 format: "yyyy-mm-ddThh:mm:ssZ" 277*a466cc55SCy Schubert * If the text does not fit in the buffer, the result is indeterminate, 278*a466cc55SCy Schubert * but is always guaranteed to be null terminated. 279*a466cc55SCy Schubert * 280*a466cc55SCy Schubert * Requires: 281*a466cc55SCy Schubert *\li 'len' > 0 282*a466cc55SCy Schubert *\li 'buf' points to an array of at least len chars 283*a466cc55SCy Schubert * 284*a466cc55SCy Schubert */ 285*a466cc55SCy Schubert 286*a466cc55SCy Schubert isc_uint32_t 287*a466cc55SCy Schubert isc_time_seconds(const isc_time_t *t); 288*a466cc55SCy Schubert 289*a466cc55SCy Schubert ISC_LANG_ENDDECLS 290*a466cc55SCy Schubert 291*a466cc55SCy Schubert #endif /* ISC_TIME_H */ 292