Source for file DateAndTimeStringParser.class.php

Documentation is available at DateAndTimeStringParser.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: DateAndTimeStringParser.class.php,v 1.3 2007/09/04 20:25:25 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.  
  16.  
  17. /**
  18. * DateAndTimeStringParser breaks up strings into a date component and a time
  19. * component and attempts to match each individually.
  20. * The string must contain a valid time component at the end in order to be parsed.
  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: DateAndTimeStringParser.class.php,v 1.3 2007/09/04 20:25:25 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 DateAndTimeStringParser
  34. extends RegexStringParser
  35. {
  36.  
  37. /*******************************************************
  38. * Instance Methods
  39. *********************************************************/
  40.  
  41. /**
  42. * Return the regular expression used by this parser.
  43. * If the input has a time component, then this parser can handle it.
  44. *
  45. * @return string
  46. * @access protected
  47. * @since 5/24/05
  48. */
  49. function getRegex () {
  50. $timeRegex = TimeStringParser::getRegex();
  51. // Remove a line-beginning anchor from the time expression
  52. return preg_replace('/\/[\s\r]*\^/', '/', $timeRegex);
  53. }
  54. /**
  55. * Parse the input string and set our elements based on the contents of the
  56. * input string. Elements not found in the string will be null.
  57. *
  58. * @return void
  59. * @access private
  60. * @since 5/24/05
  61. */
  62. function parse () {
  63. preg_match($this->getRegex(), $this->input, $timeMatches);
  64. $timeComponent = $timeMatches[0];
  65. // The date is anything before the time
  66. $dateComponent = trim(str_replace($timeComponent, '', $this->input));
  67. $timeParser = new TimeStringParser($timeComponent);
  68. $dateParser = StringParser::getParserFor($dateComponent);
  69. // Merge the two results into our fields
  70. if ($dateParser) {
  71. $this->setYear($dateParser->year());
  72. $this->setMonth($dateParser->month());
  73. $this->setDay($dateParser->day());
  74. }
  75. $this->setHour($timeParser->hour());
  76. $this->setMinute($timeParser->minute());
  77. $this->setSecond($timeParser->second());
  78. if (!is_null($timeParser->offsetHour()))
  79. $this->setOffsetHour($timeParser->offsetHour());
  80. if (!is_null($timeParser->offsetMinute()))
  81. $this->setOffsetMinute($timeParser->offsetMinute());
  82. if (!is_null($timeParser->offsetSecond()))
  83. $this->setOffsetSecond($timeParser->offsetSecond());
  84. }
  85. }
  86.  
  87. ?>

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