ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
Datetime.php
Go to the documentation of this file.
1 <?php
2 
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
4 
5 // LICENSE AGREEMENT. If folded, press za here to unfold and read license {{{
6 
40 // }}}
41 
42 // dependencies {{{
43 require_once 'XML/RPC2/Exception.php';
44 require_once 'XML/RPC2/Backend/Php/Value/Scalar.php';
45 // }}}
46 
66 {
67 
68  // {{{ constructor
69 
79  public function __construct($nativeValue)
80  {
81  if ((!is_int($nativeValue)) and (!is_float($nativeValue)) and (!is_string($nativeValue)) and (!is_object($nativeValue))) {
82  throw new XML_RPC2_InvalidTypeException(sprintf('Cannot create XML_RPC2_Backend_Php_Value_Datetime from type \'%s\'.', gettype($nativeValue)));
83  }
84  if ((is_object($nativeValue)) &&(strtolower(get_class($nativeValue)) == 'stdclass') && (isset($nativeValue->xmlrpc_type))) {
85  $scalar = $nativeValue->scalar;
86  $timestamp = $nativeValue->timestamp;
87  } else {
88  if ((is_int($nativeValue)) or (is_float($nativeValue))) {
90  $timestamp = (int) $nativeValue;
91  } elseif (is_string($nativeValue)) {
92  $scalar= $nativeValue;
94  } else {
95  throw new XML_RPC2_InvalidTypeException(sprintf('Cannot create XML_RPC2_Backend_Php_Value_Datetime from type \'%s\'.', gettype($nativeValue)));
96  }
97  }
98  $tmp = new stdclass();
99  $tmp->scalar = $scalar;
100  $tmp->timestamp = $timestamp;
101  $tmp->xmlrpc_type = 'datetime';
102  $this->setNativeValue($tmp);
103  }
104 
105  // }}}
106  // {{{ _iso8601ToTimestamp()
107 
114  private static function _iso8601ToTimestamp($datetime)
115  {
116  if (!preg_match('/([0-9]{4})(-?([0-9]{2})(-?([0-9]{2})(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?/', $datetime, $matches)) {
117  throw new XML_RPC2_InvalidDateFormatException(sprintf('Provided date \'%s\' is not ISO-8601.', $datetime));
118  }
119  $year = $matches[1];
120  $month = array_key_exists(3, $matches) ? $matches[3] : 1;
121  $day = array_key_exists(5, $matches) ? $matches[5] : 1;
122  $hour = array_key_exists(7, $matches) ? $matches[7] : 0;
123  $minutes = array_key_exists(8, $matches) ? $matches[8] : 0;
124  $seconds = array_key_exists(10, $matches) ? $matches[10] : 0;
125  $milliseconds = array_key_exists(12, $matches) ? ((double) ('0.' . $matches[12])) : 0;
126  if (array_key_exists(13, $matches)) {
127  if ($matches[13] == 'Z') {
128  $tzSeconds = 0;
129  } else {
130  $tmp = ($matches[15] == '-') ? -1 : 1;
131  $tzSeconds = $tmp * (((int) $matches[16]) * 3600 + ((int) $matches[17]) * 60);
132  }
133  } else {
134  $tzSeconds = 0;
135  }
136  $result = ((double) @mktime($hour, $minutes, $seconds, $month, $day, $year, 0)) +
137  ((double) $milliseconds) -
138  ((double) $tzSeconds);
139  if ($milliseconds==0) return ((int) $result);
140  return $result;
141  }
142 
143  // }}}
144  // {{{ _timestampToIso8601()
145 
152  private static function _timestampToIso8601($timestamp)
153  {
154  return strftime('%Y%m%dT%H:%M:%S', (int) $timestamp);
155  }
156 
157  // }}}
158  // {{{ decode()
159 
165  public static function decode($xml)
166  {
167  // TODO Remove reparsing of XML fragment, when SimpleXML proves more solid. Currently it segfaults when
168  // xpath is used both in an element and in one of its children
169  $xml = simplexml_load_string($xml->asXML());
170  $value = $xml->xpath('/value/dateTime.iso8601/text()');
171  if (!array_key_exists(0, $value)) {
172  $value = $xml->xpath('/value/text()');
173  }
174  // Emulate xmlrpcext results (to be able to switch from a backend to another)
175  $result = new stdclass();
176  $result->scalar = (string) $value[0];
177  $result->timestamp = (int) XML_RPC2_Backend_Php_Value_Datetime::_iso8601ToTimestamp((string) $value[0]);
178  $result->xmlrpc_type = 'datetime';
179  return $result;
180  }
181 
182  // }}}
183  // {{{ encode()
184 
190  public function encode()
191  {
192  $native = $this->getNativeValue();
193  return '<dateTime.iso8601>' . $native->scalar . '</dateTime.iso8601>';
194  }
195 
196  // }}}
197 
198 }
199 
200 ?>