2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
26 #include "php_http_std_defs.h"
28 static int check_day(char *day
, size_t len
);
29 static int check_month(char *month
);
30 static int check_tzone(char *tzone
);
32 /* {{{ day/month names */
33 static const char *days
[] = {
34 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
36 static const char *wkdays
[] = {
37 "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
39 static const char *weekdays
[] = {
40 "Monday", "Tuesday", "Wednesday",
41 "Thursday", "Friday", "Saturday", "Sunday"
43 static const char *months
[] = {
44 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
45 "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"
52 static const struct time_zone
{
56 {"GMT", 0}, /* Greenwich Mean */
57 {"UTC", 0}, /* Universal (Coordinated) */
58 {"WET", 0}, /* Western European */
59 {"BST", 0}, /* British Summer */
60 {"WAT", 60}, /* West Africa */
61 {"AST", 240}, /* Atlantic Standard */
62 {"ADT", 240}, /* Atlantic Daylight */
63 {"EST", 300}, /* Eastern Standard */
64 {"EDT", 300}, /* Eastern Daylight */
65 {"CST", 360}, /* Central Standard */
66 {"CDT", 360}, /* Central Daylight */
67 {"MST", 420}, /* Mountain Standard */
68 {"MDT", 420}, /* Mountain Daylight */
69 {"PST", 480}, /* Pacific Standard */
70 {"PDT", 480}, /* Pacific Daylight */
71 {"YST", 540}, /* Yukon Standard */
72 {"YDT", 540}, /* Yukon Daylight */
73 {"HST", 600}, /* Hawaii Standard */
74 {"HDT", 600}, /* Hawaii Daylight */
75 {"CAT", 600}, /* Central Alaska */
76 {"AHST", 600}, /* Alaska-Hawaii Standard */
77 {"NT", 660}, /* Nome */
78 {"IDLW", 720}, /* International Date Line West */
79 {"CET", -60}, /* Central European */
80 {"MET", -60}, /* Middle European */
81 {"MEWT", -60}, /* Middle European Winter */
82 {"MEST", -120}, /* Middle European Summer */
83 {"CEST", -120}, /* Central European Summer */
84 {"MESZ", -60}, /* Middle European Summer */
85 {"FWT", -60}, /* French Winter */
86 {"FST", -60}, /* French Summer */
87 {"EET", -120}, /* Eastern Europe, USSR Zone 1 */
88 {"WAST", -420}, /* West Australian Standard */
89 {"WADT", -420}, /* West Australian Daylight */
90 {"CCT", -480}, /* China Coast, USSR Zone 7 */
91 {"JST", -540}, /* Japan Standard, USSR Zone 8 */
92 {"EAST", -600}, /* Eastern Australian Standard */
93 {"EADT", -600}, /* Eastern Australian Daylight */
94 {"GST", -600}, /* Guam Standard, USSR Zone 9 */
95 {"NZT", -720}, /* New Zealand */
96 {"NZST", -720}, /* New Zealand Standard */
97 {"NZDT", -720}, /* New Zealand Daylight */
98 {"IDLE", -720}, /* International Date Line East */
102 /* {{{ Day/Month/TZ checks for http_parse_date()
103 Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. */
104 static int check_day(char *day
, size_t len
)
107 const char * const *check
= (len
> 3) ? &weekdays
[0] : &wkdays
[0];
108 for (i
= 0; i
< 7; i
++) {
109 if (!strcmp(day
, check
[0])) {
117 static int check_month(char *month
)
120 const char * const *check
= &months
[0];
121 for (i
= 0; i
< 12; i
++) {
122 if (!strcmp(month
, check
[0])) {
130 /* return the time zone offset between GMT and the input one, in number
131 of seconds or -1 if the timezone wasn't found/legal */
133 static int check_tzone(char *tzone
)
136 const struct time_zone
*check
= time_zones
;
137 for (i
= 0; i
< sizeof(time_zones
) / sizeof(time_zones
[0]); i
++) {
138 if (!strcmp(tzone
, check
->name
)) {
139 return check
->offset
* 60;
147 /* {{{ char *http_date(time_t) */
148 PHP_HTTP_API
char *_http_date(time_t t TSRMLS_DC
)
150 struct tm
*gmtime
, tmbuf
;
152 if (gmtime
= php_gmtime_r(&t
, &tmbuf
)) {
153 char *date
= ecalloc(1, 31);
155 "%s, %02d %s %04d %02d:%02d:%02d GMT",
156 days
[gmtime
->tm_wday
], gmtime
->tm_mday
,
157 months
[gmtime
->tm_mon
], gmtime
->tm_year
+ 1900,
158 gmtime
->tm_hour
, gmtime
->tm_min
, gmtime
->tm_sec
167 /* {{{ time_t http_parse_date(char *)
168 Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. */
169 PHP_HTTP_API
time_t _http_parse_date(const char *date
)
172 int tz_offset
= -1, year
= -1, month
= -1, monthday
= -1, weekday
= -1,
173 hours
= -1, minutes
= -1, seconds
= -1;
175 enum assume_next dignext
= DATE_MDAY
;
176 const char *indate
= date
;
178 int found
= 0, part
= 0; /* max 6 parts */
180 while (*date
&& (part
< 6)) {
183 while (*date
&& !isalnum(*date
)) {
187 if (isalpha(*date
)) {
188 /* a name coming up */
191 sscanf(date
, "%31[A-Za-z]", buf
);
195 weekday
= check_day(buf
, len
);
201 if (!found
&& (month
== -1)) {
202 month
= check_month(buf
);
208 if (!found
&& (tz_offset
== -1)) {
209 /* this just must be a time zone string */
210 tz_offset
= check_tzone(buf
);
211 if (tz_offset
!= -1) {
217 return -1; /* bad string */
221 else if (isdigit(*date
)) {
225 if ((seconds
== -1) &&
226 (3 == sscanf(date
, "%02d:%02d:%02d", &hours
, &minutes
, &seconds
))) {
232 val
= (int) strtol(date
, &end
, 10);
234 if ((tz_offset
== -1) && ((end
- date
) == 4) && (val
< 1300) &&
235 (indate
< date
) && ((date
[-1] == '+' || date
[-1] == '-'))) {
236 /* four digits and a value less than 1300 and it is preceeded with
237 a plus or minus. This is a time zone indication. */
239 tz_offset
= (val
/ 100 * 60 + val
% 100) * 60;
241 /* the + and - prefix indicates the local time compared to GMT,
242 this we need ther reversed math to get what we want */
243 tz_offset
= date
[-1] == '+' ? -tz_offset
: tz_offset
;
246 if (((end
- date
) == 8) && (year
== -1) && (month
== -1) && (monthday
== -1)) {
247 /* 8 digits, no year, month or day yet. This is YYYYMMDD */
250 month
= (val
% 10000) / 100 - 1; /* month is 0 - 11 */
251 monthday
= val
% 100;
254 if (!found
&& (dignext
== DATE_MDAY
) && (monthday
== -1)) {
255 if ((val
> 0) && (val
< 32)) {
262 if (!found
&& (dignext
== DATE_YEAR
) && (year
== -1)) {
266 year
+= year
> 70 ? 1900 : 2000;
285 seconds
= minutes
= hours
= 0; /* no time, make it zero */
288 if ((-1 == monthday
) || (-1 == month
) || (-1 == year
)) {
289 /* lacks vital info, fail */
293 if (sizeof(time_t) < 5) {
294 /* 32 bit time_t can only hold dates to the beginning of 2038 */
303 tm
.tm_mday
= monthday
;
305 tm
.tm_year
= year
- 1900;
312 /* time zone adjust */
314 struct tm
*gmt
, keeptime2
;
318 if(!(gmt
= php_gmtime_r(&t
, &keeptime2
))) {
319 return -1; /* illegal date/time */
324 /* Add the time zone diff (between the given timezone and GMT) and the
325 diff between the local time zone and GMT. */
326 delta
= (tz_offset
!= -1 ? tz_offset
: 0) + (t
- t2
);
328 if((delta
> 0) && (t
+ delta
< t
)) {
329 return -1; /* time_t overflow */
345 * vim600: sw=4 ts=4 fdm=marker