2
0

time.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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.
  15. */
  16. #include "win32/apr_arch_atime.h"
  17. #include "apr_time.h"
  18. #include "apr_general.h"
  19. #include "apr_lib.h"
  20. #include "apr_portable.h"
  21. #if APR_HAVE_TIME_H
  22. #include <time.h>
  23. #endif
  24. #if APR_HAVE_ERRNO_H
  25. #include <errno.h>
  26. #endif
  27. #include <string.h>
  28. #include <winbase.h>
  29. #include "apr_arch_misc.h"
  30. /* Leap year is any year divisible by four, but not by 100 unless also
  31. * divisible by 400
  32. */
  33. #define IsLeapYear(y) ((!(y % 4)) ? (((!(y % 400)) && (y % 100)) ? 1 : 0) : 0)
  34. static DWORD get_local_timezone(TIME_ZONE_INFORMATION **tzresult)
  35. {
  36. static TIME_ZONE_INFORMATION tz;
  37. static DWORD result;
  38. static int init = 0;
  39. if (!init) {
  40. result = GetTimeZoneInformation(&tz);
  41. init = 1;
  42. }
  43. *tzresult = &tz;
  44. return result;
  45. }
  46. static void SystemTimeToAprExpTime(apr_time_exp_t *xt, SYSTEMTIME *tm)
  47. {
  48. static const int dayoffset[12] =
  49. {0, 31, 59, 90, 120, 151, 182, 212, 243, 273, 304, 334};
  50. /* Note; the caller is responsible for filling in detailed tm_usec,
  51. * tm_gmtoff and tm_isdst data when applicable.
  52. */
  53. xt->tm_usec = tm->wMilliseconds * 1000;
  54. xt->tm_sec = tm->wSecond;
  55. xt->tm_min = tm->wMinute;
  56. xt->tm_hour = tm->wHour;
  57. xt->tm_mday = tm->wDay;
  58. xt->tm_mon = tm->wMonth - 1;
  59. xt->tm_year = tm->wYear - 1900;
  60. xt->tm_wday = tm->wDayOfWeek;
  61. xt->tm_yday = dayoffset[xt->tm_mon] + (tm->wDay - 1);
  62. xt->tm_isdst = 0;
  63. xt->tm_gmtoff = 0;
  64. /* If this is a leap year, and we're past the 28th of Feb. (the
  65. * 58th day after Jan. 1), we'll increment our tm_yday by one.
  66. */
  67. if (IsLeapYear(tm->wYear) && (xt->tm_yday > 58))
  68. xt->tm_yday++;
  69. }
  70. APR_DECLARE(apr_status_t) apr_time_ansi_put(apr_time_t *result,
  71. time_t input)
  72. {
  73. *result = (apr_time_t) input * APR_USEC_PER_SEC;
  74. return APR_SUCCESS;
  75. }
  76. /* Return micro-seconds since the Unix epoch (jan. 1, 1970) UTC */
  77. APR_DECLARE(apr_time_t) apr_time_now(void)
  78. {
  79. LONGLONG aprtime = 0;
  80. FILETIME time;
  81. #ifndef _WIN32_WCE
  82. GetSystemTimeAsFileTime(&time);
  83. #else
  84. SYSTEMTIME st;
  85. GetSystemTime(&st);
  86. SystemTimeToFileTime(&st, &time);
  87. #endif
  88. FileTimeToAprTime(&aprtime, &time);
  89. return aprtime;
  90. }
  91. APR_DECLARE(apr_status_t) apr_time_exp_gmt(apr_time_exp_t *result,
  92. apr_time_t input)
  93. {
  94. FILETIME ft;
  95. SYSTEMTIME st;
  96. AprTimeToFileTime(&ft, input);
  97. FileTimeToSystemTime(&ft, &st);
  98. /* The Platform SDK documents that SYSTEMTIME/FILETIME are
  99. * generally UTC, so no timezone info needed
  100. */
  101. SystemTimeToAprExpTime(result, &st);
  102. result->tm_usec = (apr_int32_t) (input % APR_USEC_PER_SEC);
  103. return APR_SUCCESS;
  104. }
  105. APR_DECLARE(apr_status_t) apr_time_exp_tz(apr_time_exp_t *result,
  106. apr_time_t input,
  107. apr_int32_t offs)
  108. {
  109. FILETIME ft;
  110. SYSTEMTIME st;
  111. AprTimeToFileTime(&ft, input + (offs * APR_USEC_PER_SEC));
  112. FileTimeToSystemTime(&ft, &st);
  113. /* The Platform SDK documents that SYSTEMTIME/FILETIME are
  114. * generally UTC, so we will simply note the offs used.
  115. */
  116. SystemTimeToAprExpTime(result, &st);
  117. result->tm_usec = (apr_int32_t) (input % APR_USEC_PER_SEC);
  118. result->tm_gmtoff = offs;
  119. return APR_SUCCESS;
  120. }
  121. APR_DECLARE(apr_status_t) apr_time_exp_lt(apr_time_exp_t *result,
  122. apr_time_t input)
  123. {
  124. SYSTEMTIME st;
  125. FILETIME ft, localft;
  126. AprTimeToFileTime(&ft, input);
  127. #if APR_HAS_UNICODE_FS
  128. IF_WIN_OS_IS_UNICODE
  129. {
  130. TIME_ZONE_INFORMATION *tz;
  131. SYSTEMTIME localst;
  132. apr_time_t localtime;
  133. get_local_timezone(&tz);
  134. FileTimeToSystemTime(&ft, &st);
  135. /* The Platform SDK documents that SYSTEMTIME/FILETIME are
  136. * generally UTC. We use SystemTimeToTzSpecificLocalTime
  137. * because FileTimeToLocalFileFime is documented that the
  138. * resulting time local file time would have DST relative
  139. * to the *present* date, not the date converted.
  140. */
  141. SystemTimeToTzSpecificLocalTime(tz, &st, &localst);
  142. SystemTimeToAprExpTime(result, &localst);
  143. result->tm_usec = (apr_int32_t) (input % APR_USEC_PER_SEC);
  144. /* Recover the resulting time as an apr time and use the
  145. * delta for gmtoff in seconds (and ignore msec rounding)
  146. */
  147. SystemTimeToFileTime(&localst, &localft);
  148. FileTimeToAprTime(&localtime, &localft);
  149. result->tm_gmtoff = (int)apr_time_sec(localtime)
  150. - (int)apr_time_sec(input);
  151. /* To compute the dst flag, we compare the expected
  152. * local (standard) timezone bias to the delta.
  153. * [Note, in war time or double daylight time the
  154. * resulting tm_isdst is, desireably, 2 hours]
  155. */
  156. result->tm_isdst = (result->tm_gmtoff / 3600)
  157. - (-(tz->Bias + tz->StandardBias) / 60);
  158. }
  159. #endif
  160. #if APR_HAS_ANSI_FS
  161. ELSE_WIN_OS_IS_ANSI
  162. {
  163. TIME_ZONE_INFORMATION tz;
  164. /* XXX: This code is simply *wrong*. The time converted will always
  165. * map to the *now current* status of daylight savings time.
  166. */
  167. FileTimeToLocalFileTime(&ft, &localft);
  168. FileTimeToSystemTime(&localft, &st);
  169. SystemTimeToAprExpTime(result, &st);
  170. result->tm_usec = (apr_int32_t) (input % APR_USEC_PER_SEC);
  171. switch (GetTimeZoneInformation(&tz)) {
  172. case TIME_ZONE_ID_UNKNOWN:
  173. result->tm_isdst = 0;
  174. /* Bias = UTC - local time in minutes
  175. * tm_gmtoff is seconds east of UTC
  176. */
  177. result->tm_gmtoff = tz.Bias * -60;
  178. break;
  179. case TIME_ZONE_ID_STANDARD:
  180. result->tm_isdst = 0;
  181. result->tm_gmtoff = (tz.Bias + tz.StandardBias) * -60;
  182. break;
  183. case TIME_ZONE_ID_DAYLIGHT:
  184. result->tm_isdst = 1;
  185. result->tm_gmtoff = (tz.Bias + tz.DaylightBias) * -60;
  186. break;
  187. default:
  188. /* noop */;
  189. }
  190. }
  191. #endif
  192. return APR_SUCCESS;
  193. }
  194. APR_DECLARE(apr_status_t) apr_time_exp_get(apr_time_t *t,
  195. apr_time_exp_t *xt)
  196. {
  197. apr_time_t year = xt->tm_year;
  198. apr_time_t days;
  199. static const int dayoffset[12] =
  200. {306, 337, 0, 31, 61, 92, 122, 153, 184, 214, 245, 275};
  201. /* shift new year to 1st March in order to make leap year calc easy */
  202. if (xt->tm_mon < 2)
  203. year--;
  204. /* Find number of days since 1st March 1900 (in the Gregorian calendar). */
  205. days = year * 365 + year / 4 - year / 100 + (year / 100 + 3) / 4;
  206. days += dayoffset[xt->tm_mon] + xt->tm_mday - 1;
  207. days -= 25508; /* 1 jan 1970 is 25508 days since 1 mar 1900 */
  208. days = ((days * 24 + xt->tm_hour) * 60 + xt->tm_min) * 60 + xt->tm_sec;
  209. if (days < 0) {
  210. return APR_EBADDATE;
  211. }
  212. *t = days * APR_USEC_PER_SEC + xt->tm_usec;
  213. return APR_SUCCESS;
  214. }
  215. APR_DECLARE(apr_status_t) apr_time_exp_gmt_get(apr_time_t *t,
  216. apr_time_exp_t *xt)
  217. {
  218. apr_status_t status = apr_time_exp_get(t, xt);
  219. if (status == APR_SUCCESS)
  220. *t -= (apr_time_t) xt->tm_gmtoff * APR_USEC_PER_SEC;
  221. return status;
  222. }
  223. APR_DECLARE(apr_status_t) apr_os_imp_time_get(apr_os_imp_time_t **ostime,
  224. apr_time_t *aprtime)
  225. {
  226. /* TODO: Consider not passing in pointer to apr_time_t (e.g., call by value) */
  227. AprTimeToFileTime(*ostime, *aprtime);
  228. return APR_SUCCESS;
  229. }
  230. APR_DECLARE(apr_status_t) apr_os_exp_time_get(apr_os_exp_time_t **ostime,
  231. apr_time_exp_t *aprexptime)
  232. {
  233. (*ostime)->wYear = aprexptime->tm_year + 1900;
  234. (*ostime)->wMonth = aprexptime->tm_mon + 1;
  235. (*ostime)->wDayOfWeek = aprexptime->tm_wday;
  236. (*ostime)->wDay = aprexptime->tm_mday;
  237. (*ostime)->wHour = aprexptime->tm_hour;
  238. (*ostime)->wMinute = aprexptime->tm_min;
  239. (*ostime)->wSecond = aprexptime->tm_sec;
  240. (*ostime)->wMilliseconds = aprexptime->tm_usec / 1000;
  241. return APR_SUCCESS;
  242. }
  243. APR_DECLARE(apr_status_t) apr_os_imp_time_put(apr_time_t *aprtime,
  244. apr_os_imp_time_t **ostime,
  245. apr_pool_t *cont)
  246. {
  247. /* XXX: sanity failure, what is file time, gmt or local ?
  248. */
  249. FileTimeToAprTime(aprtime, *ostime);
  250. return APR_SUCCESS;
  251. }
  252. APR_DECLARE(apr_status_t) apr_os_exp_time_put(apr_time_exp_t *aprtime,
  253. apr_os_exp_time_t **ostime,
  254. apr_pool_t *cont)
  255. {
  256. /* The Platform SDK documents that SYSTEMTIME/FILETIME are
  257. * generally UTC, so no timezone info needed
  258. */
  259. SystemTimeToAprExpTime(aprtime, *ostime);
  260. return APR_SUCCESS;
  261. }
  262. APR_DECLARE(void) apr_sleep(apr_interval_time_t t)
  263. {
  264. /* One of the few sane situations for a cast, Sleep
  265. * is in ms, not us, and passed as a DWORD value
  266. */
  267. Sleep((DWORD)(t / 1000));
  268. }
  269. static apr_status_t clock_restore(void *unsetres)
  270. {
  271. ULONG newRes;
  272. SetTimerResolution((ULONG)unsetres, FALSE, &newRes);
  273. return APR_SUCCESS;
  274. }
  275. APR_DECLARE(void) apr_time_clock_hires(apr_pool_t *p)
  276. {
  277. ULONG newRes;
  278. /* Timer resolution is stated in 100ns units. Note that TRUE requests the
  279. * new clock resolution, FALSE above releases the request.
  280. */
  281. if (SetTimerResolution(10000, TRUE, &newRes) == 0 /* STATUS_SUCCESS */) {
  282. /* register the cleanup... */
  283. apr_pool_cleanup_register(p, (void*)10000, clock_restore,
  284. apr_pool_cleanup_null);
  285. }
  286. }