| @@ -1,8 +1,25 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * class FeedTime: handle common date-time formats used in feeds. |
| 4 | + * We will try to be as tolerant as possible of different representations, since | |
| 5 | + * RSS Is A Fucking Mess, broken dates seem to be especially pervasive, etc. | |
| 6 | + * Supports anything that your version of PHP's strtotime() can handle; also has | |
| 7 | + * a built-in parser for W3C DTF, in case your PHP doesn't have that. | |
| 4 | 8 | * |
| 9 | + * Tries to parse a W3C DTF first; then falls back to strtotime(). If strtotime | |
| 10 | + * fails due to a mystery timezone, then we will try again on local time, with | |
| 11 | + * the timezone stripped out. | |
| 12 | + * | |
| 13 | + * To parse a string representation of a date-time from a feed, instantiate and | |
| 14 | + * then pull the timestamp: | |
| 15 | + * | |
| 16 | + * $time = new FeedTime($s); | |
| 17 | + * if (!$time->failed()) : | |
| 18 | + * $ts = $time->timestamp(); | |
| 19 | + * else : | |
| 20 | + * // ... | |
| 21 | + * endif; | |
| 5 | 22 | */ |
| 6 | 23 | class FeedTime { |
| 7 | 24 | var $rep; |
| 8 | 25 | var $ts; |
| @@ -10,9 +27,9 @@ | ||
| 10 | 27 | function FeedTime ($time) { |
| 11 | 28 | $this->set($time); |
| 12 | 29 | } /* FeedTime constructor */ |
| 13 | 30 | |
| 14 | - function set ($time) { | |
| 31 | + function set ($time, $recurse = false) { | |
| 15 | 32 | $this->rep = $time; |
| 16 | 33 | $this->ts = NULL; |
| 17 | 34 | if (is_numeric($time)) : // Presumably a Unix-epoch timestamp |
| 18 | 35 | $this->ts = $this->rep; |
| @@ -20,12 +37,12 @@ | ||
| 20 | 37 | // First, try to parse it as a W3C date-time |
| 21 | 38 | $this->ts = $this->parse_w3cdtf(); |
| 22 | 39 | |
| 23 | 40 | if ($this->failed()) : |
| 24 | - // In some versions of PHP, strtotime() does not support | |
| 25 | - // the UT timezone. Since UT is by definition within 1 | |
| 26 | - // second of UTC, we'll just convert it here to avoid | |
| 27 | - // problems. | |
| 41 | + // In some versions of PHP, strtotime() does not | |
| 42 | + // support the UT timezone. But since UT is by | |
| 43 | + // definition within 1 second of UTC, we'll just | |
| 44 | + // convert it preemptively to avoid problems. | |
| 28 | 45 | $time = preg_replace( |
| 29 | 46 | '/(\s)UT$/', |
| 30 | 47 | '$1UTC', |
| 31 | 48 | $time |
| @@ -30,8 +47,14 @@ | ||
| 30 | 47 | '$1UTC', |
| 31 | 48 | $time |
| 32 | 49 | ); |
| 33 | 50 | $this->ts = strtotime($time); |
| 51 | + | |
| 52 | + if ($this->failed() | |
| 53 | + and preg_match('/^(.*)([+\-][0-9]+|\s+[A-Za-z]{1,3})$/', $time, $matches)) : | |
| 54 | + // Try dropping the time zone and recurse | |
| 55 | + $this->set($matches[1], /*recurse=*/ true); | |
| 56 | + endif; | |
| 34 | 57 | endif; |
| 35 | 58 | endif; |
| 36 | 59 | } /* FeedTime::set() */ |
| 37 | 60 | |
| @@ -102,15 +125,19 @@ | ||
| 102 | 125 | $offset = 0; |
| 103 | 126 | if ( isset($match[15]) and $match[15] == 'Z' ) : |
| 104 | 127 | # zulu time, aka GMT |
| 105 | 128 | else : |
| 106 | - $tz_mod = $match[12]; | |
| 107 | - $tz_hour = $match[13]; | |
| 108 | - $tz_min = $match[14]; | |
| 129 | + $tz_mod = (isset($match[12]) ? $match[12] : NULL); | |
| 130 | + $tz_hour = (isset($match[13]) ? $match[13] : NULL); | |
| 131 | + $tz_min = (isset($match[14]) ? $match[14] : NULL); | |
| 109 | 132 | |
| 110 | 133 | # zero out the variables |
| 111 | - if ( ! $tz_hour ) { $tz_hour = 0; } | |
| 112 | - if ( ! $tz_min ) { $tz_min = 0; } | |
| 134 | + if ( is_null($tz_hour) ) : | |
| 135 | + $offset = (int) get_option('gmt_offset'); | |
| 136 | + $tz_hour = abs($offset); | |
| 137 | + $tz_mod = ((abs($offset) != $offset) ? '-' : '+'); | |
| 138 | + endif; | |
| 139 | + if ( is_null($tz_min) ) : $tz_min = 0; endif; | |
| 113 | 140 | |
| 114 | 141 | $offset_secs = (($tz_hour*60)+$tz_min)*60; |
| 115 | 142 | |
| 116 | 143 | # is timezone ahead of GMT? then subtract offset |