PluginProbe
FeedWordPress / 2017.0913
FeedWordPress v2017.0913
trunk 0.8 0.9 0.91 0.95 0.96 0.97 0.98 0.981 0.99 0.991 0.992 0.993 2008.1030 2008.1101 2008.1105 2008.1214 2009.0612 2009.0613 2009.0618 2009.0707 2009.1111 2009.1112 2010.0127 2010.0528 All 65 releases
← All changes | feedtime.class.php +42 -11 2010.05282017.0913 View file →
@@ -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
@@ -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