| @@ -1,38 +1,17 @@ | ||
| 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 | 14 | function set ($time, $recurse = false) { |
| 36 | 15 | $this->rep = $time; |
| 37 | 16 | $this->ts = NULL; |
| 38 | 17 | if (is_numeric($time)) : // Presumably a Unix-epoch timestamp |
| @@ -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 preemptively to avoid | |
| 27 | + // problems. | |
| 49 | 28 | $time = preg_replace( |
| 50 | 29 | '/(\s)UT$/', |
| 51 | 30 | '$1UTC', |
| 52 | 31 | $time |
| @@ -63,9 +42,9 @@ | ||
| 63 | 42 | } /* FeedTime::set() */ |
| 64 | 43 | |
| 65 | 44 | function timestamp () { |
| 66 | 45 | $unix = NULL; |
| 67 | - if ( ! $this->failed()) : | |
| 46 | + if (!$this->failed()) : | |
| 68 | 47 | $unix = $this->ts; |
| 69 | 48 | endif; |
| 70 | 49 | return $unix; |
| 71 | 50 | } /* FeedTime::timestamp() */ |
| @@ -70,9 +49,9 @@ | ||
| 70 | 49 | return $unix; |
| 71 | 50 | } /* FeedTime::timestamp() */ |
| 72 | 51 | |
| 73 | 52 | function failed () { |
| 74 | - return ( !is_numeric($this->ts) or ! $this->ts or ($this->ts <= 0)); | |
| 53 | + return (!is_numeric($this->ts) or !$this->ts or ($this->ts <= 0)); | |
| 75 | 54 | } /* FeedTime::failed() */ |
| 76 | 55 | |
| 77 | 56 | /** |
| 78 | 57 | * FeedTime::parse_w3cdtf() parses a W3C date-time format date into a |