testdate.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* This program tests the date_parse_http routine in ../main/util_date.c.
  2. *
  3. * It is only semiautomated in that I would run it, modify the code to
  4. * use a different algorithm or seed, recompile and run again, etc.
  5. * Obviously it should use an argument for that, but I never got around
  6. * to changing the implementation.
  7. *
  8. * gcc -g -O2 -I../main -o test_date ../main/util_date.o test_date.c
  9. * test_date | egrep '^No '
  10. *
  11. * Roy Fielding, 1996
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <time.h>
  16. #include "apr_date.h"
  17. #ifndef srand48
  18. #define srand48 srandom
  19. #endif
  20. #ifndef mrand48
  21. #define mrand48 random
  22. #endif
  23. void gm_timestr_822(char *ts, apr_time_t sec);
  24. void gm_timestr_850(char *ts, apr_time_t sec);
  25. void gm_timestr_ccc(char *ts, apr_time_t sec);
  26. static const apr_time_t year2secs[] = {
  27. 0LL, /* 1970 */
  28. 31536000LL, /* 1971 */
  29. 63072000LL, /* 1972 */
  30. 94694400LL, /* 1973 */
  31. 126230400LL, /* 1974 */
  32. 157766400LL, /* 1975 */
  33. 189302400LL, /* 1976 */
  34. 220924800LL, /* 1977 */
  35. 252460800LL, /* 1978 */
  36. 283996800LL, /* 1979 */
  37. 315532800LL, /* 1980 */
  38. 347155200LL, /* 1981 */
  39. 378691200LL, /* 1982 */
  40. 410227200LL, /* 1983 */
  41. 441763200LL, /* 1984 */
  42. 473385600LL, /* 1985 */
  43. 504921600LL, /* 1986 */
  44. 536457600LL, /* 1987 */
  45. 567993600LL, /* 1988 */
  46. 599616000LL, /* 1989 */
  47. 631152000LL, /* 1990 */
  48. 662688000LL, /* 1991 */
  49. 694224000LL, /* 1992 */
  50. 725846400LL, /* 1993 */
  51. 757382400LL, /* 1994 */
  52. 788918400LL, /* 1995 */
  53. 820454400LL, /* 1996 */
  54. 852076800LL, /* 1997 */
  55. 883612800LL, /* 1998 */
  56. 915148800LL, /* 1999 */
  57. 946684800LL, /* 2000 */
  58. 978307200LL, /* 2001 */
  59. 1009843200LL, /* 2002 */
  60. 1041379200LL, /* 2003 */
  61. 1072915200LL, /* 2004 */
  62. 1104537600LL, /* 2005 */
  63. 1136073600LL, /* 2006 */
  64. 1167609600LL, /* 2007 */
  65. 1199145600LL, /* 2008 */
  66. 1230768000LL, /* 2009 */
  67. 1262304000LL, /* 2010 */
  68. 1293840000LL, /* 2011 */
  69. 1325376000LL, /* 2012 */
  70. 1356998400LL, /* 2013 */
  71. 1388534400LL, /* 2014 */
  72. 1420070400LL, /* 2015 */
  73. 1451606400LL, /* 2016 */
  74. 1483228800LL, /* 2017 */
  75. 1514764800LL, /* 2018 */
  76. 1546300800LL, /* 2019 */
  77. 1577836800LL, /* 2020 */
  78. 1609459200LL, /* 2021 */
  79. 1640995200LL, /* 2022 */
  80. 1672531200LL, /* 2023 */
  81. 1704067200LL, /* 2024 */
  82. 1735689600LL, /* 2025 */
  83. 1767225600LL, /* 2026 */
  84. 1798761600LL, /* 2027 */
  85. 1830297600LL, /* 2028 */
  86. 1861920000LL, /* 2029 */
  87. 1893456000LL, /* 2030 */
  88. 1924992000LL, /* 2031 */
  89. 1956528000LL, /* 2032 */
  90. 1988150400LL, /* 2033 */
  91. 2019686400LL, /* 2034 */
  92. 2051222400LL, /* 2035 */
  93. 2082758400LL, /* 2036 */
  94. 2114380800LL, /* 2037 */
  95. 2145916800LL /* 2038 */
  96. };
  97. const char month_snames[12][4] = {
  98. "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
  99. };
  100. void gm_timestr_822(char *ts, apr_time_t sec)
  101. {
  102. static const char *const days[7]=
  103. {"Sun","Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
  104. struct tm *tms;
  105. time_t ls = (time_t)sec;
  106. tms = gmtime(&ls);
  107. sprintf(ts, "%s, %.2d %s %d %.2d:%.2d:%.2d GMT", days[tms->tm_wday],
  108. tms->tm_mday, month_snames[tms->tm_mon], tms->tm_year + 1900,
  109. tms->tm_hour, tms->tm_min, tms->tm_sec);
  110. }
  111. void gm_timestr_850(char *ts, apr_time_t sec)
  112. {
  113. static const char *const days[7]=
  114. {"Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
  115. "Saturday"};
  116. struct tm *tms;
  117. int year;
  118. time_t ls = (time_t)sec;
  119. tms = gmtime(&ls);
  120. year = tms->tm_year;
  121. if (year >= 100) year -= 100;
  122. sprintf(ts, "%s, %.2d-%s-%.2d %.2d:%.2d:%.2d GMT", days[tms->tm_wday],
  123. tms->tm_mday, month_snames[tms->tm_mon], year,
  124. tms->tm_hour, tms->tm_min, tms->tm_sec);
  125. }
  126. void gm_timestr_ccc(char *ts, apr_time_t sec)
  127. {
  128. static const char *const days[7]=
  129. {"Sun","Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
  130. struct tm *tms;
  131. time_t ls = (time_t)sec;
  132. tms = gmtime(&ls);
  133. sprintf(ts, "%s %s %2d %.2d:%.2d:%.2d %d", days[tms->tm_wday],
  134. month_snames[tms->tm_mon], tms->tm_mday,
  135. tms->tm_hour, tms->tm_min, tms->tm_sec, tms->tm_year + 1900);
  136. }
  137. int main (void)
  138. {
  139. int year, i;
  140. apr_time_t guess;
  141. apr_time_t offset = 0;
  142. /* apr_time_t offset = 0; */
  143. /* apr_time_t offset = ((31 + 28) * 24 * 3600) - 1; */
  144. apr_time_t secstodate, newsecs;
  145. char datestr[50];
  146. for (year = 1970; year < 2038; ++year) {
  147. secstodate = year2secs[year - 1970] + offset;
  148. gm_timestr_822(datestr, secstodate);
  149. secstodate *= APR_USEC_PER_SEC;
  150. newsecs = apr_date_parse_http(datestr);
  151. if (secstodate == newsecs)
  152. printf("Yes %4d %19" APR_TIME_T_FMT " %s\n", year, secstodate, datestr);
  153. else if (newsecs == APR_DATE_BAD)
  154. printf("No %4d %19" APR_TIME_T_FMT " %19" APR_TIME_T_FMT " %s\n",
  155. year, secstodate, newsecs, datestr);
  156. else
  157. printf("No* %4d %19" APR_TIME_T_FMT " %19" APR_TIME_T_FMT " %s\n",
  158. year, secstodate, newsecs, datestr);
  159. }
  160. srand48(978245L);
  161. for (i = 0; i < 10000; ++i) {
  162. guess = (time_t)mrand48();
  163. if (guess < 0) guess *= -1;
  164. secstodate = guess + offset;
  165. gm_timestr_822(datestr, secstodate);
  166. secstodate *= APR_USEC_PER_SEC;
  167. newsecs = apr_date_parse_http(datestr);
  168. if (secstodate == newsecs)
  169. printf("Yes %" APR_TIME_T_FMT " %s\n", secstodate, datestr);
  170. else if (newsecs == APR_DATE_BAD)
  171. printf("No %" APR_TIME_T_FMT " %" APR_TIME_T_FMT " %s\n",
  172. secstodate, newsecs, datestr);
  173. else
  174. printf("No* %" APR_TIME_T_FMT " %" APR_TIME_T_FMT " %s\n",
  175. secstodate, newsecs, datestr);
  176. }
  177. exit(0);
  178. }