timestr.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 "apr_portable.h"
  17. #include "apr_time.h"
  18. #include "apr_lib.h"
  19. #include "apr_private.h"
  20. /* System Headers required for time library */
  21. #if APR_HAVE_SYS_TIME_H
  22. #include <sys/time.h>
  23. #endif
  24. #ifdef HAVE_TIME_H
  25. #include <time.h>
  26. #endif
  27. #if APR_HAVE_STRING_H
  28. #include <string.h>
  29. #endif
  30. /* End System Headers */
  31. APR_DECLARE_DATA const char apr_month_snames[12][4] =
  32. {
  33. "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  34. };
  35. APR_DECLARE_DATA const char apr_day_snames[7][4] =
  36. {
  37. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  38. };
  39. apr_status_t apr_rfc822_date(char *date_str, apr_time_t t)
  40. {
  41. apr_time_exp_t xt;
  42. const char *s;
  43. int real_year;
  44. apr_time_exp_gmt(&xt, t);
  45. /* example: "Sat, 08 Jan 2000 18:31:41 GMT" */
  46. /* 12345678901234567890123456789 */
  47. s = &apr_day_snames[xt.tm_wday][0];
  48. *date_str++ = *s++;
  49. *date_str++ = *s++;
  50. *date_str++ = *s++;
  51. *date_str++ = ',';
  52. *date_str++ = ' ';
  53. *date_str++ = xt.tm_mday / 10 + '0';
  54. *date_str++ = xt.tm_mday % 10 + '0';
  55. *date_str++ = ' ';
  56. s = &apr_month_snames[xt.tm_mon][0];
  57. *date_str++ = *s++;
  58. *date_str++ = *s++;
  59. *date_str++ = *s++;
  60. *date_str++ = ' ';
  61. real_year = 1900 + xt.tm_year;
  62. /* This routine isn't y10k ready. */
  63. *date_str++ = real_year / 1000 + '0';
  64. *date_str++ = real_year % 1000 / 100 + '0';
  65. *date_str++ = real_year % 100 / 10 + '0';
  66. *date_str++ = real_year % 10 + '0';
  67. *date_str++ = ' ';
  68. *date_str++ = xt.tm_hour / 10 + '0';
  69. *date_str++ = xt.tm_hour % 10 + '0';
  70. *date_str++ = ':';
  71. *date_str++ = xt.tm_min / 10 + '0';
  72. *date_str++ = xt.tm_min % 10 + '0';
  73. *date_str++ = ':';
  74. *date_str++ = xt.tm_sec / 10 + '0';
  75. *date_str++ = xt.tm_sec % 10 + '0';
  76. *date_str++ = ' ';
  77. *date_str++ = 'G';
  78. *date_str++ = 'M';
  79. *date_str++ = 'T';
  80. *date_str++ = 0;
  81. return APR_SUCCESS;
  82. }
  83. apr_status_t apr_ctime(char *date_str, apr_time_t t)
  84. {
  85. apr_time_exp_t xt;
  86. const char *s;
  87. int real_year;
  88. /* example: "Wed Jun 30 21:49:08 1993" */
  89. /* 123456789012345678901234 */
  90. apr_time_exp_lt(&xt, t);
  91. s = &apr_day_snames[xt.tm_wday][0];
  92. *date_str++ = *s++;
  93. *date_str++ = *s++;
  94. *date_str++ = *s++;
  95. *date_str++ = ' ';
  96. s = &apr_month_snames[xt.tm_mon][0];
  97. *date_str++ = *s++;
  98. *date_str++ = *s++;
  99. *date_str++ = *s++;
  100. *date_str++ = ' ';
  101. *date_str++ = xt.tm_mday / 10 + '0';
  102. *date_str++ = xt.tm_mday % 10 + '0';
  103. *date_str++ = ' ';
  104. *date_str++ = xt.tm_hour / 10 + '0';
  105. *date_str++ = xt.tm_hour % 10 + '0';
  106. *date_str++ = ':';
  107. *date_str++ = xt.tm_min / 10 + '0';
  108. *date_str++ = xt.tm_min % 10 + '0';
  109. *date_str++ = ':';
  110. *date_str++ = xt.tm_sec / 10 + '0';
  111. *date_str++ = xt.tm_sec % 10 + '0';
  112. *date_str++ = ' ';
  113. real_year = 1900 + xt.tm_year;
  114. *date_str++ = real_year / 1000 + '0';
  115. *date_str++ = real_year % 1000 / 100 + '0';
  116. *date_str++ = real_year % 100 / 10 + '0';
  117. *date_str++ = real_year % 10 + '0';
  118. *date_str++ = 0;
  119. return APR_SUCCESS;
  120. }
  121. apr_status_t apr_strftime(char *s, apr_size_t *retsize, apr_size_t max,
  122. const char *format, apr_time_exp_t *xt)
  123. {
  124. struct tm tm;
  125. memset(&tm, 0, sizeof tm);
  126. tm.tm_sec = xt->tm_sec;
  127. tm.tm_min = xt->tm_min;
  128. tm.tm_hour = xt->tm_hour;
  129. tm.tm_mday = xt->tm_mday;
  130. tm.tm_mon = xt->tm_mon;
  131. tm.tm_year = xt->tm_year;
  132. tm.tm_wday = xt->tm_wday;
  133. tm.tm_yday = xt->tm_yday;
  134. tm.tm_isdst = xt->tm_isdst;
  135. #if defined(HAVE_STRUCT_TM_TM_GMTOFF)
  136. tm.tm_gmtoff = xt->tm_gmtoff;
  137. #elif defined(HAVE_STRUCT_TM___TM_GMTOFF)
  138. tm.__tm_gmtoff = xt->tm_gmtoff;
  139. #endif
  140. (*retsize) = strftime(s, max, format, &tm);
  141. return APR_SUCCESS;
  142. }