timestr.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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/fspr_arch_atime.h"
  17. #include "fspr_portable.h"
  18. #include "fspr_strings.h"
  19. APR_DECLARE_DATA const char fspr_month_snames[12][4] =
  20. {
  21. "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  22. };
  23. APR_DECLARE_DATA const char fspr_day_snames[7][4] =
  24. {
  25. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  26. };
  27. APR_DECLARE(fspr_status_t) fspr_rfc822_date(char *date_str, fspr_time_t t)
  28. {
  29. fspr_time_exp_t xt;
  30. const char *s;
  31. int real_year;
  32. fspr_time_exp_gmt(&xt, t);
  33. /* example: "Sat, 08 Jan 2000 18:31:41 GMT" */
  34. /* 12345678901234567890123456789 */
  35. s = &fspr_day_snames[xt.tm_wday][0];
  36. *date_str++ = *s++;
  37. *date_str++ = *s++;
  38. *date_str++ = *s++;
  39. *date_str++ = ',';
  40. *date_str++ = ' ';
  41. *date_str++ = xt.tm_mday / 10 + '0';
  42. *date_str++ = xt.tm_mday % 10 + '0';
  43. *date_str++ = ' ';
  44. s = &fspr_month_snames[xt.tm_mon][0];
  45. *date_str++ = *s++;
  46. *date_str++ = *s++;
  47. *date_str++ = *s++;
  48. *date_str++ = ' ';
  49. real_year = 1900 + xt.tm_year;
  50. /* This routine isn't y10k ready. */
  51. *date_str++ = real_year / 1000 + '0';
  52. *date_str++ = real_year % 1000 / 100 + '0';
  53. *date_str++ = real_year % 100 / 10 + '0';
  54. *date_str++ = real_year % 10 + '0';
  55. *date_str++ = ' ';
  56. *date_str++ = xt.tm_hour / 10 + '0';
  57. *date_str++ = xt.tm_hour % 10 + '0';
  58. *date_str++ = ':';
  59. *date_str++ = xt.tm_min / 10 + '0';
  60. *date_str++ = xt.tm_min % 10 + '0';
  61. *date_str++ = ':';
  62. *date_str++ = xt.tm_sec / 10 + '0';
  63. *date_str++ = xt.tm_sec % 10 + '0';
  64. *date_str++ = ' ';
  65. *date_str++ = 'G';
  66. *date_str++ = 'M';
  67. *date_str++ = 'T';
  68. *date_str++ = 0;
  69. return APR_SUCCESS;
  70. }
  71. APR_DECLARE(fspr_status_t) fspr_ctime(char *date_str, fspr_time_t t)
  72. {
  73. fspr_time_exp_t xt;
  74. const char *s;
  75. int real_year;
  76. /* example: "Wed Jun 30 21:49:08 1993" */
  77. /* 123456789012345678901234 */
  78. fspr_time_exp_lt(&xt, t);
  79. s = &fspr_day_snames[xt.tm_wday][0];
  80. *date_str++ = *s++;
  81. *date_str++ = *s++;
  82. *date_str++ = *s++;
  83. *date_str++ = ' ';
  84. s = &fspr_month_snames[xt.tm_mon][0];
  85. *date_str++ = *s++;
  86. *date_str++ = *s++;
  87. *date_str++ = *s++;
  88. *date_str++ = ' ';
  89. *date_str++ = xt.tm_mday / 10 + '0';
  90. *date_str++ = xt.tm_mday % 10 + '0';
  91. *date_str++ = ' ';
  92. *date_str++ = xt.tm_hour / 10 + '0';
  93. *date_str++ = xt.tm_hour % 10 + '0';
  94. *date_str++ = ':';
  95. *date_str++ = xt.tm_min / 10 + '0';
  96. *date_str++ = xt.tm_min % 10 + '0';
  97. *date_str++ = ':';
  98. *date_str++ = xt.tm_sec / 10 + '0';
  99. *date_str++ = xt.tm_sec % 10 + '0';
  100. *date_str++ = ' ';
  101. real_year = 1900 + xt.tm_year;
  102. *date_str++ = real_year / 1000 + '0';
  103. *date_str++ = real_year % 1000 / 100 + '0';
  104. *date_str++ = real_year % 100 / 10 + '0';
  105. *date_str++ = real_year % 10 + '0';
  106. *date_str++ = 0;
  107. return APR_SUCCESS;
  108. }
  109. #ifndef _WIN32_WCE
  110. fspr_size_t win32_strftime_extra(char *s, size_t max, const char *format,
  111. const struct tm *tm)
  112. {
  113. /* If the new format string is bigger than max, the result string won't fit
  114. * anyway. If format strings are added, made sure the padding below is
  115. * enough */
  116. char *new_format = (char *) malloc(max + 11);
  117. size_t i, j, format_length = strlen(format);
  118. fspr_size_t return_value;
  119. int length_written;
  120. for (i = 0, j = 0; (i < format_length && j < max);) {
  121. if (format[i] != '%') {
  122. new_format[j++] = format[i++];
  123. continue;
  124. }
  125. switch (format[i+1]) {
  126. case 'C':
  127. length_written = fspr_snprintf(new_format + j, max - j, "%2d",
  128. (tm->tm_year + 1970)/100);
  129. j = (length_written == -1) ? max : (j + length_written);
  130. i += 2;
  131. break;
  132. case 'D':
  133. /* Is this locale dependent? Shouldn't be...
  134. Also note the year 2000 exposure here */
  135. memcpy(new_format + j, "%m/%d/%y", 8);
  136. i += 2;
  137. j += 8;
  138. break;
  139. case 'r':
  140. memcpy(new_format + j, "%I:%M:%S %p", 11);
  141. i += 2;
  142. j += 11;
  143. break;
  144. case 'R':
  145. memcpy(new_format + j, "%H:%M", 5);
  146. i += 2;
  147. j += 5;
  148. break;
  149. case 'T':
  150. memcpy(new_format + j, "%H:%M:%S", 8);
  151. i += 2;
  152. j += 8;
  153. break;
  154. case 'e':
  155. length_written = fspr_snprintf(new_format + j, max - j, "%2d",
  156. tm->tm_mday);
  157. j = (length_written == -1) ? max : (j + length_written);
  158. i += 2;
  159. break;
  160. default:
  161. /* We know we can advance two characters forward here. Also
  162. * makes sure that %% is preserved. */
  163. new_format[j++] = format[i++];
  164. new_format[j++] = format[i++];
  165. }
  166. }
  167. if (j >= max) {
  168. *s = '\0'; /* Defensive programming, okay since output is undefined*/
  169. return_value = 0;
  170. } else {
  171. new_format[j] = '\0';
  172. return_value = strftime(s, max, new_format, tm);
  173. }
  174. free(new_format);
  175. return return_value;
  176. }
  177. #endif
  178. APR_DECLARE(fspr_status_t) fspr_strftime(char *s, fspr_size_t *retsize,
  179. fspr_size_t max, const char *format,
  180. fspr_time_exp_t *xt)
  181. {
  182. #ifdef _WIN32_WCE
  183. return APR_ENOTIMPL;
  184. #else
  185. struct tm tm;
  186. memset(&tm, 0, sizeof tm);
  187. tm.tm_sec = xt->tm_sec;
  188. tm.tm_min = xt->tm_min;
  189. tm.tm_hour = xt->tm_hour;
  190. tm.tm_mday = xt->tm_mday;
  191. tm.tm_mon = xt->tm_mon;
  192. tm.tm_year = xt->tm_year;
  193. tm.tm_wday = xt->tm_wday;
  194. tm.tm_yday = xt->tm_yday;
  195. tm.tm_isdst = xt->tm_isdst;
  196. (*retsize) = win32_strftime_extra(s, max, format, &tm);
  197. return APR_SUCCESS;
  198. #endif
  199. }