time.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 "fspr_portable.h"
  17. #include "fspr_time.h"
  18. #include "fspr_lib.h"
  19. #include "fspr_private.h"
  20. #include "fspr_strings.h"
  21. /* private APR headers */
  22. #include "fspr_arch_internal_time.h"
  23. /* System Headers required for time library */
  24. #if APR_HAVE_SYS_TIME_H
  25. #include <sys/time.h>
  26. #endif
  27. #if APR_HAVE_UNISTD_H
  28. #include <unistd.h>
  29. #endif
  30. #ifdef HAVE_TIME_H
  31. #include <time.h>
  32. #endif
  33. /* End System Headers */
  34. #if !defined(HAVE_STRUCT_TM_TM_GMTOFF) && !defined(HAVE_STRUCT_TM___TM_GMTOFF)
  35. static fspr_int32_t server_gmt_offset;
  36. #define NO_GMTOFF_IN_STRUCT_TM
  37. #endif
  38. static fspr_int32_t get_offset(struct tm *tm)
  39. {
  40. #if defined(HAVE_STRUCT_TM_TM_GMTOFF)
  41. return tm->tm_gmtoff;
  42. #elif defined(HAVE_STRUCT_TM___TM_GMTOFF)
  43. return tm->__tm_gmtoff;
  44. #else
  45. #ifdef NETWARE
  46. /* Need to adjust the global variable each time otherwise
  47. the web server would have to be restarted when daylight
  48. savings changes.
  49. */
  50. if (daylightOnOff) {
  51. return server_gmt_offset + daylightOffset;
  52. }
  53. #else
  54. if (tm->tm_isdst)
  55. return server_gmt_offset + 3600;
  56. #endif
  57. return server_gmt_offset;
  58. #endif
  59. }
  60. APR_DECLARE(fspr_status_t) fspr_time_ansi_put(fspr_time_t *result,
  61. time_t input)
  62. {
  63. *result = (fspr_time_t)input * APR_USEC_PER_SEC;
  64. return APR_SUCCESS;
  65. }
  66. /* NB NB NB NB This returns GMT!!!!!!!!!! */
  67. APR_DECLARE(fspr_time_t) fspr_time_now(void)
  68. {
  69. struct timeval tv;
  70. gettimeofday(&tv, NULL);
  71. return tv.tv_sec * APR_USEC_PER_SEC + tv.tv_usec;
  72. }
  73. static void explode_time(fspr_time_exp_t *xt, fspr_time_t t,
  74. fspr_int32_t offset, int use_localtime)
  75. {
  76. struct tm tm;
  77. time_t tt = (t / APR_USEC_PER_SEC) + offset;
  78. xt->tm_usec = t % APR_USEC_PER_SEC;
  79. #if APR_HAS_THREADS && defined (_POSIX_THREAD_SAFE_FUNCTIONS)
  80. if (use_localtime)
  81. localtime_r(&tt, &tm);
  82. else
  83. gmtime_r(&tt, &tm);
  84. #else
  85. if (use_localtime)
  86. tm = *localtime(&tt);
  87. else
  88. tm = *gmtime(&tt);
  89. #endif
  90. xt->tm_sec = tm.tm_sec;
  91. xt->tm_min = tm.tm_min;
  92. xt->tm_hour = tm.tm_hour;
  93. xt->tm_mday = tm.tm_mday;
  94. xt->tm_mon = tm.tm_mon;
  95. xt->tm_year = tm.tm_year;
  96. xt->tm_wday = tm.tm_wday;
  97. xt->tm_yday = tm.tm_yday;
  98. xt->tm_isdst = tm.tm_isdst;
  99. xt->tm_gmtoff = get_offset(&tm);
  100. }
  101. APR_DECLARE(fspr_status_t) fspr_time_exp_tz(fspr_time_exp_t *result,
  102. fspr_time_t input, fspr_int32_t offs)
  103. {
  104. explode_time(result, input, offs, 0);
  105. result->tm_gmtoff = offs;
  106. return APR_SUCCESS;
  107. }
  108. APR_DECLARE(fspr_status_t) fspr_time_exp_gmt(fspr_time_exp_t *result,
  109. fspr_time_t input)
  110. {
  111. return fspr_time_exp_tz(result, input, 0);
  112. }
  113. APR_DECLARE(fspr_status_t) fspr_time_exp_lt(fspr_time_exp_t *result,
  114. fspr_time_t input)
  115. {
  116. #if defined(__EMX__)
  117. /* EMX gcc (OS/2) has a timezone global we can use */
  118. return fspr_time_exp_tz(result, input, -timezone);
  119. #else
  120. explode_time(result, input, 0, 1);
  121. return APR_SUCCESS;
  122. #endif /* __EMX__ */
  123. }
  124. APR_DECLARE(fspr_status_t) fspr_time_exp_get(fspr_time_t *t, fspr_time_exp_t *xt)
  125. {
  126. fspr_time_t year = xt->tm_year;
  127. fspr_time_t days;
  128. static const int dayoffset[12] =
  129. {306, 337, 0, 31, 61, 92, 122, 153, 184, 214, 245, 275};
  130. /* shift new year to 1st March in order to make leap year calc easy */
  131. if (xt->tm_mon < 2)
  132. year--;
  133. /* Find number of days since 1st March 1900 (in the Gregorian calendar). */
  134. days = year * 365 + year / 4 - year / 100 + (year / 100 + 3) / 4;
  135. days += dayoffset[xt->tm_mon] + xt->tm_mday - 1;
  136. days -= 25508; /* 1 jan 1970 is 25508 days since 1 mar 1900 */
  137. days = ((days * 24 + xt->tm_hour) * 60 + xt->tm_min) * 60 + xt->tm_sec;
  138. if (days < 0) {
  139. return APR_EBADDATE;
  140. }
  141. *t = days * APR_USEC_PER_SEC + xt->tm_usec;
  142. return APR_SUCCESS;
  143. }
  144. APR_DECLARE(fspr_status_t) fspr_time_exp_gmt_get(fspr_time_t *t,
  145. fspr_time_exp_t *xt)
  146. {
  147. fspr_status_t status = fspr_time_exp_get(t, xt);
  148. if (status == APR_SUCCESS)
  149. *t -= (fspr_time_t) xt->tm_gmtoff * APR_USEC_PER_SEC;
  150. return status;
  151. }
  152. APR_DECLARE(fspr_status_t) fspr_os_imp_time_get(fspr_os_imp_time_t **ostime,
  153. fspr_time_t *aprtime)
  154. {
  155. (*ostime)->tv_usec = *aprtime % APR_USEC_PER_SEC;
  156. (*ostime)->tv_sec = *aprtime / APR_USEC_PER_SEC;
  157. return APR_SUCCESS;
  158. }
  159. APR_DECLARE(fspr_status_t) fspr_os_exp_time_get(fspr_os_exp_time_t **ostime,
  160. fspr_time_exp_t *aprtime)
  161. {
  162. (*ostime)->tm_sec = aprtime->tm_sec;
  163. (*ostime)->tm_min = aprtime->tm_min;
  164. (*ostime)->tm_hour = aprtime->tm_hour;
  165. (*ostime)->tm_mday = aprtime->tm_mday;
  166. (*ostime)->tm_mon = aprtime->tm_mon;
  167. (*ostime)->tm_year = aprtime->tm_year;
  168. (*ostime)->tm_wday = aprtime->tm_wday;
  169. (*ostime)->tm_yday = aprtime->tm_yday;
  170. (*ostime)->tm_isdst = aprtime->tm_isdst;
  171. #if defined(HAVE_STRUCT_TM_TM_GMTOFF)
  172. (*ostime)->tm_gmtoff = aprtime->tm_gmtoff;
  173. #elif defined(HAVE_STRUCT_TM___TM_GMTOFF)
  174. (*ostime)->__tm_gmtoff = aprtime->tm_gmtoff;
  175. #endif
  176. return APR_SUCCESS;
  177. }
  178. APR_DECLARE(fspr_status_t) fspr_os_imp_time_put(fspr_time_t *aprtime,
  179. fspr_os_imp_time_t **ostime,
  180. fspr_pool_t *cont)
  181. {
  182. *aprtime = (*ostime)->tv_sec * APR_USEC_PER_SEC + (*ostime)->tv_usec;
  183. return APR_SUCCESS;
  184. }
  185. APR_DECLARE(fspr_status_t) fspr_os_exp_time_put(fspr_time_exp_t *aprtime,
  186. fspr_os_exp_time_t **ostime,
  187. fspr_pool_t *cont)
  188. {
  189. aprtime->tm_sec = (*ostime)->tm_sec;
  190. aprtime->tm_min = (*ostime)->tm_min;
  191. aprtime->tm_hour = (*ostime)->tm_hour;
  192. aprtime->tm_mday = (*ostime)->tm_mday;
  193. aprtime->tm_mon = (*ostime)->tm_mon;
  194. aprtime->tm_year = (*ostime)->tm_year;
  195. aprtime->tm_wday = (*ostime)->tm_wday;
  196. aprtime->tm_yday = (*ostime)->tm_yday;
  197. aprtime->tm_isdst = (*ostime)->tm_isdst;
  198. #if defined(HAVE_STRUCT_TM_TM_GMTOFF)
  199. aprtime->tm_gmtoff = (*ostime)->tm_gmtoff;
  200. #elif defined(HAVE_STRUCT_TM___TM_GMTOFF)
  201. aprtime->tm_gmtoff = (*ostime)->__tm_gmtoff;
  202. #endif
  203. return APR_SUCCESS;
  204. }
  205. APR_DECLARE(void) fspr_sleep(fspr_interval_time_t t)
  206. {
  207. #ifdef OS2
  208. DosSleep(t/1000);
  209. #elif defined(BEOS)
  210. snooze(t);
  211. #elif defined(NETWARE)
  212. delay(t/1000);
  213. #else
  214. struct timeval tv;
  215. tv.tv_usec = t % APR_USEC_PER_SEC;
  216. tv.tv_sec = t / APR_USEC_PER_SEC;
  217. select(0, NULL, NULL, NULL, &tv);
  218. #endif
  219. }
  220. #ifdef OS2
  221. APR_DECLARE(fspr_status_t) fspr_os2_time_to_fspr_time(fspr_time_t *result,
  222. FDATE os2date,
  223. FTIME os2time)
  224. {
  225. struct tm tmpdate;
  226. memset(&tmpdate, 0, sizeof(tmpdate));
  227. tmpdate.tm_hour = os2time.hours;
  228. tmpdate.tm_min = os2time.minutes;
  229. tmpdate.tm_sec = os2time.twosecs * 2;
  230. tmpdate.tm_mday = os2date.day;
  231. tmpdate.tm_mon = os2date.month - 1;
  232. tmpdate.tm_year = os2date.year + 80;
  233. tmpdate.tm_isdst = -1;
  234. *result = mktime(&tmpdate) * APR_USEC_PER_SEC;
  235. return APR_SUCCESS;
  236. }
  237. APR_DECLARE(fspr_status_t) fspr_fspr_time_to_os2_time(FDATE *os2date,
  238. FTIME *os2time,
  239. fspr_time_t aprtime)
  240. {
  241. time_t ansitime = aprtime / APR_USEC_PER_SEC;
  242. struct tm *lt;
  243. lt = localtime(&ansitime);
  244. os2time->hours = lt->tm_hour;
  245. os2time->minutes = lt->tm_min;
  246. os2time->twosecs = lt->tm_sec / 2;
  247. os2date->day = lt->tm_mday;
  248. os2date->month = lt->tm_mon + 1;
  249. os2date->year = lt->tm_year - 80;
  250. return APR_SUCCESS;
  251. }
  252. #endif
  253. #ifdef NETWARE
  254. APR_DECLARE(void) fspr_netware_setup_time(void)
  255. {
  256. tzset();
  257. server_gmt_offset = -TZONE;
  258. }
  259. #else
  260. APR_DECLARE(void) fspr_unix_setup_time(void)
  261. {
  262. #ifdef NO_GMTOFF_IN_STRUCT_TM
  263. /* Precompute the offset from GMT on systems where it's not
  264. in struct tm.
  265. Note: This offset is normalized to be independent of daylight
  266. savings time; if the calculation happens to be done in a
  267. time/place where a daylight savings adjustment is in effect,
  268. the returned offset has the same value that it would have
  269. in the same location if daylight savings were not in effect.
  270. The reason for this is that the returned offset can be
  271. applied to a past or future timestamp in explode_time(),
  272. so the DST adjustment obtained from the current time won't
  273. necessarily be applicable.
  274. mktime() is the inverse of localtime(); so, presumably,
  275. passing in a struct tm made by gmtime() let's us calculate
  276. the true GMT offset. However, there's a catch: if daylight
  277. savings is in effect, gmtime()will set the tm_isdst field
  278. and confuse mktime() into returning a time that's offset
  279. by one hour. In that case, we must adjust the calculated GMT
  280. offset.
  281. */
  282. struct timeval now;
  283. time_t t1, t2;
  284. struct tm t;
  285. gettimeofday(&now, NULL);
  286. t1 = now.tv_sec;
  287. t2 = 0;
  288. #if APR_HAS_THREADS && defined(_POSIX_THREAD_SAFE_FUNCTIONS)
  289. gmtime_r(&t1, &t);
  290. #else
  291. t = *gmtime(&t1);
  292. #endif
  293. t.tm_isdst = 0; /* we know this GMT time isn't daylight-savings */
  294. t2 = mktime(&t);
  295. server_gmt_offset = (fspr_int32_t) difftime(t1, t2);
  296. #endif /* NO_GMTOFF_IN_STRUCT_TM */
  297. }
  298. #endif
  299. /* A noop on all known Unix implementations */
  300. APR_DECLARE(void) fspr_time_clock_hires(fspr_pool_t *p)
  301. {
  302. return;
  303. }