Source for file Time.class.php

Documentation is available at Time.class.php

  1. <?php
  2. /**
  3. * @since 5/5/05
  4. * @package harmoni.primitives.chronology
  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: Time.class.php,v 1.7 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.  
  15. require_once(dirname(__FILE__)."/ChronologyConstants.class.php");
  16. require_once(dirname(__FILE__)."/../Magnitudes/Magnitude.class.php");
  17. require_once(dirname(__FILE__)."/Month.class.php");
  18. require_once(dirname(__FILE__)."/TimeZone.class.php");
  19. require_once(dirname(__FILE__)."/Week.class.php");
  20. require_once(dirname(__FILE__)."/Year.class.php");
  21.  
  22. /**
  23. * This represents a period of time.
  24. *
  25. * My implementation uses one SmallIntegers:
  26. * seconds - number of seconds since midnight.
  27. *
  28. * To create new Time instances, <b>use one of the static instance-creation
  29. * methods</b>, NOT 'new Time':
  30. * - {@link fromString Time::fromString($aString)}
  31. * - {@link fromString Time::fromString($aString)}
  32. * - {@link midnight Time::midnight()}
  33. * - {@link noon Time::noon()}
  34. * - {@link withHourMinuteSecond Time::withHourMinuteSecond($anIntHour, $anIntMinute,}
  35. * $anIntSecond)}
  36. * - {@link withSeconds Time::withSeconds($anIntSeconds)}
  37. *
  38. * @since 5/5/05
  39. * @package harmoni.primitives.chronology
  40. *
  41. * @copyright Copyright &copy; 2005, Middlebury College
  42. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  43. *
  44. * @version $Id: Time.class.php,v 1.7 2007/09/04 20:25:25 adamfranco Exp $
  45. *
  46. * @link http://harmoni.sourceforge.net/
  47. * @author Adam Franco <adam AT adamfranco DOT com> <afranco AT middlebury DOT edu>
  48. */
  49. class Time
  50. extends Magnitude
  51. {
  52.  
  53. /**
  54. * @var integer $seconds; The seconds from midnight of this time
  55. * @access private
  56. * @since 5/11/05
  57. */
  58. var $seconds;
  59.  
  60. /*******************************************************
  61. * Class Methods - Instance Creation
  62. *********************************************************/
  63.  
  64. /**
  65. * Read a Time from the stream in the forms:
  66. * - <hour24>:<minute>:<second>
  67. * - <hour>:<minute>:<second> <am/pm>
  68. * - <minute>, <second> or <am/pm> may be omitted. e.g. 1:59:30 pm; 8AM; 15:30
  69. *
  70. * @param string $aString
  71. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  72. * This parameter is used to get around the limitations of not being
  73. * able to find the class of the object that recieved the initial
  74. * method call.
  75. * @return object Time
  76. * @access public
  77. * @since 5/24/05
  78. */
  79. function fromString ( $aString, $class = 'Time' ) {
  80. $parser = StringParser::getParserFor($aString);
  81. if (!is_string($aString) || !preg_match('/[^\W]/', $aString) || !$parser) {
  82. $null = null;
  83. return $null;
  84. // die("'".$aString."' is not in a valid format.");
  85. }
  86. eval('$result = '.$class.'::withHourMinuteSecond($parser->hour(),
  87. $parser->minute(), $parser->second(), $class);');
  88. return $result;
  89. }
  90. /**
  91. * Answer the Time at midnight
  92. *
  93. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  94. * This parameter is used to get around the limitations of not being
  95. * able to find the class of the object that recieved the initial
  96. * method call.
  97. * @return object Time
  98. * @access public
  99. * @since 5/25/05
  100. */
  101. function midnight ( $class = 'Time' ) {
  102. eval('$result = '.$class.'::withSeconds(0, $class);');
  103. return $result;
  104. }
  105. /**
  106. * Answer the Time at noon
  107. *
  108. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  109. * This parameter is used to get around the limitations of not being
  110. * able to find the class of the object that recieved the initial
  111. * method call.
  112. * @return object Time
  113. * @access public
  114. * @since 5/25/05
  115. */
  116. function noon ( $class = 'Time' ) {
  117. eval('$result = '.$class.'::withHourMinuteSecond(12, 0, 0, $class);');
  118. return $result;
  119. }
  120. /**
  121. * Answer a Time from midnight
  122. *
  123. * @param integer $anIntHour
  124. * @param integer $anIntMinute
  125. * @param integer $anIntSecond
  126. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  127. * This parameter is used to get around the limitations of not being
  128. * able to find the class of the object that recieved the initial
  129. * method call.
  130. * @return object Time
  131. * @access public
  132. * @static
  133. * @since 5/4/05
  134. */
  135. function withHourMinuteSecond ($anIntHour, $anIntMinute, $anIntSecond, $class = 'Time' )
  136. {
  137. eval('$result = '.$class.'::withSeconds(
  138. ($anIntHour * ChronologyConstants::SecondsInHour())
  139. + ($anIntMinute * ChronologyConstants::SecondsInMinute())
  140. + $anIntSecond, $class);');
  141. return $result;
  142. }
  143. /**
  144. * Answer a Time from midnight
  145. *
  146. * @param integer $anIntSeconds
  147. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  148. * This parameter is used to get around the limitations of not being
  149. * able to find the class of the object that recieved the initial
  150. * method call.
  151. * @return object Time
  152. * @access public
  153. * @since 5/5/05
  154. */
  155. function withSeconds ( $anIntSeconds, $class = 'Time' ) {
  156. // Lop off any seconds beyond those in a day
  157. $duration = Duration::withSeconds($anIntSeconds);
  158. $ticks = $duration->ticks();
  159. $seconds = $ticks[1];
  160. // Make sure that we have a positive time since midnight
  161. if ($seconds < 0)
  162. $seconds = ChronologyConstants::SecondsInDay() + $seconds;
  163. // Validate our passed class name.
  164. if (!(strtolower($class) == strtolower('Time')
  165. || is_subclass_of(new $class, 'Time')))
  166. {
  167. die("Class, '$class', is not a subclass of 'Time'.");
  168. }
  169. $time = new $class;
  170. $time->setSeconds($seconds);
  171. return $time;
  172. }
  173. /*******************************************************
  174. * Instance Methods - Private
  175. *********************************************************/
  176.  
  177. /**
  178. * Set our seconds
  179. *
  180. * @param ingteger $anIntSeconds
  181. * @return void
  182. * @access private
  183. * @since 5/5/05
  184. */
  185. function setSeconds ( $anIntSeconds ) {
  186. $this->seconds = $anIntSeconds;
  187. }
  188. /**
  189. * Private - answer an array with our instance variables. Assumed to be UTC
  190. *
  191. * @return array
  192. * @access private
  193. * @since 5/4/05
  194. */
  195. function ticks () {
  196. return array (0, $this->seconds);
  197. }
  198. /*******************************************************
  199. * Instance Methods - Accessing
  200. *********************************************************/
  201.  
  202. /**
  203. * Answer the duration of this object (always zero)
  204. *
  205. * @return object Duration
  206. * @access public
  207. * @since 5/5/05
  208. */
  209. function duration () {
  210. $obj = Duration::zero();
  211. return $obj;
  212. }
  213. /**
  214. * Answer the hours (0-23)
  215. *
  216. * @return integer
  217. * @access public
  218. * @since 5/3/05
  219. */
  220. function hour () {
  221. return $this->hour24();
  222. }
  223. /**
  224. * Answer an <integer> between 1 and 12, inclusive, representing the hour
  225. * of the day in the 12-hour clock of the local time of the receiver.
  226. *
  227. * @return integer
  228. * @access public
  229. * @since 5/4/05
  230. */
  231. function hour12 () {
  232. $x = ($this->hour24() - 1) % 12;
  233. if ($x < 0)
  234. $x = $x + 12;
  235. return $x + 1;
  236. }
  237. /**
  238. * Answer the hours (0-23)
  239. *
  240. * @return integer
  241. * @access public
  242. * @since 5/3/05
  243. */
  244. function hour24 () {
  245. $duration =$this->asDuration();
  246. return $duration->hours();
  247. }
  248. /**
  249. * Return the Meridian Abbreviation ('AM'/'PM')
  250. *
  251. * @return string
  252. * @access public
  253. * @since 5/5/05
  254. */
  255. function meridianAbbreviation () {
  256. if ($this->hour() < 12)
  257. return 'AM';
  258. else
  259. return 'PM';
  260. }
  261. /**
  262. * Answer the minute (0-59)
  263. *
  264. * @return integer
  265. * @access public
  266. * @since 5/3/05
  267. */
  268. function minute () {
  269. $asDuration =$this->asDuration();
  270. return $asDuration->minutes();
  271. }
  272. /**
  273. * Format is 'h:mm:ss am' or, if showSeconds is false, 'h:mm am'
  274. *
  275. * @param optional boolean $showSeconds
  276. * @return string
  277. * @access public
  278. * @since 5/20/05
  279. */
  280. function string12 ( $showSeconds = TRUE ) {
  281. if ($this->hour() > 12)
  282. $result = $this->hour() - 12;
  283. else
  284. $result = $this->hour();
  285. if (!$result)
  286. $result = 12;
  287. $result .= ':';
  288. $result .= str_pad(abs($this->minute()), 2, '0', STR_PAD_LEFT);
  289. if ($showSeconds) {
  290. $result .= ':';
  291. $result .= str_pad(abs($this->second()), 2, '0', STR_PAD_LEFT);
  292. }
  293. if ($this->hour() >= 12)
  294. $result .= ' pm';
  295. else
  296. $result .= ' am';
  297. return $result;
  298. }
  299. /**
  300. * Format is 'hh:mm:ss' or, if showSeconds is false, 'hh:mm'
  301. *
  302. * @param optional boolean $showSeconds
  303. * @return string
  304. * @access public
  305. * @since 5/20/05
  306. */
  307. function string24 ( $showSeconds = TRUE ) {
  308. $result = str_pad(abs($this->hour()), 2, '0', STR_PAD_LEFT);
  309. $result .= ':';
  310. $result .= str_pad(abs($this->minute()), 2, '0', STR_PAD_LEFT);
  311. if ($showSeconds) {
  312. $result .= ':';
  313. $result .= str_pad(abs($this->second()), 2, '0', STR_PAD_LEFT);
  314. }
  315. return $result;
  316. }
  317. /**
  318. * Format is 'h:mm<:ss> am'
  319. *
  320. * @return string
  321. * @access public
  322. * @since 5/20/05
  323. */
  324. function printableString () {
  325. return $this->string12(($this->second() != 0));
  326. }
  327. /**
  328. * Answer the second (0-59)
  329. *
  330. * @return integer
  331. * @access public
  332. * @since 5/3/05
  333. */
  334. function second () {
  335. $asDuration =$this->asDuration();
  336. return $asDuration->seconds();
  337. }
  338. /*******************************************************
  339. * Instance methods - Comparing/Testing
  340. *********************************************************/
  341.  
  342. /**
  343. * comparand conforms to protocol DateAndTime,
  344. * or can be converted into something that conforms.
  345. *
  346. * @param object $comparand
  347. * @return boolean
  348. * @access public
  349. * @since 5/3/05
  350. */
  351. function isEqualTo ( $comparand ) {
  352. if ($this === $comparand)
  353. return TRUE;
  354.  
  355. if (!strtolower(get_class($comparand)) == 'time'
  356. && !is_subclass_of($comparand, 'Time'))
  357. return FALSE;
  358. $myTicks = $this->ticks();
  359. $comparandTicks = $comparand->ticks();
  360. if ($myTicks[0] != $comparandTicks[0])
  361. return FALSE;
  362. else
  363. return ($myTicks[1] == $comparandTicks[1]);
  364. }
  365. /**
  366. * comparand conforms to protocol DateAndTime,
  367. * or can be converted into something that conforms.
  368. *
  369. * @param object $comparand
  370. * @return boolean
  371. * @access public
  372. * @since 5/3/05
  373. */
  374. function isLessThan ( $comparand ) {
  375. $myDuration =$this->asDuration();
  376. return $myDuration->isLessThan($comparand->asDuration());
  377. }
  378.  
  379. /*******************************************************
  380. * Instance methods - Operations
  381. *********************************************************/
  382.  
  383. /**
  384. * Answer a Time that is nSeconds after the receiver.
  385. *
  386. * @param integer $anInteger
  387. * @return object Time
  388. * @access public
  389. * @since 5/25/05
  390. */
  391. function addSeconds ( $anInteger ) {
  392. eval('$result = '.get_class($this).'::withSeconds(
  393. $this->asSeconds() + $anInteger);');
  394. return $result;
  395. }
  396. /**
  397. * Answer a Time that is timeInterval after the receiver. timeInterval is an
  398. * instance of Date or Time.
  399. *
  400. * @param object $timeAmount An instance of Date or Time.
  401. * @return object Time
  402. * @access public
  403. * @since 5/25/05
  404. */
  405. function addTime ( $timeAmount ) {
  406. eval('$result = '.get_class($this).'::withSeconds(
  407. $this->asSeconds() + $timeAmount->asSeconds());');
  408. return $result;
  409. }
  410. /**
  411. * Answer a Time that is timeInterval before the receiver. timeInterval is
  412. * an instance of Date or Time.
  413. *
  414. * @param object $timeAmount An instance of Date or Time.
  415. * @return object Time
  416. * @access public
  417. * @since 5/25/05
  418. */
  419. function subtractTime ( $timeAmount ) {
  420. eval('$result = '.get_class($this).'::withSeconds(
  421. $this->asSeconds() - $timeAmount->asSeconds());');
  422. return $result;
  423. }
  424.  
  425. /*******************************************************
  426. * Instance methods - Converting
  427. *********************************************************/
  428.  
  429. /**
  430. * Answer a Date that represents this object
  431. *
  432. * @return object Date
  433. * @access public
  434. * @since 5/5/05
  435. */
  436. function asDate () {
  437. $obj = Date::today();
  438. return $obj;
  439. }
  440. /**
  441. * Answer a DateAndTime that represents this object
  442. *
  443. * @return object DateAndTime
  444. * @access public
  445. * @since 5/4/05
  446. */
  447. function asDateAndTime () {
  448. $dateAndTime = DateAndTime::today();
  449. $obj =$dateAndTime->plus($this);
  450. return $obj;
  451. }
  452. /**
  453. * Answer a Duration that represents this object, the duration since
  454. * midnight.
  455. *
  456. * @return object Duration
  457. * @access public
  458. * @since 5/4/05
  459. */
  460. function asDuration () {
  461. $obj = Duration::withSeconds($this->seconds);
  462. return $obj;
  463. }
  464. /**
  465. * Answer the month that represents this date's month
  466. *
  467. * @return object Month
  468. * @access public
  469. * @since 5/5/05
  470. */
  471. function asMonth () {
  472. $asDateAndTime =$this->asDateAndTime();
  473. $obj =$asDateAndTime->asMonth();
  474. return $obj;
  475. }
  476. /**
  477. * Answer the number of seconds since midnight of the receiver.
  478. *
  479. * @return integer
  480. * @access public
  481. * @since 5/5/05
  482. */
  483. function asSeconds () {
  484. return $this->seconds;
  485. }
  486. /**
  487. * Answer a Time that represents our time component
  488. *
  489. * @return object Time
  490. * @access public
  491. * @since 5/5/05
  492. */
  493. function asTime () {
  494. return $this;
  495. }
  496. /**
  497. * Answer a Timestamp that represents this DateAndTime
  498. *
  499. * @return object TimeStamp
  500. * @access public
  501. * @since 5/5/05
  502. */
  503. function asTimeStamp () {
  504. $asDateAndTime =$this->asDateAndTime();
  505. $obj =$asDateAndTime->asTimeStamp();
  506. return $obj;
  507. }
  508. /**
  509. * Answer this time as a Week
  510. *
  511. * @return object Year
  512. * @access public
  513. * @since 5/5/05
  514. */
  515. function asWeek () {
  516. $asDateAndTime =$this->asDateAndTime();
  517. $obj =$asDateAndTime->asWeek();
  518. return $obj;
  519. }
  520. /**
  521. * Answer this time as a Year
  522. *
  523. * @return object Year
  524. * @access public
  525. * @since 5/5/05
  526. */
  527. function asYear () {
  528. $asDateAndTime =$this->asDateAndTime();
  529. $obj =$asDateAndTime->asYear();
  530. return $obj;
  531. }
  532. /**
  533. * Answer a Timespan. anEnd must respond to asDateAndTime()
  534. *
  535. * @param object $anEnd anEnd must understand asDateAndTime()
  536. * @return object Timespan
  537. * @access public
  538. * @since 5/25/05
  539. */
  540. function to ( $anEnd ) {
  541. $asDateAndTime =$this->asDateAndTime();
  542. $obj =$asDateAndTime->to($anEnd);
  543. return $obj;
  544. }
  545. }
  546.  
  547. ?>

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