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