| 1 |
<?php |
| 2 |
/** |
| 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 |
* |
| 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 |
*/ |
| 23 |
class FeedTime { |
| 24 |
var $rep; |
| 25 |
var $ts; |
| 26 |
|
| 27 |
function FeedTime ($time) { |
| 28 |
$this->set($time); |
| 29 |
} /* FeedTime constructor */ |
| 30 |
|
| 31 |
function set ($time, $recurse = false) { |
| 32 |
$this->rep = $time; |
| 33 |
$this->ts = NULL; |
| 34 |
if (is_numeric($time)) : // Presumably a Unix-epoch timestamp |
| 35 |
$this->ts = $this->rep; |
| 36 |
elseif (is_string($time)) : |
| 37 |
// First, try to parse it as a W3C date-time |
| 38 |
$this->ts = $this->parse_w3cdtf(); |
| 39 |
|
| 40 |
if ($this->failed()) : |
| 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. |
| 45 |
$time = preg_replace( |
| 46 |
'/(\s)UT$/', |
| 47 |
'$1UTC', |
| 48 |
$time |
| 49 |
); |
| 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; |
| 57 |
endif; |
| 58 |
endif; |
| 59 |
} /* FeedTime::set() */ |
| 60 |
|
| 61 |
function timestamp () { |
| 62 |
$unix = NULL; |
| 63 |
if (!$this->failed()) : |
| 64 |
$unix = $this->ts; |
| 65 |
endif; |
| 66 |
return $unix; |
| 67 |
} /* FeedTime::timestamp() */ |
| 68 |
|
| 69 |
function failed () { |
| 70 |
return (!is_numeric($this->ts) or !$this->ts or ($this->ts <= 0)); |
| 71 |
} /* FeedTime::failed() */ |
| 72 |
|
| 73 |
/** |
| 74 |
* FeedTime::parse_w3cdtf() parses a W3C date-time format date into a |
| 75 |
* Unix epoch timestamp. Derived from the parse_w3cdtf function included |
| 76 |
* with MagpieRSS by Kellan Elliot-McCrea <kellan@protest.net>, with |
| 77 |
* modifications and bugfixes by Charles Johnson |
| 78 |
* <technophilia@radgeek.com>, under the terms of the GPL. |
| 79 |
*/ |
| 80 |
function parse_w3cdtf () { |
| 81 |
$unix = NULL; // Failure |
| 82 |
|
| 83 |
# regex to match wc3dtf |
| 84 |
$pat = "/^\s* |
| 85 |
(\d{4}) |
| 86 |
(- |
| 87 |
(\d{2}) |
| 88 |
(- |
| 89 |
(\d{2}) |
| 90 |
(T |
| 91 |
(\d{2}) |
| 92 |
:(\d{2}) |
| 93 |
(: |
| 94 |
(\d{2}) |
| 95 |
(\.\d+)? |
| 96 |
)? |
| 97 |
(?:([-+])(\d{2}):?(\d{2})|(Z))? |
| 98 |
)? |
| 99 |
)? |
| 100 |
)? |
| 101 |
\s*\$ |
| 102 |
/x"; |
| 103 |
|
| 104 |
if ( preg_match( $pat, $this->rep, $match ) ) : |
| 105 |
$year = (isset($match[1]) ? $match[1] : NULL); |
| 106 |
$month = (isset($match[3]) ? $match[3] : NULL); |
| 107 |
$day = (isset($match[5]) ? $match[5] : NULL); |
| 108 |
$hours = (isset($match[7]) ? $match[7] : NULL); |
| 109 |
$minutes = (isset($match[8]) ? $match[8] : NULL); |
| 110 |
$seconds = (isset($match[10]) ? $match[10] : NULL); |
| 111 |
|
| 112 |
# W3C dates can omit the time, the day of the month, or even the month. |
| 113 |
# Fill in any blanks using information from the present moment. --CWJ |
| 114 |
$default['hr'] = (int) gmdate('H'); |
| 115 |
$default['day'] = (int) gmdate('d'); |
| 116 |
$default['month'] = (int) gmdate('m'); |
| 117 |
|
| 118 |
if (is_null($hours)) : $hours = $default['hr']; $minutes = 0; $seconds = 0; endif; |
| 119 |
if (is_null($day)) : $day = $default['day']; endif; |
| 120 |
if (is_null($month)) : $month = $default['month']; endif; |
| 121 |
|
| 122 |
# calc epoch for current date assuming GMT |
| 123 |
$unix = gmmktime( $hours, $minutes, $seconds, $month, $day, $year); |
| 124 |
|
| 125 |
$offset = 0; |
| 126 |
if ( isset($match[15]) and $match[15] == 'Z' ) : |
| 127 |
# zulu time, aka GMT |
| 128 |
else : |
| 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); |
| 132 |
|
| 133 |
# zero out the variables |
| 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; |
| 140 |
|
| 141 |
$offset_secs = (($tz_hour*60)+$tz_min)*60; |
| 142 |
|
| 143 |
# is timezone ahead of GMT? then subtract offset |
| 144 |
if ( $tz_mod == '+' ) : |
| 145 |
$offset_secs = $offset_secs * -1; |
| 146 |
endif; |
| 147 |
$offset = $offset_secs; |
| 148 |
endif; |
| 149 |
$unix = $unix + $offset; |
| 150 |
endif; |
| 151 |
return $unix; |
| 152 |
} /* FeedTime::parse_w3cdtf () */ |
| 153 |
} /* class FeedTime */ |
| 154 |
|