1 | <?php
|
---|
2 | /**
|
---|
3 | * This file contains the code for the SOAP date/time clas.
|
---|
4 | *
|
---|
5 | * PHP versions 4 and 5
|
---|
6 | *
|
---|
7 | * LICENSE: This source file is subject to version 2.02 of the PHP license,
|
---|
8 | * that is bundled with this package in the file LICENSE, and is available at
|
---|
9 | * through the world-wide-web at http://www.php.net/license/2_02.txt. If you
|
---|
10 | * did not receive a copy of the PHP license and are unable to obtain it
|
---|
11 | * through the world-wide-web, please send a note to license@php.net so we can
|
---|
12 | * mail you a copy immediately.
|
---|
13 | *
|
---|
14 | * @category Web Services
|
---|
15 | * @package SOAP
|
---|
16 | * @author Dietrich Ayala <dietrich@ganx4.com> Original Author
|
---|
17 | * @author Shane Caraveo <Shane@Caraveo.com> Port to PEAR and more
|
---|
18 | * @author Jan Schneider <jan@horde.org> Maintenance
|
---|
19 | * @copyright 2003-2005 The PHP Group
|
---|
20 | * @license http://www.php.net/license/2_02.txt PHP License 2.02
|
---|
21 | * @link http://pear.php.net/package/SOAP
|
---|
22 | */
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * This class converts from and to unix timestamps and ISO 8601 date/time.
|
---|
26 | *
|
---|
27 | * @access public
|
---|
28 | * @package SOAP
|
---|
29 | * @author Dietrich Ayala <dietrich@ganx4.com> Original Author
|
---|
30 | * @author Shane Caraveo <shane@php.net> Port to PEAR and more
|
---|
31 | * @author Jan Schneider <jan@horde.org> Maintenance
|
---|
32 | */
|
---|
33 | class SOAP_Type_dateTime
|
---|
34 | {
|
---|
35 | var $_iso8601 =
|
---|
36 | '# 1: centuries & years CCYY-
|
---|
37 | (-?[0-9]{4})-
|
---|
38 | # 2: months MM-
|
---|
39 | ([0-9]{2})-
|
---|
40 | # 3: days DD
|
---|
41 | ([0-9]{2})
|
---|
42 | # 4: separator T
|
---|
43 | T
|
---|
44 | # 5: hours hh:
|
---|
45 | ([0-9]{2}):
|
---|
46 | # 6: minutes mm:
|
---|
47 | ([0-9]{2}):
|
---|
48 | # 7: seconds ss.ss...
|
---|
49 | ([0-9]{2})(\.[0-9]*)?
|
---|
50 | # 8: Z to indicate UTC, -+HH:MM:SS.SS... for local zones
|
---|
51 | (Z|[+\-][0-9]{4}|[+\-][0-9]{2}:[0-9]{2})?';
|
---|
52 |
|
---|
53 | var $timestamp = -1;
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Constructor.
|
---|
57 | *
|
---|
58 | * @param string|integer $date The timestamp or ISO 8601 formatted
|
---|
59 | * date and time this object is going to
|
---|
60 | * represent.
|
---|
61 | */
|
---|
62 | function SOAP_Type_dateTime($date = -1)
|
---|
63 | {
|
---|
64 | if ($date == -1) {
|
---|
65 | $this->timestamp = time();
|
---|
66 | } elseif (is_int($date)) {
|
---|
67 | $this->timestamp = $date;
|
---|
68 | } else {
|
---|
69 | $this->timestamp = $this->toUnixtime($date);
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Alias of {@link SOAP_Type_dateTime::toUTC}.
|
---|
75 | */
|
---|
76 | function toSOAP($date = NULL)
|
---|
77 | {
|
---|
78 | return $this->toUTC($date);
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Converts this object or a timestamp to an ISO 8601 date/time string.
|
---|
83 | *
|
---|
84 | * @param integer $timestamp A unix timestamp
|
---|
85 | *
|
---|
86 | * @return string An ISO 8601 formatted date/time string.
|
---|
87 | */
|
---|
88 | function toString($timestamp = 0)
|
---|
89 | {
|
---|
90 | if (!$timestamp) {
|
---|
91 | $timestamp = $this->timestamp;
|
---|
92 | }
|
---|
93 | if ($timestamp < 0) {
|
---|
94 | return 0;
|
---|
95 | }
|
---|
96 |
|
---|
97 | //simulate PHP5's P parameter
|
---|
98 | $zone = date('O', $timestamp);
|
---|
99 | if (strlen($zone) == 5) {
|
---|
100 | $zone = substr($zone, 0, 3) . ':' . substr($zone, 3);
|
---|
101 | }
|
---|
102 | return date('Y-m-d\TH:i:s', $timestamp) . $zone;
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Splits a date/time into its components.
|
---|
107 | *
|
---|
108 | * @param string|integer $datestr A unix timestamp or ISO 8601 date/time
|
---|
109 | * string. If empty, this object is used.
|
---|
110 | *
|
---|
111 | * @return boolean|array An array with the date and time components or
|
---|
112 | * false on failure.
|
---|
113 | */
|
---|
114 | function _split($datestr)
|
---|
115 | {
|
---|
116 | if (!$datestr) {
|
---|
117 | $datestr = $this->toString();
|
---|
118 | } elseif (is_int($datestr)) {
|
---|
119 | $datestr = $this->toString($datestr);
|
---|
120 | }
|
---|
121 |
|
---|
122 | if (preg_match('/' . $this->_iso8601 . '/x', $datestr, $regs)) {
|
---|
123 | if (empty($regs[8])) {
|
---|
124 | $timestamp = strtotime(sprintf('%04d-%02d-%02d %02d:%02d:%02d',
|
---|
125 | $regs[1],
|
---|
126 | $regs[2],
|
---|
127 | $regs[3],
|
---|
128 | $regs[4],
|
---|
129 | $regs[5],
|
---|
130 | $regs[6]));
|
---|
131 | $regs[8] = date('O', $timestamp);
|
---|
132 | }
|
---|
133 | if ($regs[8] != 'Z') {
|
---|
134 | $op = substr($regs[8], 0, 1);
|
---|
135 | $h = substr($regs[8], 1, 2);
|
---|
136 | if (strstr($regs[8], ':')) {
|
---|
137 | $m = substr($regs[8], 4, 2);
|
---|
138 | } else {
|
---|
139 | $m = substr($regs[8], 3, 2);
|
---|
140 | }
|
---|
141 | if ($op == '+') {
|
---|
142 | $regs[4] = $regs[4] - $h;
|
---|
143 | if ($regs[4] < 0) {
|
---|
144 | $regs[4] += 24;
|
---|
145 | }
|
---|
146 | $regs[5] = $regs[5] - $m;
|
---|
147 | if ($regs[5] < 0) {
|
---|
148 | $regs[5] += 60;
|
---|
149 | }
|
---|
150 | } else {
|
---|
151 | $regs[4] = $regs[4] + $h;
|
---|
152 | if ($regs[4] > 23) {
|
---|
153 | $regs[4] -= 24;
|
---|
154 | }
|
---|
155 | $regs[5] = $regs[5] + $m;
|
---|
156 | if ($regs[5] > 59) {
|
---|
157 | $regs[5] -= 60;
|
---|
158 | }
|
---|
159 | }
|
---|
160 | }
|
---|
161 | return $regs;
|
---|
162 | }
|
---|
163 |
|
---|
164 | return false;
|
---|
165 | }
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Returns an ISO 8601 formatted UTC date/time string.
|
---|
169 | *
|
---|
170 | * @param string|integer $datestr @see SOAP_Type_dateTime::_split
|
---|
171 | *
|
---|
172 | * @return string The ISO 8601 formatted UTC date/time string.
|
---|
173 | */
|
---|
174 | function toUTC($datestr = null)
|
---|
175 | {
|
---|
176 | $regs = $this->_split($datestr);
|
---|
177 |
|
---|
178 | if ($regs) {
|
---|
179 | return sprintf('%04d-%02d-%02dT%02d:%02d:%02dZ',
|
---|
180 | $regs[1],
|
---|
181 | $regs[2],
|
---|
182 | $regs[3],
|
---|
183 | $regs[4],
|
---|
184 | $regs[5],
|
---|
185 | $regs[6]);
|
---|
186 | }
|
---|
187 |
|
---|
188 | return '';
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Returns a unix timestamp.
|
---|
193 | *
|
---|
194 | * @param string|integer $datestr @see SOAP_Type_dateTime::_split
|
---|
195 | *
|
---|
196 | * @return integer The unix timestamp.
|
---|
197 | */
|
---|
198 | function toUnixtime($datestr = null)
|
---|
199 | {
|
---|
200 | $regs = $this->_split($datestr);
|
---|
201 | if ($regs) {
|
---|
202 | return strtotime(sprintf('%04d-%02d-%02d %02d:%02d:%02dZ',
|
---|
203 | $regs[1],
|
---|
204 | $regs[2],
|
---|
205 | $regs[3],
|
---|
206 | $regs[4],
|
---|
207 | $regs[5],
|
---|
208 | $regs[6]));
|
---|
209 | }
|
---|
210 | return -1;
|
---|
211 | }
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * Compares two dates or this object with a second date.
|
---|
215 | *
|
---|
216 | * @param string|integer $date1 A unix timestamp or ISO 8601 date/time
|
---|
217 | * string.
|
---|
218 | * @param string|integer $date2 A unix timestamp or ISO 8601 date/time
|
---|
219 | * string. If empty, this object is used.
|
---|
220 | *
|
---|
221 | * @return integer The difference between the first and the second date.
|
---|
222 | */
|
---|
223 | function compare($date1, $date2 = null)
|
---|
224 | {
|
---|
225 | if (is_null($date2)) {
|
---|
226 | $date2 = $date1;
|
---|
227 | $date1 = $this->timestamp;
|
---|
228 | }
|
---|
229 | if (!is_int($date1)) {
|
---|
230 | $date1 = $this->toUnixtime($date1);
|
---|
231 | }
|
---|
232 | if (!is_int($date2)) {
|
---|
233 | $date2 = $this->toUnixtime($date2);
|
---|
234 | }
|
---|
235 |
|
---|
236 | if ($date1 != -1 && $date2 != -1) {
|
---|
237 | return $date1 - $date2;
|
---|
238 | }
|
---|
239 |
|
---|
240 | return -1;
|
---|
241 | }
|
---|
242 |
|
---|
243 | }
|
---|