time.c revision 6ebd91a0c7f4c62a501b67adce4a6800d8b7dfac
9cee5bb02863bf191e12cd4297adabf1971020deAutomatic Updater * Copyright (C) 2004, 2006-2008 Internet Systems Consortium, Inc. ("ISC")
fcb54ce0a4f7377486df5bec83b3aa4711bf4131Mark Andrews * Copyright (C) 1998-2001, 2003 Internet Software Consortium.
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * Permission to use, copy, modify, and/or distribute this software for any
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * purpose with or without fee is hereby granted, provided that the above
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * copyright notice and this permission notice appear in all copies.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * PERFORMANCE OF THIS SOFTWARE.
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt/* $Id: time.c,v 1.45 2008/08/29 23:47:22 tbox Exp $ */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * struct FILETIME uses "100-nanoseconds intervals".
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * NS / S = 1000000000 (10^9).
28b863e609ff2d97b78663b46894494cfa2ea411Mark Andrews * While it is reasonably obvious that this makes the needed
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * conversion factor 10^7, it is coded this way for additional clarity.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define INTERVALS_PER_S (NS_PER_S / NS_INTERVAL)
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence *** Absolute Times
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas GustafssonLIBISC_EXTERNAL_DATA isc_time_t *isc_time_epoch = &epoch;
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews *** Intervals
9ac7076ebad044afb15e9e2687e3696868778538Mark AndrewsLIBISC_EXTERNAL_DATA isc_interval_t *isc_interval_zero = &zero_interval;
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrewsisc_interval_set(isc_interval_t *i, unsigned int seconds,
deaaf94332abbfdb3aff53675546acfed16e5eb6Mark Andrews * This rounds nanoseconds up not down.
c46f10e4a1702191b003cf8f8fc5059c15d29c48Mark Andrews i->interval = (LONGLONG)seconds * INTERVALS_PER_S
0b056755b2f423ba5f6adac8f7851d78f7d11437David Lawrence + (nanoseconds + NS_INTERVAL - 1) / NS_INTERVAL;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrenceisc_interval_iszero(const isc_interval_t *i) {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinisc_time_nowplusinterval(isc_time_t *t, const isc_interval_t *i) {
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence if (UINT64_MAX - i1.QuadPart < (unsigned __int64)i->interval)
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupontisc_time_compare(const isc_time_t *t1, const isc_time_t *t2) {
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence return ((int)CompareFileTime(&t1->absolute, &t2->absolute));
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrenceisc_time_add(const isc_time_t *t, const isc_interval_t *i, isc_time_t *result)
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington REQUIRE(t != NULL && i != NULL && result != NULL);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence if (UINT64_MAX - i1.QuadPart < (unsigned __int64)i->interval)
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence result->absolute.dwHighDateTime = i1.HighPart;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrenceisc_time_subtract(const isc_time_t *t, const isc_interval_t *i,
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence REQUIRE(t != NULL && i != NULL && result != NULL);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence if (i1.QuadPart < (unsigned __int64) i->interval)
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence result->absolute.dwHighDateTime = i1.HighPart;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrenceisc_time_microdiff(const isc_time_t *t1, const isc_time_t *t2) {
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence * Convert to microseconds.
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * Convert the time to a SYSTEMTIME structure and the grab the
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * milliseconds
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews return ((isc_uint32_t)(st.wMilliseconds / 1000));
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews * Convert the time to a SYSTEMTIME structure and the grab the
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews * milliseconds
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews return ((isc_uint32_t)(st.wMilliseconds * 1000000));
aa05bbdef7f7827dde158dcc913f4dade84c8511Brian Wellingtonisc_time_formattimestamp(const isc_time_t *t, char *buf, unsigned int len) {
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews static const char badtime[] = "99-Bad-9999 99:99:99.999";
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews if (FileTimeToLocalFileTime(&t->absolute, &localft) &&
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington GetDateFormat(LOCALE_USER_DEFAULT, 0, &st, "dd-MMM-yyyy",
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews GetTimeFormat(LOCALE_USER_DEFAULT, TIME_NOTIMEMARKER|
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews TIME_FORCE24HOURFORMAT, &st, NULL, TimeBuf, 50);
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews snprintf(buf, len, "%s %s.%03u", DateBuf, TimeBuf,
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrewsisc_time_formathttptimestamp(const isc_time_t *t, char *buf, unsigned int len) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews GetDateFormat(LOCALE_USER_DEFAULT, 0, &st, "ddd',', dd-MMM-yyyy",
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews snprintf(buf, len, "%s %s GMT", DateBuf, TimeBuf);