/library/PEAR/SOAP/Type/dateTime.php

https://github.com/vinnivinsachi/Vincent-DR · PHP · 238 lines · 141 code · 17 blank · 80 comment · 28 complexity · 2e8c2fa95f3d7dd520cc86e54e74775a MD5 · raw file

  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. * This class converts from and to unix timestamps and ISO 8601 date/time.
  25. *
  26. * @access public
  27. * @package SOAP
  28. * @author Dietrich Ayala <dietrich@ganx4.com> Original Author
  29. * @author Shane Caraveo <shane@php.net> Port to PEAR and more
  30. * @author Jan Schneider <jan@horde.org> Maintenance
  31. */
  32. class SOAP_Type_dateTime
  33. {
  34. var $_iso8601 =
  35. '# 1: centuries & years CCYY-
  36. (-?[0-9]{4})-
  37. # 2: months MM-
  38. ([0-9]{2})-
  39. # 3: days DD
  40. ([0-9]{2})
  41. # 4: separator T
  42. T
  43. # 5: hours hh:
  44. ([0-9]{2}):
  45. # 6: minutes mm:
  46. ([0-9]{2}):
  47. # 7: seconds ss.ss...
  48. ([0-9]{2})(\.[0-9]*)?
  49. # 8: Z to indicate UTC, -+HH:MM:SS.SS... for local zones
  50. (Z|[+\-][0-9]{4}|[+\-][0-9]{2}:[0-9]{2})?';
  51. var $timestamp = -1;
  52. /**
  53. * Constructor.
  54. *
  55. * @param string|integer $date The timestamp or ISO 8601 formatted
  56. * date and time this object is going to
  57. * represent.
  58. */
  59. function SOAP_Type_dateTime($date = -1)
  60. {
  61. if ($date == -1) {
  62. $this->timestamp = time();
  63. } elseif (is_int($date)) {
  64. $this->timestamp = $date;
  65. } else {
  66. $this->timestamp = $this->toUnixtime($date);
  67. }
  68. }
  69. /**
  70. * Alias of {@link SOAP_Type_dateTime::toUTC}.
  71. */
  72. function toSOAP($date = NULL)
  73. {
  74. return $this->toUTC($date);
  75. }
  76. /**
  77. * Converts this object or a timestamp to an ISO 8601 date/time string.
  78. *
  79. * @param integer $timestamp A unix timestamp
  80. *
  81. * @return string An ISO 8601 formatted date/time string.
  82. */
  83. function toString($timestamp = 0)
  84. {
  85. if (!$timestamp) {
  86. $timestamp = $this->timestamp;
  87. }
  88. if ($timestamp < 0) {
  89. return 0;
  90. }
  91. return date('Y-m-d\TH:i:sO', $timestamp);
  92. }
  93. /**
  94. * Splits a date/time into its components.
  95. *
  96. * @param string|integer $datestr A unix timestamp or ISO 8601 date/time
  97. * string. If empty, this object is used.
  98. *
  99. * @return boolean|array An array with the date and time components or
  100. * false on failure.
  101. */
  102. function _split($datestr)
  103. {
  104. if (!$datestr) {
  105. $datestr = $this->toString();
  106. } elseif (is_int($datestr)) {
  107. $datestr = $this->toString($datestr);
  108. }
  109. if (preg_match('/' . $this->_iso8601 . '/x', $datestr, $regs)) {
  110. if (empty($regs[8])) {
  111. $timestamp = strtotime(sprintf('%04d-%02d-%02d %02d:%02d:%02d',
  112. $regs[1],
  113. $regs[2],
  114. $regs[3],
  115. $regs[4],
  116. $regs[5],
  117. $regs[6]));
  118. $regs[8] = date('O', $timestamp);
  119. }
  120. if ($regs[8] != 'Z') {
  121. $op = substr($regs[8], 0, 1);
  122. $h = substr($regs[8], 1, 2);
  123. if (strstr($regs[8], ':')) {
  124. $m = substr($regs[8], 4, 2);
  125. } else {
  126. $m = substr($regs[8], 3, 2);
  127. }
  128. if ($op == '+') {
  129. $regs[4] = $regs[4] - $h;
  130. if ($regs[4] < 0) {
  131. $regs[4] += 24;
  132. }
  133. $regs[5] = $regs[5] - $m;
  134. if ($regs[5] < 0) {
  135. $regs[5] += 60;
  136. }
  137. } else {
  138. $regs[4] = $regs[4] + $h;
  139. if ($regs[4] > 23) {
  140. $regs[4] -= 24;
  141. }
  142. $regs[5] = $regs[5] + $m;
  143. if ($regs[5] > 59) {
  144. $regs[5] -= 60;
  145. }
  146. }
  147. }
  148. return $regs;
  149. }
  150. return false;
  151. }
  152. /**
  153. * Returns an ISO 8601 formatted UTC date/time string.
  154. *
  155. * @param string|integer $datestr @see SOAP_Type_dateTime::_split
  156. *
  157. * @return string The ISO 8601 formatted UTC date/time string.
  158. */
  159. function toUTC($datestr = null)
  160. {
  161. $regs = $this->_split($datestr);
  162. if ($regs) {
  163. return sprintf('%04d-%02d-%02dT%02d:%02d:%02dZ',
  164. $regs[1],
  165. $regs[2],
  166. $regs[3],
  167. $regs[4],
  168. $regs[5],
  169. $regs[6]);
  170. }
  171. return '';
  172. }
  173. /**
  174. * Returns a unix timestamp.
  175. *
  176. * @param string|integer $datestr @see SOAP_Type_dateTime::_split
  177. *
  178. * @return integer The unix timestamp.
  179. */
  180. function toUnixtime($datestr = null)
  181. {
  182. $regs = $this->_split($datestr);
  183. if ($regs) {
  184. return strtotime(sprintf('%04d-%02d-%02d %02d:%02d:%02dZ',
  185. $regs[1],
  186. $regs[2],
  187. $regs[3],
  188. $regs[4],
  189. $regs[5],
  190. $regs[6]));
  191. }
  192. return -1;
  193. }
  194. /**
  195. * Compares two dates or this object with a second date.
  196. *
  197. * @param string|integer $date1 A unix timestamp or ISO 8601 date/time
  198. * string.
  199. * @param string|integer $date2 A unix timestamp or ISO 8601 date/time
  200. * string. If empty, this object is used.
  201. *
  202. * @return integer The difference between the first and the second date.
  203. */
  204. function compare($date1, $date2 = null)
  205. {
  206. if (is_null($date2)) {
  207. $date2 = $date1;
  208. $date1 = $this->timestamp;
  209. }
  210. if (!is_int($date1)) {
  211. $date1 = $this->toUnixtime($date1);
  212. }
  213. if (!is_int($date2)) {
  214. $date2 = $this->toUnixtime($date2);
  215. }
  216. if ($date1 != -1 && $date2 != -1) {
  217. return $date1 - $date2;
  218. }
  219. return -1;
  220. }
  221. }