1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
#include "win32/apr_arch_atime.h"
19
#include "apr_general.h"
21
#include "apr_portable.h"
30
#include "apr_arch_misc.h"
32
/* Leap year is any year divisible by four, but not by 100 unless also
35
#define IsLeapYear(y) ((!(y % 4)) ? (((!(y % 400)) && (y % 100)) ? 1 : 0) : 0)
37
static DWORD get_local_timezone(TIME_ZONE_INFORMATION **tzresult)
39
static TIME_ZONE_INFORMATION tz;
44
result = GetTimeZoneInformation(&tz);
52
static void SystemTimeToAprExpTime(apr_time_exp_t *xt, SYSTEMTIME *tm)
54
static const int dayoffset[12] =
55
{0, 31, 59, 90, 120, 151, 182, 212, 243, 273, 304, 334};
57
/* Note; the caller is responsible for filling in detailed tm_usec,
58
* tm_gmtoff and tm_isdst data when applicable.
60
xt->tm_usec = tm->wMilliseconds * 1000;
61
xt->tm_sec = tm->wSecond;
62
xt->tm_min = tm->wMinute;
63
xt->tm_hour = tm->wHour;
64
xt->tm_mday = tm->wDay;
65
xt->tm_mon = tm->wMonth - 1;
66
xt->tm_year = tm->wYear - 1900;
67
xt->tm_wday = tm->wDayOfWeek;
68
xt->tm_yday = dayoffset[xt->tm_mon] + (tm->wDay - 1);
72
/* If this is a leap year, and we're past the 28th of Feb. (the
73
* 58th day after Jan. 1), we'll increment our tm_yday by one.
75
if (IsLeapYear(tm->wYear) && (xt->tm_yday > 58))
79
APR_DECLARE(apr_status_t) apr_time_ansi_put(apr_time_t *result,
82
*result = (apr_time_t) input * APR_USEC_PER_SEC;
86
/* Return micro-seconds since the Unix epoch (jan. 1, 1970) UTC */
87
APR_DECLARE(apr_time_t) apr_time_now(void)
92
GetSystemTimeAsFileTime(&time);
96
SystemTimeToFileTime(&st, &time);
98
FileTimeToAprTime(&aprtime, &time);
102
APR_DECLARE(apr_status_t) apr_time_exp_gmt(apr_time_exp_t *result,
107
AprTimeToFileTime(&ft, input);
108
FileTimeToSystemTime(&ft, &st);
109
/* The Platform SDK documents that SYSTEMTIME/FILETIME are
110
* generally UTC, so no timezone info needed
112
SystemTimeToAprExpTime(result, &st);
113
result->tm_usec = (apr_int32_t) (input % APR_USEC_PER_SEC);
117
APR_DECLARE(apr_status_t) apr_time_exp_tz(apr_time_exp_t *result,
123
AprTimeToFileTime(&ft, input + (offs * APR_USEC_PER_SEC));
124
FileTimeToSystemTime(&ft, &st);
125
/* The Platform SDK documents that SYSTEMTIME/FILETIME are
126
* generally UTC, so we will simply note the offs used.
128
SystemTimeToAprExpTime(result, &st);
129
result->tm_usec = (apr_int32_t) (input % APR_USEC_PER_SEC);
130
result->tm_gmtoff = offs;
134
APR_DECLARE(apr_status_t) apr_time_exp_lt(apr_time_exp_t *result,
138
FILETIME ft, localft;
140
AprTimeToFileTime(&ft, input);
142
#if APR_HAS_UNICODE_FS
145
TIME_ZONE_INFORMATION *tz;
147
apr_time_t localtime;
149
get_local_timezone(&tz);
151
FileTimeToSystemTime(&ft, &st);
153
/* The Platform SDK documents that SYSTEMTIME/FILETIME are
154
* generally UTC. We use SystemTimeToTzSpecificLocalTime
155
* because FileTimeToLocalFileFime is documented that the
156
* resulting time local file time would have DST relative
157
* to the *present* date, not the date converted.
159
SystemTimeToTzSpecificLocalTime(tz, &st, &localst);
160
SystemTimeToAprExpTime(result, &localst);
161
result->tm_usec = (apr_int32_t) (input % APR_USEC_PER_SEC);
164
/* Recover the resulting time as an apr time and use the
165
* delta for gmtoff in seconds (and ignore msec rounding)
167
SystemTimeToFileTime(&localst, &localft);
168
FileTimeToAprTime(&localtime, &localft);
169
result->tm_gmtoff = (int)apr_time_sec(localtime)
170
- (int)apr_time_sec(input);
172
/* To compute the dst flag, we compare the expected
173
* local (standard) timezone bias to the delta.
174
* [Note, in war time or double daylight time the
175
* resulting tm_isdst is, desireably, 2 hours]
177
result->tm_isdst = (result->tm_gmtoff / 3600)
178
- (-(tz->Bias + tz->StandardBias) / 60);
184
TIME_ZONE_INFORMATION tz;
185
/* XXX: This code is simply *wrong*. The time converted will always
186
* map to the *now current* status of daylight savings time.
189
FileTimeToLocalFileTime(&ft, &localft);
190
FileTimeToSystemTime(&localft, &st);
191
SystemTimeToAprExpTime(result, &st);
192
result->tm_usec = (apr_int32_t) (input % APR_USEC_PER_SEC);
194
switch (GetTimeZoneInformation(&tz)) {
195
case TIME_ZONE_ID_UNKNOWN:
196
result->tm_isdst = 0;
197
/* Bias = UTC - local time in minutes
198
* tm_gmtoff is seconds east of UTC
200
result->tm_gmtoff = tz.Bias * -60;
202
case TIME_ZONE_ID_STANDARD:
203
result->tm_isdst = 0;
204
result->tm_gmtoff = (tz.Bias + tz.StandardBias) * -60;
206
case TIME_ZONE_ID_DAYLIGHT:
207
result->tm_isdst = 1;
208
result->tm_gmtoff = (tz.Bias + tz.DaylightBias) * -60;
219
APR_DECLARE(apr_status_t) apr_time_exp_get(apr_time_t *t,
222
apr_time_t year = xt->tm_year;
224
static const int dayoffset[12] =
225
{306, 337, 0, 31, 61, 92, 122, 153, 184, 214, 245, 275};
227
/* shift new year to 1st March in order to make leap year calc easy */
232
/* Find number of days since 1st March 1900 (in the Gregorian calendar). */
234
days = year * 365 + year / 4 - year / 100 + (year / 100 + 3) / 4;
235
days += dayoffset[xt->tm_mon] + xt->tm_mday - 1;
236
days -= 25508; /* 1 jan 1970 is 25508 days since 1 mar 1900 */
238
days = ((days * 24 + xt->tm_hour) * 60 + xt->tm_min) * 60 + xt->tm_sec;
243
*t = days * APR_USEC_PER_SEC + xt->tm_usec;
247
APR_DECLARE(apr_status_t) apr_time_exp_gmt_get(apr_time_t *t,
250
apr_status_t status = apr_time_exp_get(t, xt);
251
if (status == APR_SUCCESS)
252
*t -= (apr_time_t) xt->tm_gmtoff * APR_USEC_PER_SEC;
256
APR_DECLARE(apr_status_t) apr_os_imp_time_get(apr_os_imp_time_t **ostime,
259
/* TODO: Consider not passing in pointer to apr_time_t (e.g., call by value) */
260
AprTimeToFileTime(*ostime, *aprtime);
264
APR_DECLARE(apr_status_t) apr_os_exp_time_get(apr_os_exp_time_t **ostime,
265
apr_time_exp_t *aprexptime)
267
(*ostime)->wYear = aprexptime->tm_year + 1900;
268
(*ostime)->wMonth = aprexptime->tm_mon + 1;
269
(*ostime)->wDayOfWeek = aprexptime->tm_wday;
270
(*ostime)->wDay = aprexptime->tm_mday;
271
(*ostime)->wHour = aprexptime->tm_hour;
272
(*ostime)->wMinute = aprexptime->tm_min;
273
(*ostime)->wSecond = aprexptime->tm_sec;
274
(*ostime)->wMilliseconds = aprexptime->tm_usec / 1000;
278
APR_DECLARE(apr_status_t) apr_os_imp_time_put(apr_time_t *aprtime,
279
apr_os_imp_time_t **ostime,
282
/* XXX: sanity failure, what is file time, gmt or local ?
284
FileTimeToAprTime(aprtime, *ostime);
288
APR_DECLARE(apr_status_t) apr_os_exp_time_put(apr_time_exp_t *aprtime,
289
apr_os_exp_time_t **ostime,
292
/* The Platform SDK documents that SYSTEMTIME/FILETIME are
293
* generally UTC, so no timezone info needed
295
SystemTimeToAprExpTime(aprtime, *ostime);
299
APR_DECLARE(void) apr_sleep(apr_interval_time_t t)
301
/* One of the few sane situations for a cast, Sleep
302
* is in ms, not us, and passed as a DWORD value
304
Sleep((DWORD)(t / 1000));
308
static apr_status_t clock_restore(void *unsetres)
311
SetTimerResolution((ULONG)unsetres, FALSE, &newRes);
315
APR_DECLARE(void) apr_time_clock_hires(apr_pool_t *p)
318
/* Timer resolution is stated in 100ns units. Note that TRUE requests the
319
* new clock resolution, FALSE above releases the request.
321
if (SetTimerResolution(10000, TRUE, &newRes) == 0 /* STATUS_SUCCESS */) {
322
/* register the cleanup... */
323
apr_pool_cleanup_register(p, (void*)10000, clock_restore,
324
apr_pool_cleanup_null);