Source for file TimeStringParser.class.php

Documentation is available at TimeStringParser.class.php

  1. <?php
  2. /**
  3. * @since 5/24/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: TimeStringParser.class.php,v 1.3 2006/12/01 16:34:53 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 Times (12-hour or 24-hour) in the form:
  19. * - <hour>:<minute>:<second> <am/pm>
  20. * <minute>, <second> or <am/pm> may be omitted. e.g. 1:59:30 pm; 8AM; 15:30
  21. *
  22. * @since 5/24/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: TimeStringParser.class.php,v 1.3 2006/12/01 16:34:53 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 TimeStringParser
  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. ( # One or Two-digit hour
  53. (?: [1-9])
  54. |
  55. (?: 1[0-9])
  56. |
  57. (?: 2[0-4])
  58. )
  59. (?: # Optional :Minute:Seconds component
  60. : # Colon
  61. ([0-5][0-9])? # Two-digit minute
  62. (?: # Optional :Seconds component
  63. : # Colon
  64. ( # Two-digit second
  65. [0-5][0-9]
  66. (?: \.[0-9]+)? # followed by an optional decimal.
  67. )
  68. )?
  69. )?
  70. \s? # Optional space
  71. (am|pm)? # Optional AM or PM
  72. $
  73. /xi";
  74. }
  75. /**
  76. * Parse the input string and set our elements based on the contents of the
  77. * input string. Elements not found in the string will be null.
  78. *
  79. * @return void
  80. * @access private
  81. * @since 5/24/05
  82. */
  83. function parse () {
  84. preg_match($this->getRegex(), $this->input, $matches);
  85. // Matches:
  86. // [0] => 3:25:10 pm
  87. // [1] => 3
  88. // [2] => 25
  89. // [3] => 10
  90. // [4] => pm
  91. if (isset($matches[4]) && strtolower($matches[4]) == 'am' && $matches[1] == '12')
  92. $this->setHour(0);
  93. else if (isset($matches[4]) && strtolower($matches[4]) == 'pm' && $matches[1] < 13)
  94. $this->setHour($matches[1] + 12);
  95. else
  96. $this->setHour($matches[1]);
  97. if (isset($matches[2]))
  98. $this->setMinute($matches[2]);
  99. if (isset($matches[3]))
  100. $this->setSecond($matches[3]);
  101. }
  102. }
  103.  
  104. ?>

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