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