1290001Sglebius/* 2290001Sglebius * Copyright (C) 2004, 2006-2009 Internet Systems Consortium, Inc. ("ISC") 3290001Sglebius * Copyright (C) 1998-2001 Internet Software Consortium. 4290001Sglebius * 5290001Sglebius * Permission to use, copy, modify, and/or distribute this software for any 6290001Sglebius * purpose with or without fee is hereby granted, provided that the above 7290001Sglebius * copyright notice and this permission notice appear in all copies. 8290001Sglebius * 9290001Sglebius * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 10290001Sglebius * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 11290001Sglebius * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 12290001Sglebius * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 13290001Sglebius * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 14290001Sglebius * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 15290001Sglebius * PERFORMANCE OF THIS SOFTWARE. 16290001Sglebius */ 17290001Sglebius 18290001Sglebius/* $Id: time.h,v 1.35 2009/01/05 23:47:54 tbox Exp $ */ 19290001Sglebius 20290001Sglebius#ifndef ISC_TIME_H 21290001Sglebius#define ISC_TIME_H 1 22290001Sglebius 23290001Sglebius#include <windows.h> 24290001Sglebius 25290001Sglebius#include <isc/lang.h> 26290001Sglebius#include <isc/types.h> 27290001Sglebius 28290001Sglebius/*** 29290001Sglebius *** Intervals 30290001Sglebius ***/ 31290001Sglebius 32290001Sglebius/* 33290001Sglebius * The contents of this structure are private, and MUST NOT be accessed 34290001Sglebius * directly by callers. 35290001Sglebius * 36290001Sglebius * The contents are exposed only to allow callers to avoid dynamic allocation. 37290001Sglebius */ 38290001Sglebiusstruct isc_interval { 39290001Sglebius isc_int64_t interval; 40290001Sglebius}; 41290001Sglebius 42290001SglebiusLIBISC_EXTERNAL_DATA extern isc_interval_t *isc_interval_zero; 43290001Sglebius 44290001SglebiusISC_LANG_BEGINDECLS 45290001Sglebius 46290001Sglebiusvoid 47290001Sglebiusisc_interval_set(isc_interval_t *i, 48290001Sglebius unsigned int seconds, unsigned int nanoseconds); 49290001Sglebius/* 50290001Sglebius * Set 'i' to a value representing an interval of 'seconds' seconds and 51290001Sglebius * 'nanoseconds' nanoseconds, suitable for use in isc_time_add() and 52290001Sglebius * isc_time_subtract(). 53290001Sglebius * 54290001Sglebius * Requires: 55290001Sglebius * 56290001Sglebius * 't' is a valid pointer. 57290001Sglebius * nanoseconds < 1000000000. 58290001Sglebius */ 59290001Sglebius 60290001Sglebiusisc_boolean_t 61290001Sglebiusisc_interval_iszero(const isc_interval_t *i); 62290001Sglebius/* 63290001Sglebius * Returns ISC_TRUE iff. 'i' is the zero interval. 64290001Sglebius * 65290001Sglebius * Requires: 66290001Sglebius * 67290001Sglebius * 'i' is a valid pointer. 68290001Sglebius */ 69290001Sglebius 70290001Sglebius/*** 71290001Sglebius *** Absolute Times 72290001Sglebius ***/ 73290001Sglebius 74290001Sglebius/* 75290001Sglebius * The contents of this structure are private, and MUST NOT be accessed 76290001Sglebius * directly by callers. 77290001Sglebius * 78290001Sglebius * The contents are exposed only to allow callers to avoid dynamic allocation. 79290001Sglebius */ 80290001Sglebius 81290001Sglebiusstruct isc_time { 82290001Sglebius FILETIME absolute; 83290001Sglebius}; 84290001Sglebius 85290001SglebiusLIBISC_EXTERNAL_DATA extern isc_time_t *isc_time_epoch; 86290001Sglebius 87290001Sglebiusvoid 88290001Sglebiusisc_time_set(isc_time_t *t, unsigned int seconds, unsigned int nanoseconds); 89290001Sglebius/*%< 90290001Sglebius * Set 't' to a value which represents the given number of seconds and 91290001Sglebius * nanoseconds since 00:00:00 January 1, 1970, UTC. 92290001Sglebius * 93290001Sglebius * Requires: 94290001Sglebius *\li 't' is a valid pointer. 95290001Sglebius *\li nanoseconds < 1000000000. 96290001Sglebius */ 97290001Sglebius 98290001Sglebiusvoid 99290001Sglebiusisc_time_settoepoch(isc_time_t *t); 100290001Sglebius/* 101290001Sglebius * Set 't' to the time of the epoch. 102290001Sglebius * 103290001Sglebius * Notes: 104290001Sglebius * The date of the epoch is platform-dependent. 105290001Sglebius * 106290001Sglebius * Requires: 107290001Sglebius * 108290001Sglebius * 't' is a valid pointer. 109290001Sglebius */ 110290001Sglebius 111290001Sglebiusisc_boolean_t 112290001Sglebiusisc_time_isepoch(const isc_time_t *t); 113290001Sglebius/* 114290001Sglebius * Returns ISC_TRUE iff. 't' is the epoch ("time zero"). 115290001Sglebius * 116290001Sglebius * Requires: 117290001Sglebius * 118290001Sglebius * 't' is a valid pointer. 119290001Sglebius */ 120290001Sglebius 121290001Sglebiusisc_result_t 122290001Sglebiusisc_time_now(isc_time_t *t); 123290001Sglebius/* 124290001Sglebius * Set 't' to the current absolute time. 125290001Sglebius * 126290001Sglebius * Requires: 127290001Sglebius * 128290001Sglebius * 't' is a valid pointer. 129290001Sglebius * 130290001Sglebius * Returns: 131290001Sglebius * 132290001Sglebius * Success 133290001Sglebius * Unexpected error 134290001Sglebius * Getting the time from the system failed. 135290001Sglebius * Out of range 136290001Sglebius * The time from the system is too large to be represented 137290001Sglebius * in the current definition of isc_time_t. 138290001Sglebius */ 139290001Sglebius 140290001Sglebiusisc_result_t 141290001Sglebiusisc_time_nowplusinterval(isc_time_t *t, const isc_interval_t *i); 142290001Sglebius/* 143290001Sglebius * Set *t to the current absolute time + i. 144290001Sglebius * 145290001Sglebius * Note: 146290001Sglebius * This call is equivalent to: 147290001Sglebius * 148290001Sglebius * isc_time_now(t); 149290001Sglebius * isc_time_add(t, i, t); 150290001Sglebius * 151290001Sglebius * Requires: 152290001Sglebius * 153290001Sglebius * 't' and 'i' are valid pointers. 154290001Sglebius * 155290001Sglebius * Returns: 156290001Sglebius * 157290001Sglebius * Success 158290001Sglebius * Unexpected error 159290001Sglebius * Getting the time from the system failed. 160290001Sglebius * Out of range 161290001Sglebius * The interval added to the time from the system is too large to 162290001Sglebius * be represented in the current definition of isc_time_t. 163290001Sglebius */ 164290001Sglebius 165290001Sglebiusint 166290001Sglebiusisc_time_compare(const isc_time_t *t1, const isc_time_t *t2); 167290001Sglebius/* 168290001Sglebius * Compare the times referenced by 't1' and 't2' 169290001Sglebius * 170290001Sglebius * Requires: 171290001Sglebius * 172290001Sglebius * 't1' and 't2' are valid pointers. 173290001Sglebius * 174290001Sglebius * Returns: 175290001Sglebius * 176290001Sglebius * -1 t1 < t2 (comparing times, not pointers) 177290001Sglebius * 0 t1 = t2 178290001Sglebius * 1 t1 > t2 179290001Sglebius */ 180290001Sglebius 181290001Sglebiusisc_result_t 182290001Sglebiusisc_time_add(const isc_time_t *t, const isc_interval_t *i, isc_time_t *result); 183290001Sglebius/* 184290001Sglebius * Add 'i' to 't', storing the result in 'result'. 185290001Sglebius * 186290001Sglebius * Requires: 187290001Sglebius * 188290001Sglebius * 't', 'i', and 'result' are valid pointers. 189290001Sglebius * 190290001Sglebius * Returns: 191290001Sglebius * Success 192290001Sglebius * Out of range 193290001Sglebius * The interval added to the time is too large to 194290001Sglebius * be represented in the current definition of isc_time_t. 195290001Sglebius */ 196290001Sglebius 197290001Sglebiusisc_result_t 198290001Sglebiusisc_time_subtract(const isc_time_t *t, const isc_interval_t *i, 199290001Sglebius isc_time_t *result); 200290001Sglebius/* 201290001Sglebius * Subtract 'i' from 't', storing the result in 'result'. 202290001Sglebius * 203290001Sglebius * Requires: 204290001Sglebius * 205290001Sglebius * 't', 'i', and 'result' are valid pointers. 206290001Sglebius * 207290001Sglebius * Returns: 208290001Sglebius * Success 209290001Sglebius * Out of range 210290001Sglebius * The interval is larger than the time since the epoch. 211290001Sglebius */ 212290001Sglebius 213290001Sglebiusisc_uint64_t 214290001Sglebiusisc_time_microdiff(const isc_time_t *t1, const isc_time_t *t2); 215290001Sglebius/* 216290001Sglebius * Find the difference in milliseconds between time t1 and time t2. 217290001Sglebius * t2 is the subtrahend of t1; ie, difference = t1 - t2. 218290001Sglebius * 219290001Sglebius * Requires: 220290001Sglebius * 221290001Sglebius * 't1' and 't2' are valid pointers. 222290001Sglebius * 223290001Sglebius * Returns: 224290001Sglebius * The difference of t1 - t2, or 0 if t1 <= t2. 225290001Sglebius */ 226290001Sglebius 227290001Sglebiusisc_uint32_t 228290001Sglebiusisc_time_nanoseconds(const isc_time_t *t); 229290001Sglebius/* 230290001Sglebius * Return the number of nanoseconds stored in a time structure. 231290001Sglebius * 232290001Sglebius * Notes: 233290001Sglebius * This is the number of nanoseconds in excess of the number 234290001Sglebius * of seconds since the epoch; it will always be less than one 235290001Sglebius * full second. 236290001Sglebius * 237290001Sglebius * Requires: 238290001Sglebius * 't' is a valid pointer. 239290001Sglebius * 240290001Sglebius * Ensures: 241290001Sglebius * The returned value is less than 1*10^9. 242290001Sglebius */ 243290001Sglebius 244290001Sglebiusvoid 245290001Sglebiusisc_time_formattimestamp(const isc_time_t *t, char *buf, unsigned int len); 246290001Sglebius/* 247290001Sglebius * Format the time 't' into the buffer 'buf' of length 'len', 248290001Sglebius * using a format like "30-Aug-2000 04:06:47.997" and the local time zone. 249290001Sglebius * If the text does not fit in the buffer, the result is indeterminate, 250290001Sglebius * but is always guaranteed to be null terminated. 251290001Sglebius * 252290001Sglebius * Requires: 253290001Sglebius * 'len' > 0 254290001Sglebius * 'buf' points to an array of at least len chars 255290001Sglebius * 256290001Sglebius */ 257290001Sglebius 258290001Sglebiusvoid 259290001Sglebiusisc_time_formathttptimestamp(const isc_time_t *t, char *buf, unsigned int len); 260290001Sglebius/* 261290001Sglebius * Format the time 't' into the buffer 'buf' of length 'len', 262290001Sglebius * using a format like "Mon, 30 Aug 2000 04:06:47 GMT" 263290001Sglebius * If the text does not fit in the buffer, the result is indeterminate, 264290001Sglebius * but is always guaranteed to be null terminated. 265290001Sglebius * 266290001Sglebius * Requires: 267290001Sglebius * 'len' > 0 268290001Sglebius * 'buf' points to an array of at least len chars 269290001Sglebius * 270290001Sglebius */ 271290001Sglebius 272290001Sglebiusvoid 273290001Sglebiusisc_time_formatISO8601(const isc_time_t *t, char *buf, unsigned int len); 274290001Sglebius/*%< 275290001Sglebius * Format the time 't' into the buffer 'buf' of length 'len', 276290001Sglebius * using the ISO8601 format: "yyyy-mm-ddThh:mm:ssZ" 277290001Sglebius * If the text does not fit in the buffer, the result is indeterminate, 278290001Sglebius * but is always guaranteed to be null terminated. 279290001Sglebius * 280290001Sglebius * Requires: 281290001Sglebius *\li 'len' > 0 282290001Sglebius *\li 'buf' points to an array of at least len chars 283290001Sglebius * 284290001Sglebius */ 285290001Sglebius 286290001Sglebiusisc_uint32_t 287290001Sglebiusisc_time_seconds(const isc_time_t *t); 288290001Sglebius 289290001SglebiusISC_LANG_ENDDECLS 290290001Sglebius 291290001Sglebius#endif /* ISC_TIME_H */ 292