09b9db3c91786130fc095df5055098964e7cdb83Tinderbox User * Copyright (C) 1998-2001, 2003, 2004, 2006-2009, 2012-2018 Internet Systems Consortium, Inc. ("ISC")
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
ece6c39dd823d92cf89e7e37614bd458d5d42658Mark Andrews/* $Id: time.c,v 1.52 2009/08/14 07:51:08 marka Exp $ */
6fa1cb5754695d550a58c6e8978fda65f5146af7David Lawrence * struct FILETIME uses "100-nanoseconds intervals".
11d732daac76a24a0f3e5948a2758a4b866a0825David Lawrence * NS / S = 1000000000 (10^9).
11d732daac76a24a0f3e5948a2758a4b866a0825David Lawrence * While it is reasonably obvious that this makes the needed
11d732daac76a24a0f3e5948a2758a4b866a0825David Lawrence * conversion factor 10^7, it is coded this way for additional clarity.
11d732daac76a24a0f3e5948a2758a4b866a0825David Lawrence#define INTERVALS_PER_S (NS_PER_S / NS_INTERVAL)
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer *** Absolute Times
27809a2ee5db141b684e53bf1d94da26e9f92d3aMark AndrewsLIBISC_EXTERNAL_DATA const isc_time_t * const isc_time_epoch = &epoch;
4d6964d70a114b53a11a3bd778d9b8f5179a7934Bob Halley *** Intervals
27809a2ee5db141b684e53bf1d94da26e9f92d3aMark Andrewsstatic const isc_interval_t zero_interval = { 0 };
27809a2ee5db141b684e53bf1d94da26e9f92d3aMark AndrewsLIBISC_EXTERNAL_DATA const isc_interval_t * const isc_interval_zero = &zero_interval;
e76d4c91bfadf823f04dcca1c1c5bcc14c67671dAndreas Gustafssonisc_interval_set(isc_interval_t *i, unsigned int seconds,
e72c1e7e465822fc9b5067b2dd3cf047f6132214Mark Andrews * This rounds nanoseconds up not down.
11d732daac76a24a0f3e5948a2758a4b866a0825David Lawrence i->interval = (LONGLONG)seconds * INTERVALS_PER_S
e72c1e7e465822fc9b5067b2dd3cf047f6132214Mark Andrews + (nanoseconds + NS_INTERVAL - 1) / NS_INTERVAL;
6c6ceac1bc66812f6e0097dcf5fd8b901c3fecd1Andreas Gustafssonisc_interval_iszero(const isc_interval_t *i) {
8343d55b3d97923fa444ea9b92aae2ec60ffd40fMark Andrewsisc_time_set(isc_time_t *t, unsigned int seconds, unsigned int nanoseconds) {
bcb68be0a8f3c3eca58d6a6a869267e5c1841de2Francis Dupont SYSTEMTIME epoch1970 = { 1970, 1, 4, 1, 0, 0, 0, 0 };
8343d55b3d97923fa444ea9b92aae2ec60ffd40fMark Andrews i1.QuadPart += (unsigned __int64)nanoseconds/100;
ed9ca2306508e3106e3d111d7cf39bf82f8689d0Mark Andrews i1.QuadPart += (unsigned __int64)seconds*10000000;
6c6ceac1bc66812f6e0097dcf5fd8b901c3fecd1Andreas Gustafssonisc_time_nowplusinterval(isc_time_t *t, const isc_interval_t *i) {
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer if (UINT64_MAX - i1.QuadPart < (unsigned __int64)i->interval)
6c6ceac1bc66812f6e0097dcf5fd8b901c3fecd1Andreas Gustafssonisc_time_compare(const isc_time_t *t1, const isc_time_t *t2) {
b592e197fe354d7258dc4166fce0a9425a338b0bBob Halley return ((int)CompareFileTime(&t1->absolute, &t2->absolute));
6c6ceac1bc66812f6e0097dcf5fd8b901c3fecd1Andreas Gustafssonisc_time_add(const isc_time_t *t, const isc_interval_t *i, isc_time_t *result)
4d6964d70a114b53a11a3bd778d9b8f5179a7934Bob Halley REQUIRE(t != NULL && i != NULL && result != NULL);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer if (UINT64_MAX - i1.QuadPart < (unsigned __int64)i->interval)
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence result->absolute.dwHighDateTime = i1.HighPart;
6c6ceac1bc66812f6e0097dcf5fd8b901c3fecd1Andreas Gustafssonisc_time_subtract(const isc_time_t *t, const isc_interval_t *i,
4d6964d70a114b53a11a3bd778d9b8f5179a7934Bob Halley REQUIRE(t != NULL && i != NULL && result != NULL);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer if (i1.QuadPart < (unsigned __int64) i->interval)
585529aaeb95a71cd3d95df2602a4688fc7c3292David Lawrence result->absolute.dwHighDateTime = i1.HighPart;
6c6ceac1bc66812f6e0097dcf5fd8b901c3fecd1Andreas Gustafssonisc_time_microdiff(const isc_time_t *t1, const isc_time_t *t2) {
d3e7d196cd14fc3095ce97846a66cd49fc6fee6dDavid Lawrence * Convert to microseconds.
bcb68be0a8f3c3eca58d6a6a869267e5c1841de2Francis Dupont SYSTEMTIME epoch1970 = { 1970, 1, 4, 1, 0, 0, 0, 0 };
1bb2f53b9f74a8ca9812cbe9243ef41190b4da14Evan Huntisc_time_secondsastimet(const isc_time_t *t, time_t *secondsp) {
1bb2f53b9f74a8ca9812cbe9243ef41190b4da14Evan Hunt INSIST(sizeof(unsigned int) == sizeof(isc_uint32_t));
f459b70c8ea8fb7aed93ff3b3104b837e009c6fdEvan Hunt if (isc_time_seconds(t) > (~0U>>1) && seconds <= (time_t)(~0U>>1))
6c6ceac1bc66812f6e0097dcf5fd8b901c3fecd1Andreas Gustafssonisc_time_nanoseconds(const isc_time_t *t) {
1d9958c6cc291916010779792f0fbdf6cd5ba368Mark Andrews return ((isc_uint32_t)(i.QuadPart % 10000000) * 100);
ff8cd3afa7f900d7986ccbc3638235cb8ad6f1ecAndreas Gustafssonisc_time_formattimestamp(const isc_time_t *t, char *buf, unsigned int len) {
ff8cd3afa7f900d7986ccbc3638235cb8ad6f1ecAndreas Gustafsson if (FileTimeToLocalFileTime(&t->absolute, &localft) &&
fcb54ce0a4f7377486df5bec83b3aa4711bf4131Mark Andrews GetDateFormat(LOCALE_USER_DEFAULT, 0, &st, "dd-MMM-yyyy",
b38ab99bdc833da490c0ca0cbd05fb9d54dff997Andreas Gustafsson GetTimeFormat(LOCALE_USER_DEFAULT, TIME_NOTIMEMARKER|
b38ab99bdc833da490c0ca0cbd05fb9d54dff997Andreas Gustafsson TIME_FORCE24HOURFORMAT, &st, NULL, TimeBuf, 50);
b38ab99bdc833da490c0ca0cbd05fb9d54dff997Andreas Gustafsson snprintf(buf, len, "%s %s.%03u", DateBuf, TimeBuf,
186e7f37c9fc985a7a7264cc8170e48a25bed434Mark Andrewsisc_time_formathttptimestamp(const isc_time_t *t, char *buf, unsigned int len) {
aeff7de836558fa8002ab5db35292d2bb6450da8Evan Hunt/* strftime() format: "%a, %d %b %Y %H:%M:%S GMT" */
186e7f37c9fc985a7a7264cc8170e48a25bed434Mark Andrews snprintf(buf, len, "%s %s GMT", DateBuf, TimeBuf);
1bb2f53b9f74a8ca9812cbe9243ef41190b4da14Evan Huntisc_time_parsehttptimestamp(char *buf, isc_time_t *t) {
a147de10fe5e19e593d42152ffd6879eca69860dEvan Hunt p = isc_tm_strptime(buf, "%a, %d %b %Y %H:%M:%S", &t_tm);
aeff7de836558fa8002ab5db35292d2bb6450da8Evan Huntisc_time_formatISO8601(const isc_time_t *t, char *buf, unsigned int len) {
bb5d14d7243d71e03d262ff175b355b52873e195Evan Hunt /* strtime() format: "%Y-%m-%dT%H:%M:%SZ" */
aeff7de836558fa8002ab5db35292d2bb6450da8Evan Hunt GetDateFormat(LOCALE_NEUTRAL, 0, &st, "yyyy-MM-dd",
8a6f41d86ac80fd1397ffee65bed5131129a84e2Mark Andrews snprintf(buf, len, "%sT%sZ", DateBuf, TimeBuf);
bb5d14d7243d71e03d262ff175b355b52873e195Evan Huntisc_time_formatISO8601ms(const isc_time_t *t, char *buf, unsigned int len) {
bb5d14d7243d71e03d262ff175b355b52873e195Evan Hunt /* strtime() format: "%Y-%m-%dT%H:%M:%S.SSSZ" */
bb5d14d7243d71e03d262ff175b355b52873e195Evan Hunt GetDateFormat(LOCALE_NEUTRAL, 0, &st, "yyyy-MM-dd",