Source for file TimeStamp.class.php

Documentation is available at TimeStamp.class.php

  1. <?php
  2. /**
  3. * @since 5/11/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: TimeStamp.class.php,v 1.5 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__)."/DateAndTime.class.php");
  15.  
  16. /**
  17. * This represents a duration of 0 length that marks a particular point in time.
  18. *
  19. * To create new TimeStamp instances, <b>use one of the static instance-creation
  20. * methods</b>, NOT 'new TimeStamp':
  21. * - {@link current TimeStamp::current()}
  22. * - {@link current TimeStamp::current()}
  23. * - {@link epoch TimeStamp::epoch()}
  24. * - {@link fromString TimeStamp::fromString($aString)}
  25. * - {@link fromUnixTimeStamp TimeStamp::fromUnixTimeStamp($aUnixTimeStamp)}
  26. * - {@link midnight TimeStamp::midnight()}
  27. * - {@link now TimeStamp::now()}
  28. * - {@link noon TimeStamp::noon()}
  29. * - {@link today TimeStamp::today()}
  30. * - {@link tomorrow TimeStamp::tomorrow()}
  31. * - {@link withDateAndTime TimeStamp::withDateAndTime($aDate, $aTime)}
  32. * - {@link withJulianDayNumber TimeStamp::withJulianDayNumber($aJulianDayNumber)}
  33. * - {@link withYearDay TimeStamp::withYearDay($anIntYear, $anIntDayOfYear)}
  34. * - {@link withYearDayHourMinuteSecond TimeStamp::withYearDayHourMinuteSecond(}
  35. * $anIntYear, $anIntDayOfYear, $anIntHour, $anIntMinute,
  36. * $anIntSecond)}
  37. * - {@link withYearDayHourMinuteSecondOffset}
  38. * TimeStamp::withYearDayHourMinuteSecondOffset($anIntYear,
  39. * $anIntDayOfYear, $anIntHour, $anIntMinute, $anIntSecond,
  40. * $aDurationOffset)}
  41. * - {@link withYearMonthDay TimeStamp::withYearMonthDay($anIntYear,}
  42. * $anIntOrStringMonth, $anIntDay)}
  43. * - {@link withYearMonthDayHourMinute TimeStamp::withYearMonthDayHourMinute(}
  44. * $anIntYear, $anIntOrStringMonth, $anIntDay, $anIntHour,
  45. * $anIntMinute)}
  46. * - {@link withYearMonthDayHourMinuteSecond}
  47. * TimeStamp::withYearMonthDayHourMinuteSecond($anIntYear,
  48. * $anIntOrStringMonth, $anIntDay, $anIntHour, $anIntMinute,
  49. * $anIntSecond)}
  50. * - {@link withYearMonthDayHourMinuteSecondOffset}
  51. * TimeStamp::withYearMonthDayHourMinuteSecondOffset($anIntYear,
  52. * $anIntOrStringMonth, $anIntDay, $anIntHour, $anIntMinute,
  53. * $anIntSecond, $aDurationOffset)}
  54. * - {@link yesterday TimeStamp::yesterday()}
  55. *
  56. * @since 5/11/05
  57. * @package harmoni.primitives.chronology
  58. *
  59. * @copyright Copyright &copy; 2005, Middlebury College
  60. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  61. *
  62. * @version $Id: TimeStamp.class.php,v 1.5 2007/09/04 20:25:25 adamfranco Exp $
  63. *
  64. * @link http://harmoni.sourceforge.net/
  65. * @author Adam Franco <adam AT adamfranco DOT com> <afranco AT middlebury DOT edu>
  66. */
  67. class TimeStamp
  68. extends DateAndTime
  69. {
  70. /*******************************************************
  71. * Class Methods - Instance Creation
  72. *
  73. * All static instance creation methods have an optional
  74. * $class parameter which is used to get around the limitations
  75. * of not being able to find the class of the object that
  76. * recieved the initial method call rather than the one in
  77. * which it is implemented. These parameters SHOULD NOT BE
  78. * USED OUTSIDE OF THIS PACKAGE.
  79. *********************************************************/
  80.  
  81. /**
  82. * Answer a TimeStamp representing now
  83. *
  84. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  85. * This parameter is used to get around the limitations of not being
  86. * able to find the class of the object that recieved the initial
  87. * method call.
  88. * @return object TimeStamp
  89. * @access public
  90. * @since 5/13/05
  91. */
  92. function current ( $class = 'TimeStamp' ) {
  93. eval('$result = '.$class.'::now();');
  94. return $result;
  95. }
  96. /**
  97. * Answer a TimeStamp representing the Squeak epoch: 1 January 1901
  98. *
  99. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  100. * This parameter is used to get around the limitations of not being
  101. * able to find the class of the object that recieved the initial
  102. * method call.
  103. * @return object TimeStamp
  104. * @access public
  105. * @since 5/2/05
  106. * @static
  107. */
  108. function epoch ( $class = 'TimeStamp' ) {
  109. $obj = parent::epoch($class);
  110. return $obj;
  111. }
  112. /**
  113. * Answer a new instance represented by a string:
  114. *
  115. * - '-1199-01-05T20:33:14.321-05:00'
  116. * - ' 2002-05-16T17:20:45.00000001+01:01'
  117. * - ' 2002-05-16T17:20:45.00000001'
  118. * - ' 2002-05-16T17:20'
  119. * - ' 2002-05-16T17:20:45'
  120. * - ' 2002-05-16T17:20:45+01:57'
  121. * - ' 2002-05-16T17:20:45-02:34'
  122. * - ' 2002-05-16T17:20:45+00:00'
  123. * - ' 1997-04-26T01:02:03+01:02:3'
  124. *
  125. * @param string $aString The input string.
  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 TimeStamp
  131. * @access public
  132. * @since 5/12/05
  133. * @static
  134. */
  135. function fromString ( $aString, $class = 'TimeStamp' ) {
  136. $obj = parent::fromString( $aString, $class);
  137. return $obj;
  138. }
  139. /**
  140. * Create a new TimeStamp from a UNIX timestamp.
  141. *
  142. * @param integer $aUnixTimeStamp The number of seconds since the Unix Epoch
  143. * (January 1 1970 00:00:00 GMT/UTC)
  144. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  145. * This parameter is used to get around the limitations of not being
  146. * able to find the class of the object that recieved the initial
  147. * method call.
  148. * @return object Timestamp
  149. * @access public
  150. * @since 5/27/05
  151. */
  152. function fromUnixTimeStamp ( $aUnixTimeStamp, $class = 'TimeStamp' ) {
  153. $sinceUnixEpoch = Duration::withSeconds($aUnixTimeStamp);
  154. eval('$unixEpoch = '.$class.'::withYearMonthDayHourMinuteSecondOffset(
  155. 1970, 1, 1, 0, 0, 0, Duration::zero());');
  156. $obj =$unixEpoch->plus($sinceUnixEpoch);
  157. return $obj;
  158. }
  159. /**
  160. * Answer a new instance starting at midnight local time.
  161. *
  162. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  163. * This parameter is used to get around the limitations of not being
  164. * able to find the class of the object that recieved the initial
  165. * method call.
  166. * @return object TimeStamp
  167. * @access public
  168. * @since 5/3/05
  169. * @static
  170. */
  171. function midnight ( $class = 'TimeStamp' ) {
  172. $obj = parent::midnight( $class );
  173. return $obj;
  174. }
  175. /**
  176. * Answer the current time.
  177. *
  178. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  179. * This parameter is used to get around the limitations of not being
  180. * able to find the class of the object that recieved the initial
  181. * method call.
  182. * @return object TimeStamp
  183. * @access public
  184. * @since 5/12/05
  185. * @static
  186. */
  187. function now ( $class = 'TimeStamp' ) {
  188. $obj = parent::now( $class );
  189. return $obj;
  190. }
  191. /**
  192. * Answer a new instance starting at noon local time.
  193. *
  194. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  195. * This parameter is used to get around the limitations of not being
  196. * able to find the class of the object that recieved the initial
  197. * method call.
  198. * @return object TimeStamp
  199. * @access public
  200. * @since 5/3/05
  201. * @static
  202. */
  203. function noon ( $class = 'TimeStamp' ) {
  204. $obj = parent::noon( $class );
  205. return $obj;
  206. }
  207. /**
  208. * Answer a new instance representing today
  209. *
  210. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  211. * This parameter is used to get around the limitations of not being
  212. * able to find the class of the object that recieved the initial
  213. * method call.
  214. * @return object TimeStamp
  215. * @access public
  216. * @since 5/12/05
  217. * @static
  218. */
  219. function today ( $class = 'TimeStamp' ) {
  220. $obj = parent::today( $class );
  221. return $obj;
  222. }
  223. /**
  224. * Answer a new instance representing tomorow
  225. *
  226. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  227. * This parameter is used to get around the limitations of not being
  228. * able to find the class of the object that recieved the initial
  229. * method call.
  230. * @return object TimeStamp
  231. * @access public
  232. * @since 5/12/05
  233. * @static
  234. */
  235. function tomorrow ( $class = 'TimeStamp' ) {
  236. $obj = parent::tomorrow( $class );
  237. return $obj;
  238. }
  239. /**
  240. * Create a new instance from Date and Time objects
  241. *
  242. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  243. * This parameter is used to get around the limitations of not being
  244. * able to find the class of the object that recieved the initial
  245. * method call.
  246. * @return object TimeStamp
  247. * @access public
  248. * @since 5/12/05
  249. * @static
  250. */
  251. function withDateAndTime ( $aDate, $aTime, $class = 'TimeStamp' ) {
  252. $obj = parent::withDateAndTime( $aDate, $aTime, $class );
  253. return $obj;
  254. }
  255. /**
  256. * Create a new instance for a given Julian Day Number.
  257. *
  258. * @param integer $aJulianDayNumber
  259. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  260. * This parameter is used to get around the limitations of not being
  261. * able to find the class of the object that recieved the initial
  262. * method call.
  263. * @return object TimeStamp
  264. * @access public
  265. * @since 5/2/05
  266. * @static
  267. */
  268. function withJulianDayNumber ( $aJulianDayNumber, $class = 'TimeStamp' ) {
  269. $obj = parent::withJulianDayNumber($aJulianDayNumber, $class);
  270. return $obj;
  271. }
  272. /**
  273. * Create a new instance.
  274. *
  275. * @param integer $anIntYear
  276. * @param integer $anIntDayOfYear
  277. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  278. * This parameter is used to get around the limitations of not being
  279. * able to find the class of the object that recieved the initial
  280. * method call.
  281. * @access public
  282. * @static
  283. * @since 5/4/05
  284. */
  285. function withYearDay ( $anIntYear, $anIntDayOfYear, $class = 'TimeStamp') {
  286. $obj = parent::withYearDay ( $anIntYear, $anIntDayOfYear, $class );
  287. return $obj;
  288. }
  289. /**
  290. * Create a new instance.
  291. *
  292. * @param integer $anIntYear
  293. * @param integer $anIntDayOfYear
  294. * @param integer $anIntHour
  295. * @param integer $anIntMinute
  296. * @param integer $anIntSecond
  297. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  298. * This parameter is used to get around the limitations of not being
  299. * able to find the class of the object that recieved the initial
  300. * method call.
  301. * @return object TimeStamp
  302. * @access public
  303. * @static
  304. * @since 5/4/05
  305. */
  306. function withYearDayHourMinuteSecond ( $anIntYear, $anIntDayOfYear,
  307. $anIntHour, $anIntMinute, $anIntSecond, $class = 'TimeStamp' )
  308. {
  309. $obj = parent::withYearDayHourMinuteSecond ( $anIntYear, $anIntDayOfYear,
  310. $anIntHour, $anIntMinute, $anIntSecond, $class);
  311. return $obj;
  312. }
  313. /**
  314. * Create a new instance.
  315. *
  316. * @param integer $anIntYear
  317. * @param integer $anIntDayOfYear
  318. * @param integer $anIntHour
  319. * @param integer $anIntMinute
  320. * @param integer $anIntSecond
  321. * @param object Duration $aDurationOffset
  322. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  323. * This parameter is used to get around the limitations of not being
  324. * able to find the class of the object that recieved the initial
  325. * method call.
  326. * @return object TimeStamp
  327. * @access public
  328. * @static
  329. * @since 5/4/05
  330. */
  331. function withYearDayHourMinuteSecondOffset ( $anIntYear, $anIntDayOfYear,
  332. $anIntHour, $anIntMinute, $anIntSecond, $aDurationOffset, $class = 'TimeStamp' )
  333. {
  334. $obj = parent::withYearDayHourMinuteSecondOffset ( $anIntYear, $anIntDayOfYear,
  335. $anIntHour, $anIntMinute, $anIntSecond, $aDurationOffset, $class);
  336. return $obj;
  337. }
  338. /**
  339. * Create a new instance.
  340. *
  341. * @param integer $anIntYear
  342. * @param integer $anIntOrStringMonth
  343. * @param integer $anIntDay
  344. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  345. * This parameter is used to get around the limitations of not being
  346. * able to find the class of the object that recieved the initial
  347. * method call.
  348. * @access public
  349. * @return object TimeStamp
  350. * @static
  351. * @since 5/4/05
  352. */
  353. function withYearMonthDay ( $anIntYear, $anIntOrStringMonth, $anIntDay,
  354. $class = 'Timestamp' )
  355. {
  356. $obj = parent::withYearMonthDay ( $anIntYear, $anIntOrStringMonth, $anIntDay,
  357. $class);
  358. return $obj;
  359. }
  360. /**
  361. * Create a new instance.
  362. *
  363. * @param integer $anIntYear
  364. * @param integer $anIntOrStringMonth
  365. * @param integer $anIntDay
  366. * @param integer $anIntHour
  367. * @param integer $anIntMinute
  368. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  369. * This parameter is used to get around the limitations of not being
  370. * able to find the class of the object that recieved the initial
  371. * method call.
  372. * @return object TimeStamp
  373. * @access public
  374. * @static
  375. * @since 5/4/05
  376. */
  377. function withYearMonthDayHourMinute ( $anIntYear, $anIntOrStringMonth,
  378. $anIntDay, $anIntHour, $anIntMinute, $class = 'TimeStamp' )
  379. {
  380. $obj = parent::withYearMonthDayHourMinute ( $anIntYear, $anIntOrStringMonth,
  381. $anIntDay, $anIntHour, $anIntMinute, $class);
  382.  
  383. return $obj;
  384. }
  385. /**
  386. * Create a new instance.
  387. *
  388. * @param integer $anIntYear
  389. * @param integer $anIntOrStringMonth
  390. * @param integer $anIntDay
  391. * @param integer $anIntHour
  392. * @param integer $anIntMinute
  393. * @param integer $anIntSecond
  394. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  395. * This parameter is used to get around the limitations of not being
  396. * able to find the class of the object that recieved the initial
  397. * method call.
  398. * @return object TimeStamp
  399. * @access public
  400. * @static
  401. * @since 5/4/05
  402. */
  403. function withYearMonthDayHourMinuteSecond ( $anIntYear, $anIntOrStringMonth,
  404. $anIntDay, $anIntHour, $anIntMinute, $anIntSecond, $class = 'TimeStamp' )
  405. {
  406. $obj = parent::withYearMonthDayHourMinuteSecond ( $anIntYear, $anIntOrStringMonth,
  407. $anIntDay, $anIntHour, $anIntMinute, $anIntSecond, $class);
  408. return $obj;
  409. }
  410. /**
  411. * Create a new instance.
  412. *
  413. * @param integer $anIntYear
  414. * @param integer $anIntOrStringMonth
  415. * @param integer $anIntDay
  416. * @param integer $anIntHour
  417. * @param integer $anIntMinute
  418. * @param integer $anIntSecond
  419. * @param object Duration $aDurationOffset
  420. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  421. * This parameter is used to get around the limitations of not being
  422. * able to find the class of the object that recieved the initial
  423. * method call.
  424. * @return object TimeStamp
  425. * @access public
  426. * @static
  427. * @since 5/4/05
  428. */
  429. function withYearMonthDayHourMinuteSecondOffset ( $anIntYear,
  430. $anIntOrStringMonth, $anIntDay, $anIntHour, $anIntMinute,
  431. $anIntSecond, $aDurationOffset, $class = 'TimeStamp' )
  432. {
  433. $obj = parent::withYearMonthDayHourMinuteSecondOffset ( $anIntYear,
  434. $anIntOrStringMonth, $anIntDay, $anIntHour, $anIntMinute,
  435. $anIntSecond, $aDurationOffset, $class);
  436.  
  437. return $obj;
  438. }
  439. /**
  440. * Answer a new instance representing yesterday
  441. *
  442. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  443. * This parameter is used to get around the limitations of not being
  444. * able to find the class of the object that recieved the initial
  445. * method call.
  446. * @return object TimeStamp
  447. * @access public
  448. * @since 5/12/05
  449. * @static
  450. */
  451. function yesterday ( $class = 'TimeStamp' ) {
  452. $obj = parent::yesterday($class);
  453. return $obj;
  454. }
  455.  
  456. /*******************************************************
  457. * Instance methods - Accessing
  458. *********************************************************/
  459.  
  460. /**
  461. * Print receiver's date and time
  462. *
  463. * @return string
  464. * @access public
  465. * @since 5/13/05
  466. */
  467. function printableString () {
  468. $date =$this->date();
  469. $time =$this->time();
  470. return $date->printableString().' '.$time->printableString();
  471. }
  472.  
  473. /*******************************************************
  474. * Instance methods - Converting
  475. *********************************************************/
  476.  
  477. /**
  478. * Answer a Timestamp that represents this DateAndTime
  479. *
  480. * @return object TimeStamp
  481. * @access public
  482. * @since 5/5/05
  483. */
  484. function asTimeStamp () {
  485. return $this;
  486. }
  487. /**
  488. * Answer the reciever as a UNIX timestamp - The number of seconds since the
  489. * Unix Epoch (January 1 1970 00:00:00 GMT/UTC).
  490. *
  491. * @return integer
  492. * @access public
  493. * @since 5/27/05
  494. */
  495. function asUnixTimeStamp () {
  496. $sinceUnixEpoch =$this->minus(TimeStamp::withYearMonthDayHourMinuteSecondOffset(
  497. 1970, 1, 1, 0, 0, 0, Duration::zero()));
  498. return $sinceUnixEpoch->asSeconds();
  499. }
  500. /**
  501. * Answer the date of the receiver.
  502. *
  503. * @return object Date
  504. * @access public
  505. * @since 5/13/05
  506. */
  507. function date () {
  508. $obj =$this->asDate();
  509. return $obj;
  510. }
  511. /**
  512. * Answer a two element Array containing the receiver's date and time.
  513. *
  514. * @return array
  515. * @access public
  516. * @since 5/13/05
  517. */
  518. function dateAndTimeArray () {
  519. return array (
  520. $this->date(),
  521. $this->time()
  522. );
  523. }
  524. /**
  525. * Answer a TimeStamp which is anInteger days before the receiver.
  526. *
  527. * @param integer $anInteger
  528. * @return object TimeStamp
  529. * @access public
  530. * @since 5/13/05
  531. */
  532. function minusDays ( $anInteger ) {
  533. $obj =$this->minus(Duration::withDays($anInteger));
  534. return $obj;
  535. }
  536. /**
  537. * Answer a TimeStamp which is anInteger seconds before the receiver.
  538. *
  539. * @param integer $anInteger
  540. * @return object TimeStamp
  541. * @access public
  542. * @since 5/13/05
  543. */
  544. function minusSeconds ( $anInteger ) {
  545. $obj =$this->minus(Duration::withSeconds($anInteger));
  546. return $obj;
  547. }
  548. /**
  549. * Answer a TimeStamp which is anInteger days after the receiver.
  550. *
  551. * @param integer $anInteger
  552. * @return object TimeStamp
  553. * @access public
  554. * @since 5/13/05
  555. */
  556. function plusDays ( $anInteger ) {
  557. $obj =$this->plus(Duration::withDays($anInteger));
  558. return $obj;
  559. }
  560. /**
  561. * Answer a TimeStamp which is anInteger seconds after the receiver.
  562. *
  563. * @param integer $anInteger
  564. * @return object TimeStamp
  565. * @access public
  566. * @since 5/13/05
  567. */
  568. function plusSeconds ( $anInteger ) {
  569. $obj =$this->plus(Duration::withSeconds($anInteger));
  570. return $obj;
  571. }
  572. /**
  573. * Answer the time of the receiver.
  574. *
  575. * @return object Time
  576. * @access public
  577. * @since 5/13/05
  578. */
  579. function time () {
  580. $obj =$this->asTime();
  581. return $obj;
  582. }
  583. }
  584.  
  585. ?>

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