PluginProbe
FeedWordPress / 2011.0512
FeedWordPress v2011.0512
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
feedwordpress / feedtime.class.php

feedtime.class.php in FeedWordPress 2011.0512, at feedtime.class.php

137 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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, $recurse = false) {
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 preemptively to avoid
27 // problems.
28 $time = preg_replace(
29 '/(\s)UT$/',
30 '$1UTC',
31 $time
32 );
33 $this->ts = strtotime($time);
34
35 if ($this->failed()
36 and preg_match('/^(.*)([+\-][0-9]+|\s+[A-Za-z]{1,3})$/', $time, $matches)) :
37 // Try dropping the time zone and recurse
38 $this->set($matches[1], /*recurse=*/ true);
39 endif;
40 endif;
41 endif;
42 } /* FeedTime::set() */
43
44 function timestamp () {
45 $unix = NULL;
46 if (!$this->failed()) :
47 $unix = $this->ts;
48 endif;
49 return $unix;
50 } /* FeedTime::timestamp() */
51
52 function failed () {
53 return (!is_numeric($this->ts) or !$this->ts or ($this->ts <= 0));
54 } /* FeedTime::failed() */
55
56 /**
57 * FeedTime::parse_w3cdtf() parses a W3C date-time format date into a
58 * Unix epoch timestamp. Derived from the parse_w3cdtf function included
59 * with MagpieRSS by Kellan Elliot-McCrea <kellan@protest.net>, with
60 * modifications and bugfixes by Charles Johnson
61 * <technophilia@radgeek.com>, under the terms of the GPL.
62 */
63 function parse_w3cdtf () {
64 $unix = NULL; // Failure
65
66 # regex to match wc3dtf
67 $pat = "/^\s*
68 (\d{4})
69 (-
70 (\d{2})
71 (-
72 (\d{2})
73 (T
74 (\d{2})
75 :(\d{2})
76 (:
77 (\d{2})
78 (\.\d+)?
79 )?
80 (?:([-+])(\d{2}):?(\d{2})|(Z))?
81 )?
82 )?
83 )?
84 \s*\$
85 /x";
86
87 if ( preg_match( $pat, $this->rep, $match ) ) :
88 $year = (isset($match[1]) ? $match[1] : NULL);
89 $month = (isset($match[3]) ? $match[3] : NULL);
90 $day = (isset($match[5]) ? $match[5] : NULL);
91 $hours = (isset($match[7]) ? $match[7] : NULL);
92 $minutes = (isset($match[8]) ? $match[8] : NULL);
93 $seconds = (isset($match[10]) ? $match[10] : NULL);
94
95 # W3C dates can omit the time, the day of the month, or even the month.
96 # Fill in any blanks using information from the present moment. --CWJ
97 $default['hr'] = (int) gmdate('H');
98 $default['day'] = (int) gmdate('d');
99 $default['month'] = (int) gmdate('m');
100
101 if (is_null($hours)) : $hours = $default['hr']; $minutes = 0; $seconds = 0; endif;
102 if (is_null($day)) : $day = $default['day']; endif;
103 if (is_null($month)) : $month = $default['month']; endif;
104
105 # calc epoch for current date assuming GMT
106 $unix = gmmktime( $hours, $minutes, $seconds, $month, $day, $year);
107
108 $offset = 0;
109 if ( isset($match[15]) and $match[15] == 'Z' ) :
110 # zulu time, aka GMT
111 else :
112 $tz_mod = (isset($match[12]) ? $match[12] : NULL);
113 $tz_hour = (isset($match[13]) ? $match[13] : NULL);
114 $tz_min = (isset($match[14]) ? $match[14] : NULL);
115
116 # zero out the variables
117 if ( is_null($tz_hour) ) :
118 $offset = (int) get_option('gmt_offset');
119 $tz_hour = abs($offset);
120 $tz_mod = ((abs($offset) != $offset) ? '-' : '+');
121 endif;
122 if ( is_null($tz_min) ) : $tz_min = 0; endif;
123
124 $offset_secs = (($tz_hour*60)+$tz_min)*60;
125
126 # is timezone ahead of GMT? then subtract offset
127 if ( $tz_mod == '+' ) :
128 $offset_secs = $offset_secs * -1;
129 endif;
130 $offset = $offset_secs;
131 endif;
132 $unix = $unix + $offset;
133 endif;
134 return $unix;
135 } /* FeedTime::parse_w3cdtf () */
136 } /* class FeedTime */
137