int main(void) { struct universaltime ut; Octstr *os; Octstr *buf; gwlib_init(); buf = octstr_create(""); while ((os = read_line(stdin, buf)) != NULL) { if (parse_date(&ut, os) == -1) panic(0, "Bad line: %s", octstr_get_cstr(os)); printf("%ld %s\n", date_convert_universal(&ut), octstr_get_cstr(os)); octstr_destroy(os); } log_set_output_level(GW_PANIC); gwlib_shutdown(); return 0; }
long date_parse_http(Octstr *date) { long pos; struct universaltime t; Octstr *monthstr = NULL; /* First, skip the leading day-of-week string. */ pos = octstr_search_char(date, ' ', 0); if (pos < 0 || pos == octstr_len(date) - 1) return -1; pos++; /* Skip the space */ /* Distinguish between the three acceptable formats */ if (isdigit(octstr_get_char(date, pos)) && octstr_get_char(date, pos + 2) == ' ') { if (octstr_len(date) - pos < (long)strlen("06 Nov 1994 08:49:37 GMT")) goto error; if (octstr_parse_long(&t.day, date, pos, 10) != pos + 2) goto error; monthstr = octstr_copy(date, pos + 3, 3); if (octstr_parse_long(&t.year, date, pos + 7, 10) != pos + 11) goto error; if (octstr_parse_long(&t.hour, date, pos + 12, 10) != pos + 14) goto error; if (octstr_parse_long(&t.minute, date, pos + 15, 10) != pos + 17) goto error; if (octstr_parse_long(&t.second, date, pos + 18, 10) != pos + 20) goto error; /* Take the GMT part on faith. */ } else if (isdigit(octstr_get_char(date, pos)) && octstr_get_char(date, pos + 2) == '-') { if (octstr_len(date) - pos < (long)strlen("06-Nov-94 08:49:37 GMT")) goto error; if (octstr_parse_long(&t.day, date, pos, 10) != pos + 2) goto error; monthstr = octstr_copy(date, pos + 3, 3); if (octstr_parse_long(&t.year, date, pos + 7, 10) != pos + 9) goto error; if (t.year > 60) t.year += 1900; else t.year += 2000; if (octstr_parse_long(&t.hour, date, pos + 10, 10) != pos + 12) goto error; if (octstr_parse_long(&t.minute, date, pos + 13, 10) != pos + 15) goto error; if (octstr_parse_long(&t.second, date, pos + 16, 10) != pos + 18) goto error; /* Take the GMT part on faith. */ } else { if (octstr_len(date) - pos < (long)strlen(" 6 08:49:37 1994")) goto error; monthstr = octstr_copy(date, pos, 3); if (octstr_parse_long(&t.day, date, pos + 4, 10) != pos + 6) goto error; if (octstr_parse_long(&t.hour, date, pos + 7, 10) != pos + 9) goto error; if (octstr_parse_long(&t.minute, date, pos + 10, 10) != pos + 12) goto error; if (octstr_parse_long(&t.second, date, pos + 13, 10) != pos + 15) goto error; if (octstr_parse_long(&t.year, date, pos + 16, 10) != pos + 20) goto error; } for (t.month = 0; t.month < 12; t.month++) { if (octstr_str_compare(monthstr, monthname[t.month]) == 0) break; } if (t.month == 12) goto error; octstr_destroy(monthstr); return date_convert_universal(&t); error: octstr_destroy(monthstr); return -1; }