Source for file ISO8601StringParser.class.php

Documentation is available at ISO8601StringParser.class.php

  1. <?php
  2. /**
  3. * @since 5/23/05
  4. * @package harmoni.primitives.chronology.string_parsers
  5. *
  6. * @copyright Copyright &copy; 2005, Middlebury College
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  8. *
  9. * @version $Id: ISO8601StringParser.class.php,v 1.5 2007/02/26 14:47:44 adamfranco Exp $
  10. *
  11. * @link http://harmoni.sourceforge.net/
  12. * @author Adam Franco <adam AT adamfranco DOT com> <afranco AT middlebury DOT edu>
  13. */
  14. require_once(dirname(__FILE__)."/StringParser.class.php");
  15. //require_once(dirname(__FILE__)."/RegexStringParser.class.php");
  16.  
  17. /**
  18. * This StringParser can handle ISO 8601 dates. {@link http://www.cl.cam.ac.uk/~mgk25/iso-time.html}
  19. * Examples:
  20. * - 1982-04-05T15:25:21+5:00
  21. *
  22. * @since 5/23/05
  23. * @package harmoni.primitives.chronology.string_parsers
  24. *
  25. * @copyright Copyright &copy; 2005, Middlebury College
  26. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  27. *
  28. * @version $Id: ISO8601StringParser.class.php,v 1.5 2007/02/26 14:47:44 adamfranco Exp $
  29. *
  30. * @link http://harmoni.sourceforge.net/
  31. * @author Adam Franco <adam AT adamfranco DOT com> <afranco AT middlebury DOT edu>
  32. */
  33. class ISO8601StringParser
  34. extends RegexStringParser {
  35. /*******************************************************
  36. * Instance Methods
  37. *********************************************************/
  38.  
  39. /**
  40. * Return the regular expression used by this parser
  41. *
  42. * @return string
  43. * @access protected
  44. * @since 5/24/05
  45. */
  46. function getRegex () {
  47. return
  48. "/
  49. ^ # Start of the line
  50.  
  51. #-----------------------------------------------------------------------------
  52. (?: # The date component
  53. ([0-9]{4}) # Four-digit year
  54. [\-\/:]? # Optional Hyphen, slash, or colon delimiter
  55. (?: # Two-digit month
  56. (
  57. (?: 0[1-9])
  58. |
  59. (?: 1[0-2])
  60. )
  61. [\-\/:]? # Optional Hyphen, slash, or colon delimiter
  62. (?: # Two-digit day
  63. (
  64. (?: 0[1-9])
  65. |
  66. (?: (?: 1|2)[0-9])
  67. |
  68. (?: 3[0-1])
  69. )
  70. [\sT]? # Optional delimiter
  71. #-----------------------------------------------------------------------------
  72. (?: # The time component
  73. ( # Two-digit hour
  74. (?: [0-1][0-9])
  75. |
  76. (?: 2[0-4])
  77. )
  78. (?:
  79. :? # Optional Colon
  80. ([0-5][0-9])? # Two-digit minute
  81. (?:
  82. :? # Optional Colon
  83. ( # Two-digit second
  84. [0-5][0-9]
  85. (?: \.[0-9]+)? # followed by an optional decimal.
  86. )?
  87. #-----------------------------------------------------------------------------
  88. ( # Offset component
  89. Z # Zero offset (UTC)
  90. | # OR
  91. (?: # Offset from UTC
  92. ([+\-]) # Sign of the offset
  93. ( # Two-digit offset hour
  94. (?: [0-1][0-9])
  95. |
  96. (?: 2[0-4])
  97. )
  98. :? # Optional Colon
  99. ([0-5][0-9])? # Two-digit offset minute
  100. )
  101. )?
  102. )?
  103. )?
  104. )?
  105. )?
  106. )?
  107. )?
  108.  
  109. $
  110. /x";
  111. }
  112. /**
  113. * Parse the input string and set our elements based on the contents of the
  114. * input string. Elements not found in the string will be null.
  115. *
  116. * @return void
  117. * @access private
  118. * @since 5/23/05
  119. */
  120. function parse () {
  121. preg_match($this->getRegex(), $this->input, $matches);
  122. // Matches:
  123. // [0] => 2005-05-23T15:25:10-04:00
  124. // [1] => 2005
  125. // [2] => 05
  126. // [3] => 23
  127. // [4] => 15
  128. // [5] => 25
  129. // [6] => 10
  130. // [7] => -04:00
  131. // [8] => -
  132. // [9] => 04
  133. // [10] => 00
  134.  
  135. if (isset($matches[1]))
  136. $this->setYear($matches[1]);
  137. if (isset($matches[2]))
  138. $this->setMonth($matches[2]);
  139. if (isset($matches[3]))
  140. $this->setDay($matches[3]);
  141. if (isset($matches[4]))
  142. $this->setHour($matches[4]);
  143. if (isset($matches[5]))
  144. $this->setMinute($matches[5]);
  145. if (isset($matches[6]))
  146. $this->setSecond($matches[6]);
  147. if (isset($matches[7]) && $matches[7] == 'Z') {
  148. $this->setOffsetHour(0);
  149. $this->setOffsetMinute(0);
  150. } else if (isset($matches[7])) {
  151. $sign = $matches[8];
  152. $hour = $matches[9];
  153. if (isset($matches[10]))
  154. $minute = $matches[10];
  155. else
  156. $minute = 0;
  157. $this->setOffsetHour(intval($sign.$hour));
  158. $this->setOffsetMinute(intval($sign.$minute));
  159. }
  160. }
  161. }
  162.  
  163. ?>

Documentation generated on Wed, 19 Sep 2007 10:24:42 -0400 by phpDocumentor 1.3.0RC3