| @@ -1,18 +1,39 @@ | ||
| 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; |
| 9 | 26 | |
| 10 | - function FeedTime ($time) { | |
| 27 | + function __construct( $time ) { | |
| 11 | 28 | $this->set($time); |
| 12 | 29 | } /* FeedTime constructor */ |
| 13 | 30 | |
| 14 | - function set ($time) { | |
| 31 | + function FeedTime( $time ) { | |
| 32 | + self::__construct( $time ); | |
| 33 | + } | |
| 34 | + | |
| 35 | + function set ($time, $recurse = false) { | |
| 15 | 36 | $this->rep = $time; |
| 16 | 37 | $this->ts = NULL; |
| 17 | 38 | if (is_numeric($time)) : // Presumably a Unix-epoch timestamp |
| 18 | 39 | $this->ts = $this->rep; |
| @@ -20,12 +41,12 @@ | ||
| 20 | 41 | // First, try to parse it as a W3C date-time |
| 21 | 42 | $this->ts = $this->parse_w3cdtf(); |
| 22 | 43 | |
| 23 | 44 | 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. | |
| 45 | + // In some versions of PHP, strtotime() does not | |
| 46 | + // support the UT timezone. But since UT is by | |
| 47 | + // definition within 1 second of UTC, we'll just | |
| 48 | + // convert it preemptively to avoid problems. | |
| 28 | 49 | $time = preg_replace( |
| 29 | 50 | '/(\s)UT$/', |
| 30 | 51 | '$1UTC', |
| 31 | 52 | $time |
| @@ -30,8 +51,14 @@ | ||
| 30 | 51 | '$1UTC', |
| 31 | 52 | $time |
| 32 | 53 | ); |
| 33 | 54 | $this->ts = strtotime($time); |
| 55 | + | |
| 56 | + if ($this->failed() | |
| 57 | + and preg_match('/^(.*)([+\-][0-9]+|\s+[A-Za-z]{1,3})$/', $time, $matches)) : | |
| 58 | + // Try dropping the time zone and recurse | |
| 59 | + $this->set($matches[1], /*recurse=*/ true); | |
| 60 | + endif; | |
| 34 | 61 | endif; |
| 35 | 62 | endif; |
| 36 | 63 | } /* FeedTime::set() */ |
| 37 | 64 | |
| @@ -36,9 +63,9 @@ | ||
| 36 | 63 | } /* FeedTime::set() */ |
| 37 | 64 | |
| 38 | 65 | function timestamp () { |
| 39 | 66 | $unix = NULL; |
| 40 | - if (!$this->failed()) : | |
| 67 | + if ( ! $this->failed()) : | |
| 41 | 68 | $unix = $this->ts; |
| 42 | 69 | endif; |
| 43 | 70 | return $unix; |
| 44 | 71 | } /* FeedTime::timestamp() */ |
| @@ -43,9 +70,9 @@ | ||
| 43 | 70 | return $unix; |
| 44 | 71 | } /* FeedTime::timestamp() */ |
| 45 | 72 | |
| 46 | 73 | function failed () { |
| 47 | - return (!is_numeric($this->ts) or !$this->ts or ($this->ts <= 0)); | |
| 74 | + return ( !is_numeric($this->ts) or ! $this->ts or ($this->ts <= 0)); | |
| 48 | 75 | } /* FeedTime::failed() */ |
| 49 | 76 | |
| 50 | 77 | /** |
| 51 | 78 | * FeedTime::parse_w3cdtf() parses a W3C date-time format date into a |
| @@ -102,15 +129,19 @@ | ||
| 102 | 129 | $offset = 0; |
| 103 | 130 | if ( isset($match[15]) and $match[15] == 'Z' ) : |
| 104 | 131 | # zulu time, aka GMT |
| 105 | 132 | else : |
| 106 | - $tz_mod = $match[12]; | |
| 107 | - $tz_hour = $match[13]; | |
| 108 | - $tz_min = $match[14]; | |
| 133 | + $tz_mod = (isset($match[12]) ? $match[12] : NULL); | |
| 134 | + $tz_hour = (isset($match[13]) ? $match[13] : NULL); | |
| 135 | + $tz_min = (isset($match[14]) ? $match[14] : NULL); | |
| 109 | 136 | |
| 110 | 137 | # zero out the variables |
| 111 | - if ( ! $tz_hour ) { $tz_hour = 0; } | |
| 112 | - if ( ! $tz_min ) { $tz_min = 0; } | |
| 138 | + if ( is_null($tz_hour) ) : | |
| 139 | + $offset = (int) get_option('gmt_offset'); | |
| 140 | + $tz_hour = abs($offset); | |
| 141 | + $tz_mod = ((abs($offset) != $offset) ? '-' : '+'); | |
| 142 | + endif; | |
| 143 | + if ( is_null($tz_min) ) : $tz_min = 0; endif; | |
| 113 | 144 | |
| 114 | 145 | $offset_secs = (($tz_hour*60)+$tz_min)*60; |
| 115 | 146 | |
| 116 | 147 | # is timezone ahead of GMT? then subtract offset |