| @@ -1,39 +1,18 @@ | ||
| 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. | |
| 8 | 4 | * |
| 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; | |
| 22 | 5 | */ |
| 23 | 6 | class FeedTime { |
| 24 | 7 | var $rep; |
| 25 | 8 | var $ts; |
| 26 | 9 | |
| 27 | - function __construct( $time ) { | |
| 10 | + function FeedTime ($time) { | |
| 28 | 11 | $this->set($time); |
| 29 | 12 | } /* FeedTime constructor */ |
| 30 | 13 | |
| 31 | - function FeedTime( $time ) { | |
| 32 | - self::__construct( $time ); | |
| 33 | - } | |
| 34 | - | |
| 35 | - function set ($time, $recurse = false) { | |
| 14 | + function set ($time) { | |
| 36 | 15 | $this->rep = $time; |
| 37 | 16 | $this->ts = NULL; |
| 38 | 17 | if (is_numeric($time)) : // Presumably a Unix-epoch timestamp |
| 39 | 18 | $this->ts = $this->rep; |
| @@ -41,12 +20,12 @@ | ||
| 41 | 20 | // First, try to parse it as a W3C date-time |
| 42 | 21 | $this->ts = $this->parse_w3cdtf(); |
| 43 | 22 | |
| 44 | 23 | if ($this->failed()) : |
| 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. | |
| 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. | |
| 49 | 28 | $time = preg_replace( |
| 50 | 29 | '/(\s)UT$/', |
| 51 | 30 | '$1UTC', |
| 52 | 31 | $time |
| @@ -51,14 +30,8 @@ | ||
| 51 | 30 | '$1UTC', |
| 52 | 31 | $time |
| 53 | 32 | ); |
| 54 | 33 | $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; | |
| 61 | 34 | endif; |
| 62 | 35 | endif; |
| 63 | 36 | } /* FeedTime::set() */ |
| 64 | 37 | |
| @@ -63,9 +36,9 @@ | ||
| 63 | 36 | } /* FeedTime::set() */ |
| 64 | 37 | |
| 65 | 38 | function timestamp () { |
| 66 | 39 | $unix = NULL; |
| 67 | - if ( ! $this->failed()) : | |
| 40 | + if (!$this->failed()) : | |
| 68 | 41 | $unix = $this->ts; |
| 69 | 42 | endif; |
| 70 | 43 | return $unix; |
| 71 | 44 | } /* FeedTime::timestamp() */ |
| @@ -70,9 +43,9 @@ | ||
| 70 | 43 | return $unix; |
| 71 | 44 | } /* FeedTime::timestamp() */ |
| 72 | 45 | |
| 73 | 46 | function failed () { |
| 74 | - return ( !is_numeric($this->ts) or ! $this->ts or ($this->ts <= 0)); | |
| 47 | + return (!is_numeric($this->ts) or !$this->ts or ($this->ts <= 0)); | |
| 75 | 48 | } /* FeedTime::failed() */ |
| 76 | 49 | |
| 77 | 50 | /** |
| 78 | 51 | * FeedTime::parse_w3cdtf() parses a W3C date-time format date into a |
| @@ -129,19 +102,15 @@ | ||
| 129 | 102 | $offset = 0; |
| 130 | 103 | if ( isset($match[15]) and $match[15] == 'Z' ) : |
| 131 | 104 | # zulu time, aka GMT |
| 132 | 105 | else : |
| 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); | |
| 106 | + $tz_mod = $match[12]; | |
| 107 | + $tz_hour = $match[13]; | |
| 108 | + $tz_min = $match[14]; | |
| 136 | 109 | |
| 137 | 110 | # zero out the variables |
| 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; | |
| 111 | + if ( ! $tz_hour ) { $tz_hour = 0; } | |
| 112 | + if ( ! $tz_min ) { $tz_min = 0; } | |
| 144 | 113 | |
| 145 | 114 | $offset_secs = (($tz_hour*60)+$tz_min)*60; |
| 146 | 115 | |
| 147 | 116 | # is timezone ahead of GMT? then subtract offset |