22 static const int num_weekdays = 7;
23 static const char *
const weekdays[num_weekdays] = {
24 "Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat" 27 static const int num_months = 12;
28 static const char *
const months[num_months] = {
29 "Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
30 "Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec" 39 HTTPDate(
const string &format) {
43 memset(&t, 0,
sizeof(t));
45 bool got_weekday =
false;
46 bool got_month =
false;
48 bool got_year =
false;
49 bool got_hour =
false;
50 bool got_minute =
false;
51 bool got_second =
false;
58 ExpectNext expect_next = EN_none;
61 string token = get_token(format, pos);
62 while (!token.empty()) {
63 ExpectNext expected = expect_next;
64 expect_next = EN_none;
66 if (isdigit(token[0])) {
68 int value = atoi(token.c_str());
69 if (token[token.length() - 1] ==
':') {
75 }
else if (!got_minute) {
78 expect_next = EN_second;
84 }
else if (token[token.length() - 1] ==
'/') {
90 }
else if (!got_day) {
93 expect_next = EN_year;
100 if (expected == EN_second) {
105 }
else if (expected == EN_year) {
110 }
else if (!got_day) {
115 }
else if (!got_year) {
120 }
else if (!got_hour) {
124 }
else if (!got_minute) {
128 }
else if (!got_second) {
142 bool matched =
false;
145 for (i = 0; i < num_weekdays && !matched; i++) {
146 if (token == weekdays[i]) {
156 for (i = 0; i < num_months && !matched; i++) {
157 if (token == months[i]) {
167 if (!matched && token ==
"Gmt") {
177 token = get_token(format, pos);
181 if (!(got_month && got_day && got_year && got_hour && got_minute)) {
186 if (t.tm_year < 100) {
189 time_t
now = time(
nullptr);
190 struct tm *tp = gmtime(&
now);
191 t.tm_year += 100 * (tp->tm_year / 100);
192 if (t.tm_year - tp->tm_year > 50) {
196 }
else if (t.tm_year < 1900) {
204 if (!((t.tm_mon >= 0 && t.tm_mon < num_months) &&
205 (t.tm_mday >= 1 && t.tm_mday <= 31) &&
206 (t.tm_hour >= 0 && t.tm_hour < 60) &&
207 (t.tm_min >= 0 && t.tm_min < 60) &&
208 (t.tm_sec >= 0 && t.tm_sec < 62) )) {
223 if (_time != (time_t)-1) {
225 #if defined(IS_FREEBSD) 226 time_t
now = time(
nullptr);
227 struct tm *tp = localtime(&
now);
228 _time -= tp->tm_gmtoff;
229 #elif defined(_WIN32) 231 _get_timezone(&timezone);
234 extern long int timezone;
247 return "Invalid Date";
250 struct tm *tp = gmtime(&_time);
252 std::ostringstream result;
254 << weekdays[tp->tm_wday] <<
", " 255 << setw(2) << setfill(
'0') << tp->tm_mday <<
" " 256 << months[tp->tm_mon] <<
" " 257 << setw(4) << setfill(
'0') << tp->tm_year + 1900 <<
" " 258 << setw(2) << setfill(
'0') << tp->tm_hour <<
":" 259 << setw(2) << setfill(
'0') << tp->tm_min <<
":" 260 << setw(2) << setfill(
'0') << tp->tm_sec <<
" GMT";
270 input(std::istream &in) {
282 while (!in.fail() && ch !=
'"') {
301 output(std::ostream &out)
const {
304 out <<
'"' << get_string() <<
'"';
319 get_token(
const string &str,
size_t &pos) {
322 while (start < str.length() && !isalnum(str[start])) {
326 if (start >= str.length()) {
334 if (isalpha(str[start])) {
336 token = toupper(str[start]);
338 while (pos < str.length() && isalpha(str[pos])) {
339 if (token.length() < 3) {
340 token += tolower(str[pos]);
348 while (pos < str.length() && isdigit(str[pos])) {
352 if (pos < str.length() && !isalpha(str[pos])) {
355 token = str.substr(start, pos - start);
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bool is_valid() const
Returns true if the date is meaningful, or false if it is -1 (which generally indicates the source st...
A container for an HTTP-legal time/date indication.
static HTTPDate now()
Returns an HTTPDate that represents the current time and date.