time.c revision dbb012765c735ee0d82dedb116cdc7cf18957814
1ccbfca64ae86ace521053773001cb995352f96fBob Halley/*
633c5dc507fa3133a6d49a55cfe84bf4fd522c72Tinderbox User * Copyright (C) 2004, 2006-2009, 2012-2014 Internet Systems Consortium, Inc. ("ISC")
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * Copyright (C) 1998-2001, 2003 Internet Software Consortium.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
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.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
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.
1ccbfca64ae86ace521053773001cb995352f96fBob Halley */
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
7829fad4093f2c1985b1efb7cea00287ff015d2bckb/* $Id: time.c,v 1.52 2009/08/14 07:51:08 marka Exp $ */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <config.h>
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley#include <errno.h>
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley#include <limits.h>
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley#include <stddef.h>
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley#include <stdlib.h>
1ccbfca64ae86ace521053773001cb995352f96fBob Halley#include <string.h>
1ccbfca64ae86ace521053773001cb995352f96fBob Halley#include <time.h>
341323e0e7ef69f7c425cd3462afe06d0930c528Michael Graff
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington#include <windows.h>
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington#include <isc/assertions.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/time.h>
364a82f7c25b62967678027043425201a5e5171aBob Halley#include <isc/tm.h>
1ccbfca64ae86ace521053773001cb995352f96fBob Halley#include <isc/util.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt/*
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.
341323e0e7ef69f7c425cd3462afe06d0930c528Michael Graff */
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley#define NS_PER_S 1000000000
cfb1587eb9a6dc6d1d36ea0344e1b20068b81e88Evan Hunt#define NS_INTERVAL 100
b20ee662a7c847c9ef7b96ab9e5e34543efe5c0dMark Andrews#define INTERVALS_PER_S (NS_PER_S / NS_INTERVAL)
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley#define UINT64_MAX _UI64_MAX
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley/***
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley *** Absolute Times
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley ***/
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtonstatic const isc_time_t epoch = { { 0, 0 } };
77ac297199fc44809d9628558223627c10ae3f31Brian WellingtonLIBISC_EXTERNAL_DATA const isc_time_t * const isc_time_epoch = &epoch;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington/***
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington *** Intervals
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington ***/
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleystatic const isc_interval_t zero_interval = { 0 };
62837b832f6a9999976d607eb0a9125bbbbb138bBob HalleyLIBISC_EXTERNAL_DATA const isc_interval_t * const isc_interval_zero = &zero_interval;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyvoid
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyisc_interval_set(isc_interval_t *i, unsigned int seconds,
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington unsigned int nanoseconds)
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley{
1ccbfca64ae86ace521053773001cb995352f96fBob Halley REQUIRE(i != NULL);
1ccbfca64ae86ace521053773001cb995352f96fBob Halley REQUIRE(nanoseconds < NS_PER_S);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
0e3ad060581e366d3e682caf2dd5d9ea2e0f4893Bob Halley /*
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * This rounds nanoseconds up not down.
1ccbfca64ae86ace521053773001cb995352f96fBob Halley */
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington i->interval = (LONGLONG)seconds * INTERVALS_PER_S
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington + (nanoseconds + NS_INTERVAL - 1) / NS_INTERVAL;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington}
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtonisc_boolean_t
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉isc_interval_iszero(const isc_interval_t *i) {
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington REQUIRE(i != NULL);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 if (i->interval == 0)
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington return (ISC_TRUE);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
b61c97cebd9c23e7bbfbdc9508cf022c68461d46Danny Mayer return (ISC_FALSE);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington}
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtonvoid
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 FILETIME temp;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington ULARGE_INTEGER i1;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 REQUIRE(t != NULL);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington REQUIRE(nanoseconds < NS_PER_S);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington SystemTimeToFileTime(&epoch, &temp);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington i1.LowPart = temp.dwLowDateTime;
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 i1.HighPart = temp.dwHighDateTime;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington i1.QuadPart += (unsigned __int64)nanoseconds/100;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington i1.QuadPart += (unsigned __int64)seconds*10000000;
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington t->absolute.dwLowDateTime = i1.LowPart;
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 t->absolute.dwHighDateTime = i1.HighPart;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington}
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtonvoid
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtonisc_time_settoepoch(isc_time_t *t) {
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington REQUIRE(t != NULL);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater t->absolute.dwLowDateTime = 0;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington t->absolute.dwHighDateTime = 0;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington}
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtonisc_boolean_t
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtonisc_time_isepoch(const isc_time_t *t) {
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington REQUIRE(t != NULL);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley if (t->absolute.dwLowDateTime == 0 &&
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley t->absolute.dwHighDateTime == 0)
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley return (ISC_TRUE);
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley return (ISC_FALSE);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff}
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
a6733246eafeb43755ce6d7ec3627ac4209cbccbMark Andrewsisc_result_t
1ccbfca64ae86ace521053773001cb995352f96fBob Halleyisc_time_now(isc_time_t *t) {
1ccbfca64ae86ace521053773001cb995352f96fBob Halley REQUIRE(t != NULL);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington GetSystemTimeAsFileTime(&t->absolute);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley return (ISC_R_SUCCESS);
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley}
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyisc_result_t
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyisc_time_nowplusinterval(isc_time_t *t, const isc_interval_t *i) {
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley ULARGE_INTEGER i1;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley REQUIRE(t != NULL);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington REQUIRE(i != NULL);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington GetSystemTimeAsFileTime(&t->absolute);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington i1.LowPart = t->absolute.dwLowDateTime;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington i1.HighPart = t->absolute.dwHighDateTime;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington if (UINT64_MAX - i1.QuadPart < (unsigned __int64)i->interval)
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington return (ISC_R_RANGE);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington i1.QuadPart += i->interval;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
1ccbfca64ae86ace521053773001cb995352f96fBob Halley t->absolute.dwLowDateTime = i1.LowPart;
c03ce72a3b1bd639d5bc430ed37f791051125c56Andreas Gustafsson t->absolute.dwHighDateTime = i1.HighPart;
c03ce72a3b1bd639d5bc430ed37f791051125c56Andreas Gustafsson
c03ce72a3b1bd639d5bc430ed37f791051125c56Andreas Gustafsson return (ISC_R_SUCCESS);
c03ce72a3b1bd639d5bc430ed37f791051125c56Andreas Gustafsson}
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
1ccbfca64ae86ace521053773001cb995352f96fBob Halleyint
1ccbfca64ae86ace521053773001cb995352f96fBob Halleyisc_time_compare(const isc_time_t *t1, const isc_time_t *t2) {
1ccbfca64ae86ace521053773001cb995352f96fBob Halley REQUIRE(t1 != NULL && t2 != NULL);
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley return ((int)CompareFileTime(&t1->absolute, &t2->absolute));
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley}
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyisc_result_t
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyisc_time_add(const isc_time_t *t, const isc_interval_t *i, isc_time_t *result)
1ccbfca64ae86ace521053773001cb995352f96fBob Halley{
2d63e8f8d2a8ef70e404182caf5b0d95dbcf2027Andreas Gustafsson ULARGE_INTEGER i1;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley REQUIRE(t != NULL && i != NULL && result != NULL);
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley i1.LowPart = t->absolute.dwLowDateTime;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley i1.HighPart = t->absolute.dwHighDateTime;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley if (UINT64_MAX - i1.QuadPart < (unsigned __int64)i->interval)
1ccbfca64ae86ace521053773001cb995352f96fBob Halley return (ISC_R_RANGE);
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley i1.QuadPart += i->interval;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley result->absolute.dwLowDateTime = i1.LowPart;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley result->absolute.dwHighDateTime = i1.HighPart;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley return (ISC_R_SUCCESS);
1ccbfca64ae86ace521053773001cb995352f96fBob Halley}
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyisc_result_t
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyisc_time_subtract(const isc_time_t *t, const isc_interval_t *i,
1ccbfca64ae86ace521053773001cb995352f96fBob Halley isc_time_t *result) {
1ccbfca64ae86ace521053773001cb995352f96fBob Halley ULARGE_INTEGER i1;
d549c3734869b48e740c64e80890bcb6f3ce672cJames Brister
d549c3734869b48e740c64e80890bcb6f3ce672cJames Brister REQUIRE(t != NULL && i != NULL && result != NULL);
d549c3734869b48e740c64e80890bcb6f3ce672cJames Brister
d549c3734869b48e740c64e80890bcb6f3ce672cJames Brister i1.LowPart = t->absolute.dwLowDateTime;
d549c3734869b48e740c64e80890bcb6f3ce672cJames Brister i1.HighPart = t->absolute.dwHighDateTime;
d549c3734869b48e740c64e80890bcb6f3ce672cJames Brister
d549c3734869b48e740c64e80890bcb6f3ce672cJames Brister if (i1.QuadPart < (unsigned __int64) i->interval)
d549c3734869b48e740c64e80890bcb6f3ce672cJames Brister return (ISC_R_RANGE);
d549c3734869b48e740c64e80890bcb6f3ce672cJames Brister
1ccbfca64ae86ace521053773001cb995352f96fBob Halley i1.QuadPart -= i->interval;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley result->absolute.dwLowDateTime = i1.LowPart;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley result->absolute.dwHighDateTime = i1.HighPart;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley return (ISC_R_SUCCESS);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence}
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halleyisc_uint64_t
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyisc_time_microdiff(const isc_time_t *t1, const isc_time_t *t2) {
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley ULARGE_INTEGER i1, i2;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley LONGLONG i3;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley REQUIRE(t1 != NULL && t2 != NULL);
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley i1.LowPart = t1->absolute.dwLowDateTime;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley i1.HighPart = t1->absolute.dwHighDateTime;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley i2.LowPart = t2->absolute.dwLowDateTime;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley i2.HighPart = t2->absolute.dwHighDateTime;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley if (i1.QuadPart <= i2.QuadPart)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence return (0);
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley /*
a6733246eafeb43755ce6d7ec3627ac4209cbccbMark Andrews * Convert to microseconds.
a6733246eafeb43755ce6d7ec3627ac4209cbccbMark Andrews */
a6733246eafeb43755ce6d7ec3627ac4209cbccbMark Andrews i3 = (i1.QuadPart - i2.QuadPart) / 10;
a6733246eafeb43755ce6d7ec3627ac4209cbccbMark Andrews
a6733246eafeb43755ce6d7ec3627ac4209cbccbMark Andrews return (i3);
a6733246eafeb43755ce6d7ec3627ac4209cbccbMark Andrews}
a6733246eafeb43755ce6d7ec3627ac4209cbccbMark Andrews
a6733246eafeb43755ce6d7ec3627ac4209cbccbMark Andrewsisc_uint32_t
a6733246eafeb43755ce6d7ec3627ac4209cbccbMark Andrewsisc_time_seconds(const isc_time_t *t) {
a6733246eafeb43755ce6d7ec3627ac4209cbccbMark Andrews SYSTEMTIME epoch = { 1970, 1, 4, 1, 0, 0, 0, 0 };
a6733246eafeb43755ce6d7ec3627ac4209cbccbMark Andrews FILETIME temp;
a6733246eafeb43755ce6d7ec3627ac4209cbccbMark Andrews ULARGE_INTEGER i1, i2;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence LONGLONG i3;
a6733246eafeb43755ce6d7ec3627ac4209cbccbMark Andrews
a6733246eafeb43755ce6d7ec3627ac4209cbccbMark Andrews SystemTimeToFileTime(&epoch, &temp);
a6733246eafeb43755ce6d7ec3627ac4209cbccbMark Andrews
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley i1.LowPart = t->absolute.dwLowDateTime;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley i1.HighPart = t->absolute.dwHighDateTime;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley i2.LowPart = temp.dwLowDateTime;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley i2.HighPart = temp.dwHighDateTime;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews i3 = (i1.QuadPart - i2.QuadPart) / 10000000;
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews return ((isc_uint32_t)i3);
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews}
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrewsisc_result_t
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrewsisc_time_secondsastimet(const isc_time_t *t, time_t *secondsp) {
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews time_t seconds;
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews REQUIRE(t != NULL);
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews seconds = (time_t)isc_time_seconds(t);
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews INSIST(sizeof(unsigned int) == sizeof(isc_uint32_t));
a76b380643a22f23a67a9df284e86cd7ef7608c1Mark Andrews INSIST(sizeof(time_t) >= sizeof(isc_uint32_t));
b3e2e7c4d6b5c5ee90ba5bb775d635834dccec81Bob Halley
b3e2e7c4d6b5c5ee90ba5bb775d635834dccec81Bob Halley if (isc_time_seconds(t) > (~0U>>1) && seconds <= (time_t)(~0U>>1))
b3e2e7c4d6b5c5ee90ba5bb775d635834dccec81Bob Halley return (ISC_R_RANGE);
b3e2e7c4d6b5c5ee90ba5bb775d635834dccec81Bob Halley
b3e2e7c4d6b5c5ee90ba5bb775d635834dccec81Bob Halley *secondsp = seconds;
b3e2e7c4d6b5c5ee90ba5bb775d635834dccec81Bob Halley
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence return (ISC_R_SUCCESS);
b3e2e7c4d6b5c5ee90ba5bb775d635834dccec81Bob Halley}
b3e2e7c4d6b5c5ee90ba5bb775d635834dccec81Bob Halley
b3e2e7c4d6b5c5ee90ba5bb775d635834dccec81Bob Halley
b3e2e7c4d6b5c5ee90ba5bb775d635834dccec81Bob Halleyisc_uint32_t
b3e2e7c4d6b5c5ee90ba5bb775d635834dccec81Bob Halleyisc_time_nanoseconds(const isc_time_t *t) {
b3e2e7c4d6b5c5ee90ba5bb775d635834dccec81Bob Halley ULARGE_INTEGER i;
fe12eb4fc27f49c6b3e42b1d7a6b40310e41e6dfBrian Wellington
fe12eb4fc27f49c6b3e42b1d7a6b40310e41e6dfBrian Wellington i.LowPart = t->absolute.dwLowDateTime;
fe12eb4fc27f49c6b3e42b1d7a6b40310e41e6dfBrian Wellington i.HighPart = t->absolute.dwHighDateTime;
fe12eb4fc27f49c6b3e42b1d7a6b40310e41e6dfBrian Wellington return ((isc_uint32_t)(i.QuadPart % 10000000) * 100);
fe12eb4fc27f49c6b3e42b1d7a6b40310e41e6dfBrian Wellington}
fe12eb4fc27f49c6b3e42b1d7a6b40310e41e6dfBrian Wellington
fe12eb4fc27f49c6b3e42b1d7a6b40310e41e6dfBrian Wellingtonvoid
fe12eb4fc27f49c6b3e42b1d7a6b40310e41e6dfBrian Wellingtonisc_time_formattimestamp(const isc_time_t *t, char *buf, unsigned int len) {
fe12eb4fc27f49c6b3e42b1d7a6b40310e41e6dfBrian Wellington FILETIME localft;
fe12eb4fc27f49c6b3e42b1d7a6b40310e41e6dfBrian Wellington SYSTEMTIME st;
fe12eb4fc27f49c6b3e42b1d7a6b40310e41e6dfBrian Wellington char DateBuf[50];
fe12eb4fc27f49c6b3e42b1d7a6b40310e41e6dfBrian Wellington char TimeBuf[50];
a99979c686ae04efd55ba8f0aacf32493b4aa7faBob Halley
a99979c686ae04efd55ba8f0aacf32493b4aa7faBob Halley static const char badtime[] = "99-Bad-9999 99:99:99.999";
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley REQUIRE(len > 0);
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley if (FileTimeToLocalFileTime(&t->absolute, &localft) &&
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley FileTimeToSystemTime(&localft, &st)) {
a99979c686ae04efd55ba8f0aacf32493b4aa7faBob Halley GetDateFormat(LOCALE_USER_DEFAULT, 0, &st, "dd-MMM-yyyy",
a99979c686ae04efd55ba8f0aacf32493b4aa7faBob Halley DateBuf, 50);
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley GetTimeFormat(LOCALE_USER_DEFAULT, TIME_NOTIMEMARKER|
a99979c686ae04efd55ba8f0aacf32493b4aa7faBob Halley TIME_FORCE24HOURFORMAT, &st, NULL, TimeBuf, 50);
a99979c686ae04efd55ba8f0aacf32493b4aa7faBob Halley
48481c9b6e19501457bcbc2995555412f352b99fBob Halley snprintf(buf, len, "%s %s.%03u", DateBuf, TimeBuf,
48481c9b6e19501457bcbc2995555412f352b99fBob Halley st.wMilliseconds);
48481c9b6e19501457bcbc2995555412f352b99fBob Halley
48481c9b6e19501457bcbc2995555412f352b99fBob Halley } else
48481c9b6e19501457bcbc2995555412f352b99fBob Halley snprintf(buf, len, badtime);
48481c9b6e19501457bcbc2995555412f352b99fBob Halley}
48481c9b6e19501457bcbc2995555412f352b99fBob Halley
48481c9b6e19501457bcbc2995555412f352b99fBob Halleyvoid
48481c9b6e19501457bcbc2995555412f352b99fBob Halleyisc_time_formathttptimestamp(const isc_time_t *t, char *buf, unsigned int len) {
48481c9b6e19501457bcbc2995555412f352b99fBob Halley SYSTEMTIME st;
48481c9b6e19501457bcbc2995555412f352b99fBob Halley char DateBuf[50];
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 char TimeBuf[50];
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff
7829fad4093f2c1985b1efb7cea00287ff015d2bckb/* strftime() format: "%a, %d %b %Y %H:%M:%S GMT" */
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halley
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halley REQUIRE(len > 0);
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halley if (FileTimeToSystemTime(&t->absolute, &st)) {
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halley GetDateFormat(LOCALE_USER_DEFAULT, 0, &st,
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halley "ddd',' dd MMM yyyy", DateBuf, 50);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb GetTimeFormat(LOCALE_USER_DEFAULT,
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halley TIME_NOTIMEMARKER | TIME_FORCE24HOURFORMAT,
7829fad4093f2c1985b1efb7cea00287ff015d2bckb &st, "hh':'mm':'ss", TimeBuf, 50);
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halley
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halley snprintf(buf, len, "%s %s GMT", DateBuf, TimeBuf);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff } else {
7829fad4093f2c1985b1efb7cea00287ff015d2bckb buf[0] = 0;
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halley }
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halley}
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halley
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halleyisc_result_t
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halleyisc_time_parsehttptimestamp(char *buf, isc_time_t *t) {
7829fad4093f2c1985b1efb7cea00287ff015d2bckb struct tm t_tm;
7829fad4093f2c1985b1efb7cea00287ff015d2bckb time_t when;
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halley char *p;
7829fad4093f2c1985b1efb7cea00287ff015d2bckb
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halley REQUIRE(buf != NULL);
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halley REQUIRE(t != NULL);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff p = isc_tm_strptime(buf, "%a, %d %b %Y %H:%M:%S", &t_tm);
bad8294771671374e811afac79a20cc6927e3e2fBob Halley if (p == NULL)
553ead32ff5b00284e574dcabc39115d4d74ec66Evan Hunt return (ISC_R_UNEXPECTED);
e174044290953a2499f574e35cc9c22ba126a303Mark Andrews when = isc_tm_timegm(&t_tm);
e174044290953a2499f574e35cc9c22ba126a303Mark Andrews if (when == -1)
e174044290953a2499f574e35cc9c22ba126a303Mark Andrews return (ISC_R_UNEXPECTED);
e174044290953a2499f574e35cc9c22ba126a303Mark Andrews isc_time_set(t, (unsigned int)when, 0);
553ead32ff5b00284e574dcabc39115d4d74ec66Evan Hunt return (ISC_R_SUCCESS);
553ead32ff5b00284e574dcabc39115d4d74ec66Evan Hunt}
553ead32ff5b00284e574dcabc39115d4d74ec66Evan Hunt
553ead32ff5b00284e574dcabc39115d4d74ec66Evan Huntvoid
553ead32ff5b00284e574dcabc39115d4d74ec66Evan Huntisc_time_formatISO8601(const isc_time_t *t, char *buf, unsigned int len) {
26d8ffe715e74d1e67d268551449b780fec1b95fAutomatic Updater SYSTEMTIME st;
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff char DateBuf[50];
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halley char TimeBuf[50];
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley/* strtime() format: "%Y-%m-%dT%H:%M:%SZ" */
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley REQUIRE(len > 0);
b03b67a6f1ea2966367e7beb2ef276ed6a1d3f92Bob Halley if (FileTimeToSystemTime(&t->absolute, &st)) {
b03b67a6f1ea2966367e7beb2ef276ed6a1d3f92Bob Halley GetDateFormat(LOCALE_NEUTRAL, 0, &st, "yyyy-MM-dd",
b03b67a6f1ea2966367e7beb2ef276ed6a1d3f92Bob Halley DateBuf, 50);
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halley GetTimeFormat(LOCALE_NEUTRAL,
0deebcd15ad440c7ecaaa77f8e06232b331aae79Mark Andrews TIME_NOTIMEMARKER | TIME_FORCE24HOURFORMAT,
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halley &st, "hh':'mm':'ss", TimeBuf, 50);
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halley snprintf(buf, len, "%sT%sZ", DateBuf, TimeBuf);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb } else {
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff buf[0] = 0;
7d44d8aacda98eb2b526af34757a6bbcc97cd388Bob Halley }
e174044290953a2499f574e35cc9c22ba126a303Mark Andrews}
e174044290953a2499f574e35cc9c22ba126a303Mark Andrews