Source for file ANSI58216StringParser.class.php

Documentation is available at ANSI58216StringParser.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: ANSI58216StringParser.class.php,v 1.2 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 parses durations formatted as per ANSI 5.8.2.16: [-]D:HH:MM:SS[.S]
  19. *
  20. * Examples:
  21. * - '0:00:00:00'
  22. * - '0:00:00:00.000000001'
  23. * - '0:00:00:00.999999999'
  24. * - '0:00:00:00.100000000'
  25. * - '0:00:00:00.10'
  26. * - '0:00:00:00.1'
  27. * - '0:00:00:01'
  28. * - '0:12:45:45'
  29. * - '1:00:00:00'
  30. * - '365:00:00:00'
  31. * - '-7:09:12:06.10'
  32. * - '+0:01:02'
  33. * - '+0:01:02:3'
  34. *
  35. * @since 5/23/05
  36. * @package harmoni.primitives.chronology.string_parsers
  37. *
  38. * @copyright Copyright &copy; 2005, Middlebury College
  39. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  40. *
  41. * @version $Id: ANSI58216StringParser.class.php,v 1.2 2006/06/26 12:55:08 adamfranco Exp $
  42. *
  43. * @link http://harmoni.sourceforge.net/
  44. * @author Adam Franco <adam AT adamfranco DOT com> <afranco AT middlebury DOT edu>
  45. */
  46. class ANSI58216StringParser
  47. extends RegexStringParser {
  48. /*******************************************************
  49. * Instance Methods
  50. *********************************************************/
  51.  
  52. /**
  53. * Return the regular expression used by this parser
  54. *
  55. * @return string
  56. * @access protected
  57. * @since 5/24/05
  58. */
  59. function getRegex () {
  60. return
  61. "/
  62. ^ # Start of the line
  63.  
  64. #-----------------------------------------------------------------------------
  65. (?:
  66. (-) # The sign of the duration
  67. |
  68. \+ # Or a + or space
  69. |
  70. \s
  71. )?
  72. ([0-9]+) # The number of Days
  73.  
  74. #-----------------------------------------------------------------------------
  75. : # Colon
  76. ( # Two-digit hour
  77. (?: [0-1][0-9])
  78. |
  79. (?: 2[0-4])
  80. )
  81. : # Colon
  82. ([0-5][0-9]) # Two-digit minute
  83. (?: # Optional second component
  84. : # Colon
  85.  
  86. ( # Two-digit second
  87. [0-5][0-9]
  88.  
  89. (?: \.[0-9]+)? # followed by an optional decimal.
  90. )
  91. )?
  92. $
  93. /x";
  94. }
  95. /**
  96. * Parse the input string and set our elements based on the contents of the
  97. * input string. Elements not found in the string will be null.
  98. *
  99. * @return void
  100. * @access private
  101. * @since 5/23/05
  102. */
  103. function parse () {
  104. preg_match($this->getRegex(), $this->input, $matches);
  105. // Matches:
  106. // [0] => -7:09:12:06.10
  107. // [1] => -
  108. // [2] => 7
  109. // [3] => 09
  110. // [4] => 12
  111. // [5] => 06.10
  112. if (isset($matches[2]))
  113. $this->setDay($matches[1].$matches[2]);
  114. if (isset($matches[3]))
  115. $this->setHour($matches[1].$matches[3]);
  116. if (isset($matches[4]))
  117. $this->setMinute($matches[1].$matches[4]);
  118. if (isset($matches[5]))
  119. $this->setSecond($matches[1].$matches[5]);
  120. }
  121. /**
  122. * To allow for very large days, override setDay to not use intval(). and
  123. * to just leave the days as a string for now.
  124. *
  125. * @param integer $anInteger
  126. * @return void
  127. * @access private
  128. * @since 5/25/05
  129. */
  130. function setDay ( $anInteger ) {
  131. $this->day = $anInteger;
  132. }
  133. }
  134.  
  135. ?>

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