time.c revision dbb012765c735ee0d82dedb116cdc7cf18957814
633c5dc507fa3133a6d49a55cfe84bf4fd522c72Tinderbox User * Copyright (C) 2004, 2006-2009, 2012-2014 Internet Systems Consortium, Inc. ("ISC")
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * Copyright (C) 1998-2001, 2003 Internet Software Consortium.
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * Permission to use, copy, modify, and/or distribute this software for any
1ccbfca64ae86ace521053773001cb995352f96fBob Halley * purpose with or without fee is hereby granted, provided that the above
1ccbfca64ae86ace521053773001cb995352f96fBob Halley * 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.
7829fad4093f2c1985b1efb7cea00287ff015d2bckb/* $Id: time.c,v 1.52 2009/08/14 07:51:08 marka Exp $ */
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington * struct FILETIME uses "100-nanoseconds intervals".
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt * NS / S = 1000000000 (10^9).
c03ce72a3b1bd639d5bc430ed37f791051125c56Andreas Gustafsson * While it is reasonably obvious that this makes the needed
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halley * conversion factor 10^7, it is coded this way for additional clarity.
b20ee662a7c847c9ef7b96ab9e5e34543efe5c0dMark Andrews#define INTERVALS_PER_S (NS_PER_S / NS_INTERVAL)
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley *** Absolute Times
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtonstatic const isc_time_t epoch = { { 0, 0 } };
77ac297199fc44809d9628558223627c10ae3f31Brian WellingtonLIBISC_EXTERNAL_DATA const isc_time_t * const isc_time_epoch = &epoch;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington *** Intervals
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleystatic const isc_interval_t zero_interval = { 0 };
62837b832f6a9999976d607eb0a9125bbbbb138bBob HalleyLIBISC_EXTERNAL_DATA const isc_interval_t * const isc_interval_zero = &zero_interval;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyisc_interval_set(isc_interval_t *i, unsigned int seconds,
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * This rounds nanoseconds up not down.
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington i->interval = (LONGLONG)seconds * INTERVALS_PER_S
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington + (nanoseconds + NS_INTERVAL - 1) / NS_INTERVAL;
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉isc_interval_iszero(const isc_interval_t *i) {
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtonisc_time_set(isc_time_t *t, unsigned int seconds, unsigned int nanoseconds) {
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington SYSTEMTIME epoch = { 1970, 1, 4, 1, 0, 0, 0, 0 };
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington i1.QuadPart += (unsigned __int64)nanoseconds/100;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington i1.QuadPart += (unsigned __int64)seconds*10000000;
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 t->absolute.dwHighDateTime = i1.HighPart;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyisc_time_nowplusinterval(isc_time_t *t, const isc_interval_t *i) {
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington if (UINT64_MAX - i1.QuadPart < (unsigned __int64)i->interval)
c03ce72a3b1bd639d5bc430ed37f791051125c56Andreas Gustafsson t->absolute.dwHighDateTime = i1.HighPart;
1ccbfca64ae86ace521053773001cb995352f96fBob Halleyisc_time_compare(const isc_time_t *t1, const isc_time_t *t2) {
1ccbfca64ae86ace521053773001cb995352f96fBob Halley return ((int)CompareFileTime(&t1->absolute, &t2->absolute));
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyisc_time_add(const isc_time_t *t, const isc_interval_t *i, isc_time_t *result)
1ccbfca64ae86ace521053773001cb995352f96fBob Halley REQUIRE(t != NULL && i != NULL && result != NULL);
1ccbfca64ae86ace521053773001cb995352f96fBob Halley if (UINT64_MAX - i1.QuadPart < (unsigned __int64)i->interval)
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyisc_time_subtract(const isc_time_t *t, const isc_interval_t *i,
d549c3734869b48e740c64e80890bcb6f3ce672cJames Brister REQUIRE(t != NULL && i != NULL && result != NULL);
d549c3734869b48e740c64e80890bcb6f3ce672cJames Brister if (i1.QuadPart < (unsigned __int64) i->interval)
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyisc_time_microdiff(const isc_time_t *t1, const isc_time_t *t2) {
a6733246eafeb43755ce6d7ec3627ac4209cbccbMark Andrews * Convert to microseconds.
a6733246eafeb43755ce6d7ec3627ac4209cbccbMark Andrews SYSTEMTIME epoch = { 1970, 1, 4, 1, 0, 0, 0, 0 };
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrewsisc_time_secondsastimet(const isc_time_t *t, time_t *secondsp) {
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews INSIST(sizeof(unsigned int) == sizeof(isc_uint32_t));
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews INSIST(sizeof(time_t) >= sizeof(isc_uint32_t));
b3e2e7c4d6b5c5ee90ba5bb775d635834dccec81Bob Halley if (isc_time_seconds(t) > (~0U>>1) && seconds <= (time_t)(~0U>>1))
fe12eb4fc27f49c6b3e42b1d7a6b40310e41e6dfBrian Wellington return ((isc_uint32_t)(i.QuadPart % 10000000) * 100);
fe12eb4fc27f49c6b3e42b1d7a6b40310e41e6dfBrian Wellingtonisc_time_formattimestamp(const isc_time_t *t, char *buf, unsigned int len) {
a99979c686ae04efd55ba8f0aacf32493b4aa7faBob Halley static const char badtime[] = "99-Bad-9999 99:99:99.999";
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley if (FileTimeToLocalFileTime(&t->absolute, &localft) &&
a99979c686ae04efd55ba8f0aacf32493b4aa7faBob Halley GetDateFormat(LOCALE_USER_DEFAULT, 0, &st, "dd-MMM-yyyy",
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley GetTimeFormat(LOCALE_USER_DEFAULT, TIME_NOTIMEMARKER|
48481c9b6e19501457bcbc2995555412f352b99fBob Halley snprintf(buf, len, "%s %s.%03u", DateBuf, TimeBuf,
48481c9b6e19501457bcbc2995555412f352b99fBob Halleyisc_time_formathttptimestamp(const isc_time_t *t, char *buf, unsigned int len) {
7829fad4093f2c1985b1efb7cea00287ff015d2bckb/* strftime() format: "%a, %d %b %Y %H:%M:%S GMT" */
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halley snprintf(buf, len, "%s %s GMT", DateBuf, TimeBuf);
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halleyisc_time_parsehttptimestamp(char *buf, isc_time_t *t) {
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff p = isc_tm_strptime(buf, "%a, %d %b %Y %H:%M:%S", &t_tm);
553ead32ff5b00284e574dcabc39115d4d74ec66Evan Huntisc_time_formatISO8601(const isc_time_t *t, char *buf, unsigned int len) {
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley/* strtime() format: "%Y-%m-%dT%H:%M:%SZ" */
b03b67a6f1ea2966367e7beb2ef276ed6a1d3f92Bob Halley GetDateFormat(LOCALE_NEUTRAL, 0, &st, "yyyy-MM-dd",