Source for file ISO8601TimeStringParser.class.php

Documentation is available at ISO8601TimeStringParser.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: ISO8601TimeStringParser.class.php,v 1.3 2006/06/26 12:55:08 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. * - 4/5/82
  21. * - 04/05/82
  22. * - 04/05/1982
  23. * - 4-5-82
  24. *
  25. * @since 5/23/05
  26. * @package harmoni.primitives.chronology.string_parsers
  27. *
  28. * @copyright Copyright &copy; 2005, Middlebury College
  29. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  30. *
  31. * @version $Id: ISO8601TimeStringParser.class.php,v 1.3 2006/06/26 12:55:08 adamfranco Exp $
  32. *
  33. * @link http://harmoni.sourceforge.net/
  34. * @author Adam Franco <adam AT adamfranco DOT com> <afranco AT middlebury DOT edu>
  35. */
  36. class ISO8601TimeStringParser
  37. extends RegexStringParser {
  38. /*******************************************************
  39. * Instance Methods
  40. *********************************************************/
  41.  
  42. /**
  43. * Return the regular expression used by this parser
  44. *
  45. * @return string
  46. * @access protected
  47. * @since 5/24/05
  48. */
  49. function getRegex () {
  50. return
  51. "/
  52. ^ # Start of the line
  53.  
  54. [\sT]? # Optional delimiter
  55.  
  56. #-----------------------------------------------------------------------------
  57. # The time component
  58.  
  59. ( # Two-digit hour
  60. (?: [0-1][0-9])
  61. |
  62. (?: 2[0-4])
  63. )
  64. :? # Optional Colon
  65. ([0-5][0-9])? # Two-digit minute
  66. :? # Optional Colon
  67. ( # Two-digit second
  68. [0-5][0-9]
  69. (?: \.[0-9]+)? # followed by an optional decimal.
  70. )?
  71.  
  72. #-----------------------------------------------------------------------------
  73. ( # Offset component
  74. Z # Zero offset (UTC)
  75. | # OR
  76. (?: # Offset from UTC
  77. ([+\-]) # Sign of the offset
  78. ( # Two-digit offset hour
  79. (?: [0-1][0-9])
  80. |
  81. (?: 2[0-4])
  82. )
  83.  
  84. :? # Optional Colon
  85. ([0-5][0-9])? # Two-digit offset minute
  86. )
  87. )?
  88.  
  89. $
  90. /x";
  91. }
  92. /**
  93. * Parse the input string and set our elements based on the contents of the
  94. * input string. Elements not found in the string will be null.
  95. *
  96. * @return void
  97. * @access private
  98. * @since 5/23/05
  99. */
  100. function parse () {
  101. preg_match($this->getRegex(), $this->input, $matches);
  102. // Matches:
  103. // [0] => T15:25:10-04:00
  104. // [1] => 15
  105. // [2] => 25
  106. // [3] => 10
  107. // [4] => -04:00
  108. // [5] => -
  109. // [6] => 04
  110. // [7] => 00
  111. if (isset($matches[1]))
  112. $this->setHour($matches[1]);
  113. if (isset($matches[2]))
  114. $this->setMinute($matches[2]);
  115. if (isset($matches[3]))
  116. $this->setSecond($matches[3]);
  117. if (isset($matches[4]) && $matches[4] == 'Z') {
  118. $this->setOffsetHour(0);
  119. $this->setOffsetMinute(0);
  120. } else if (isset($matches[4])) {
  121. $sign = $matches[5];
  122. $hour = $matches[6];
  123. if (isset($matches[7]))
  124. $minute = $matches[7];
  125. else
  126. $minute = 0;
  127. $this->setOffsetHour(intval($sign.$hour));
  128. $this->setOffsetMinute(intval($sign.$minute));
  129. }
  130. }
  131. }
  132.  
  133. ?>

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