#include "apr_arch_atime.h"
#include "apr_time.h"
#include "apr_general.h"
#include "apr_lib.h"
#include "apr_portable.h"
#if APR_HAVE_TIME_H
#include <time.h>
#endif
#if APR_HAVE_ERRNO_H
#include <errno.h>
#endif
#include <string.h>
#include <winbase.h>
#include "apr_arch_misc.h"
#define IsLeapYear(y) ((!(y % 4)) ? (((!(y % 400)) && (y % 100)) ? 1 : 0) : 0)
static DWORD get_local_timezone(TIME_ZONE_INFORMATION **tzresult)
{
static TIME_ZONE_INFORMATION tz;
static DWORD result;
static int init = 0;
if (!init) {
result = GetTimeZoneInformation(&tz);
init = 1;
}
*tzresult = &tz;
return result;
}
static void SystemTimeToAprExpTime(apr_time_exp_t *xt, SYSTEMTIME *tm)
{
static const int dayoffset[12] =
{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
xt->tm_usec = tm->wMilliseconds * 1000;
xt->tm_sec = tm->wSecond;
xt->tm_min = tm->wMinute;
xt->tm_hour = tm->wHour;
xt->tm_mday = tm->wDay;
xt->tm_mon = tm->wMonth - 1;
xt->tm_year = tm->wYear - 1900;
xt->tm_wday = tm->wDayOfWeek;
xt->tm_yday = dayoffset[xt->tm_mon] + (tm->wDay - 1);
xt->tm_isdst = 0;
xt->tm_gmtoff = 0;
if (IsLeapYear(tm->wYear) && (xt->tm_yday > 58))
xt->tm_yday++;
}
APR_DECLARE(apr_status_t) apr_time_ansi_put(apr_time_t *result,
time_t input)
{
*result = (apr_time_t) input * APR_USEC_PER_SEC;
return APR_SUCCESS;
}
APR_DECLARE(apr_time_t) apr_time_now(void)
{
LONGLONG aprtime = 0;
FILETIME time;
#ifndef _WIN32_WCE
GetSystemTimeAsFileTime(&time);
#else
SYSTEMTIME st;
GetSystemTime(&st);
SystemTimeToFileTime(&st, &time);
#endif
FileTimeToAprTime(&aprtime, &time);
return aprtime;
}
APR_DECLARE(apr_status_t) apr_time_exp_gmt(apr_time_exp_t *result,
apr_time_t input)
{
FILETIME ft;
SYSTEMTIME st;
AprTimeToFileTime(&ft, input);
FileTimeToSystemTime(&ft, &st);
SystemTimeToAprExpTime(result, &st);
result->tm_usec = (apr_int32_t) (input % APR_USEC_PER_SEC);
return APR_SUCCESS;
}
APR_DECLARE(apr_status_t) apr_time_exp_tz(apr_time_exp_t *result,
apr_time_t input,
apr_int32_t offs)
{
FILETIME ft;
SYSTEMTIME st;
AprTimeToFileTime(&ft, input + (offs * APR_USEC_PER_SEC));
FileTimeToSystemTime(&ft, &st);
SystemTimeToAprExpTime(result, &st);
result->tm_usec = (apr_int32_t) (input % APR_USEC_PER_SEC);
result->tm_gmtoff = offs;
return APR_SUCCESS;
}
APR_DECLARE(apr_status_t) apr_time_exp_lt(apr_time_exp_t *result,
apr_time_t input)
{
SYSTEMTIME st;
FILETIME ft, localft;
AprTimeToFileTime(&ft, input);
#if APR_HAS_UNICODE_FS && !defined(_WIN32_WCE)
IF_WIN_OS_IS_UNICODE
{
TIME_ZONE_INFORMATION *tz;
SYSTEMTIME localst;
apr_time_t localtime;
get_local_timezone(&tz);
FileTimeToSystemTime(&ft, &st);
SystemTimeToTzSpecificLocalTime(tz, &st, &localst);
SystemTimeToAprExpTime(result, &localst);
result->tm_usec = (apr_int32_t) (input % APR_USEC_PER_SEC);
SystemTimeToFileTime(&localst, &localft);
FileTimeToAprTime(&localtime, &localft);
result->tm_gmtoff = (int)apr_time_sec(localtime)
- (int)apr_time_sec(input);
result->tm_isdst = (result->tm_gmtoff / 3600)
- (-(tz->Bias + tz->StandardBias) / 60);
}
#endif
#if APR_HAS_ANSI_FS || defined(_WIN32_WCE)
ELSE_WIN_OS_IS_ANSI
{
TIME_ZONE_INFORMATION tz;
FileTimeToLocalFileTime(&ft, &localft);
FileTimeToSystemTime(&localft, &st);
SystemTimeToAprExpTime(result, &st);
result->tm_usec = (apr_int32_t) (input % APR_USEC_PER_SEC);
switch (GetTimeZoneInformation(&tz)) {
case TIME_ZONE_ID_UNKNOWN:
result->tm_isdst = 0;
result->tm_gmtoff = tz.Bias * -60;
break;
case TIME_ZONE_ID_STANDARD:
result->tm_isdst = 0;
result->tm_gmtoff = (tz.Bias + tz.StandardBias) * -60;
break;
case TIME_ZONE_ID_DAYLIGHT:
result->tm_isdst = 1;
result->tm_gmtoff = (tz.Bias + tz.DaylightBias) * -60;
break;
default:
;
}
}
#endif
return APR_SUCCESS;
}
APR_DECLARE(apr_status_t) apr_time_exp_get(apr_time_t *t,
apr_time_exp_t *xt)
{
apr_time_t year = xt->tm_year;
apr_time_t days;
static const int dayoffset[12] =
{306, 337, 0, 31, 61, 92, 122, 153, 184, 214, 245, 275};
if (xt->tm_mon < 2)
year--;
days = year * 365 + year / 4 - year / 100 + (year / 100 + 3) / 4;
days += dayoffset[xt->tm_mon] + xt->tm_mday - 1;
days -= 25508;
days = ((days * 24 + xt->tm_hour) * 60 + xt->tm_min) * 60 + xt->tm_sec;
if (days < 0) {
return APR_EBADDATE;
}
*t = days * APR_USEC_PER_SEC + xt->tm_usec;
return APR_SUCCESS;
}
APR_DECLARE(apr_status_t) apr_time_exp_gmt_get(apr_time_t *t,
apr_time_exp_t *xt)
{
apr_status_t status = apr_time_exp_get(t, xt);
if (status == APR_SUCCESS)
*t -= (apr_time_t) xt->tm_gmtoff * APR_USEC_PER_SEC;
return status;
}
APR_DECLARE(apr_status_t) apr_os_imp_time_get(apr_os_imp_time_t **ostime,
apr_time_t *aprtime)
{
AprTimeToFileTime(*ostime, *aprtime);
return APR_SUCCESS;
}
APR_DECLARE(apr_status_t) apr_os_exp_time_get(apr_os_exp_time_t **ostime,
apr_time_exp_t *aprexptime)
{
(*ostime)->wYear = aprexptime->tm_year + 1900;
(*ostime)->wMonth = aprexptime->tm_mon + 1;
(*ostime)->wDayOfWeek = aprexptime->tm_wday;
(*ostime)->wDay = aprexptime->tm_mday;
(*ostime)->wHour = aprexptime->tm_hour;
(*ostime)->wMinute = aprexptime->tm_min;
(*ostime)->wSecond = aprexptime->tm_sec;
(*ostime)->wMilliseconds = aprexptime->tm_usec / 1000;
return APR_SUCCESS;
}
APR_DECLARE(apr_status_t) apr_os_imp_time_put(apr_time_t *aprtime,
apr_os_imp_time_t **ostime,
apr_pool_t *cont)
{
FileTimeToAprTime(aprtime, *ostime);
return APR_SUCCESS;
}
APR_DECLARE(apr_status_t) apr_os_exp_time_put(apr_time_exp_t *aprtime,
apr_os_exp_time_t **ostime,
apr_pool_t *cont)
{
SystemTimeToAprExpTime(aprtime, *ostime);
return APR_SUCCESS;
}
APR_DECLARE(void) apr_sleep(apr_interval_time_t t)
{
Sleep((DWORD)(t / 1000));
}
#if defined(_WIN32_WCE)
APR_DECLARE(void) apr_time_clock_hires(apr_pool_t *p)
{
return;
}
#else
static apr_status_t clock_restore(void *unsetres)
{
ULONG newRes;
SetTimerResolution((ULONG)(apr_ssize_t)unsetres, FALSE, &newRes);
return APR_SUCCESS;
}
APR_DECLARE(void) apr_time_clock_hires(apr_pool_t *p)
{
ULONG newRes;
if (SetTimerResolution(10000, TRUE, &newRes) == 0 ) {
apr_pool_cleanup_register(p, (void*)10000, clock_restore,
apr_pool_cleanup_null);
}
}
#endif