Source for file StorableTime.class.php

Documentation is available at StorableTime.class.php

  1. <?php
  2.  
  3. /**
  4. * This is the {@link StorablePrimitive} equivalent of {@link Time}.
  5. *
  6. * @package harmoni.datamanager.storableprimitives
  7. *
  8. * @copyright Copyright &copy; 2005, Middlebury College
  9. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  10. *
  11. * @version $Id: StorableTime.class.php,v 1.20 2007/09/04 20:25:33 adamfranco Exp $
  12. */
  13. class StorableTime
  14. extends DateAndTime /* implements StorablePrimitive */
  15. {
  16. var $_table;
  17.  
  18. /*******************************************************
  19. * Class Methods
  20. *********************************************************/
  21.  
  22. /**
  23. * Takes a single database row, which would contain the columns added by alterQuery()
  24. * and extracts the values to setup the object with the appropriate data.
  25. *
  26. * All times are stored in the database as UTC.
  27. *
  28. * @param array $dbRow
  29. * @access public
  30. * @return object StorableTime
  31. * @static
  32. */
  33. function createAndPopulate( $dbRow ) {
  34. $date = StorableTime::withJulianDayNumber($dbRow["time_jdn"]);
  35. $timeComponent = Duration::withSeconds($dbRow["time_seconds"]);
  36. $date =$date->plus($timeComponent);
  37. // The date in the DB was UTC, so be sure to set the offset to zero here.
  38. $date =$date->withOffset(Duration::zero());
  39. // Convert the time to the local offset, maintain equivalent time to the
  40. // UTC version
  41. return $date->asLocal();
  42. }
  43. /**
  44. * Returns a string that could be inserted into an SQL query's WHERE clause, based on the
  45. * {@link Primitive} value that is passed. It is used when searching for datasets that contain a certain
  46. * field=value pair.
  47. * @param ref object $value The {@link Primitive} object to search for.
  48. * @param int $searchType One of the SEARCH_TYPE_* constants, defining what type of search this should be (ie, equals,
  49. * contains, greater than, less than, etc)
  50. * @return string or NULL if no searching is allowed.
  51. * @static
  52. */
  53. function makeSearchString($value, $searchType = SEARCH_TYPE_EQUALS) {
  54. // Convert to UTC
  55. $utc =$this->asUTC();
  56. $utcTime =$utc->asTime();
  57. $jdn = $utc->julianDayNumber();
  58. $seconds = $utcTime->asSeconds();
  59. switch ($searchType) {
  60. case SEARCH_TYPE_EQUALS:
  61. return "(dm_time.jdn=$jdn AND dm_time.seconds=$seconds)";
  62. case SEARCH_TYPE_GREATER_THAN:
  63. return "(dm_time.jdn<$jdn OR (dm_time.jdn=$jdn AND dm_time.seconds<$seconds))";
  64. case SEARCH_TYPE_LESS_THAN:
  65. return "(dm_time.jdn>$jdn OR (dm_time.jdn=$jdn AND dm_time.seconds>$seconds))";
  66. case SEARCH_TYPE_GREATER_THAN_OR_EQUALS:
  67. return "(dm_time.jdn>$jdn OR (dm_time.jdn=$jdn AND dm_time.seconds>=$seconds))";
  68. case SEARCH_TYPE_LESS_THAN_OR_EQUALS:
  69. return "(dm_time.jdn<$jdn OR (dm_time.jdn=$jdn AND dm_time.seconds<=$seconds))";
  70. case SEARCH_TYPE_IN_LIST:
  71. $string = "(";
  72. while ($value->hasNext()) {
  73. $valueObj =$value->next();
  74. $string .= "(dm_time.jdn=$jdn AND dm_time.seconds=$seconds)";
  75. if ($value->hasNext())
  76. $string .= " OR ";
  77. }
  78. $string .= ")";
  79. return $string;
  80. case SEARCH_TYPE_NOT_IN_LIST:
  81. $string = "NOT (";
  82. while ($value->hasNext()) {
  83. $valueObj =$value->next();
  84. $string .= "(dm_time.jdn=$jdn AND dm_time.seconds=$seconds)";
  85. if ($value->hasNext())
  86. $string .= " OR ";
  87. }
  88. $string .= ")";
  89. return $string;
  90. }
  91. return null;
  92. }
  93. /*******************************************************
  94. * Class Methods - Instance Creation
  95. *
  96. * All static instance creation methods have an optional
  97. * $class parameter which is used to get around the limitations
  98. * of not being able to find the class of the object that
  99. * recieved the initial method call rather than the one in
  100. * which it is implemented. These parameters SHOULD NOT BE
  101. * USED OUTSIDE OF THIS PACKAGE.
  102. *********************************************************/
  103.  
  104. /**
  105. * Answer a StorableTime representing now
  106. *
  107. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  108. * This parameter is used to get around the limitations of not being
  109. * able to find the class of the object that recieved the initial
  110. * method call.
  111. * @return object StorableTime
  112. * @access public
  113. * @since 5/13/05
  114. */
  115. function current ( $class = 'StorableTime' ) {
  116. eval('$result = '.$class.'::now();');
  117. return $result;
  118. }
  119. /**
  120. * Answer a StorableTime representing the Squeak epoch: 1 January 1901
  121. *
  122. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  123. * This parameter is used to get around the limitations of not being
  124. * able to find the class of the object that recieved the initial
  125. * method call.
  126. * @return object StorableTime
  127. * @access public
  128. * @since 5/2/05
  129. * @static
  130. */
  131. function epoch ( $class = 'StorableTime' ) {
  132. return parent::epoch($class);
  133. }
  134. /**
  135. * Answer a new instance represented by a string:
  136. *
  137. * - '-1199-01-05T20:33:14.321-05:00'
  138. * - ' 2002-05-16T17:20:45.00000001+01:01'
  139. * - ' 2002-05-16T17:20:45.00000001'
  140. * - ' 2002-05-16T17:20'
  141. * - ' 2002-05-16T17:20:45'
  142. * - ' 2002-05-16T17:20:45+01:57'
  143. * - ' 2002-05-16T17:20:45-02:34'
  144. * - ' 2002-05-16T17:20:45+00:00'
  145. * - ' 1997-04-26T01:02:03+01:02:3'
  146. *
  147. * @param string $aString The input string.
  148. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  149. * This parameter is used to get around the limitations of not being
  150. * able to find the class of the object that recieved the initial
  151. * method call.
  152. * @return object StorableTime
  153. * @access public
  154. * @since 5/12/05
  155. * @static
  156. */
  157. function fromString ( $aString, $class = 'StorableTime' ) {
  158. return parent::fromString( $aString, $class);
  159. }
  160. /**
  161. * Create a new TimeStamp from a UNIX timestamp.
  162. *
  163. * @param integer $aUnixTimeStamp The number of seconds since the Unix Epoch
  164. * (January 1 1970 00:00:00 GMT/UTC)
  165. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  166. * This parameter is used to get around the limitations of not being
  167. * able to find the class of the object that recieved the initial
  168. * method call.
  169. * @return object Timestamp
  170. * @access public
  171. * @since 5/27/05
  172. */
  173. function fromUnixTimeStamp ( $aUnixTimeStamp, $class = 'StorableTime' ) {
  174. $sinceUnixEpoch = Duration::withSeconds($aUnixTimeStamp);
  175. eval('$unixEpoch = '.$class.'::withYearMonthDayHourMinuteSecondOffset(
  176. 1970, 1, 1, 0, 0, 0, Duration::zero());');
  177. return $unixEpoch->plus($sinceUnixEpoch);
  178. }
  179. /**
  180. * Answer a new instance starting at midnight local time.
  181. *
  182. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  183. * This parameter is used to get around the limitations of not being
  184. * able to find the class of the object that recieved the initial
  185. * method call.
  186. * @return object StorableTime
  187. * @access public
  188. * @since 5/3/05
  189. * @static
  190. */
  191. function midnight ( $class = 'StorableTime' ) {
  192. return parent::midnight( $class );
  193. }
  194. /**
  195. * Answer the current time.
  196. *
  197. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  198. * This parameter is used to get around the limitations of not being
  199. * able to find the class of the object that recieved the initial
  200. * method call.
  201. * @return object StorableTime
  202. * @access public
  203. * @since 5/12/05
  204. * @static
  205. */
  206. function now ( $class = 'StorableTime' ) {
  207. return parent::now( $class );
  208. }
  209. /**
  210. * Answer a new instance starting at noon local time.
  211. *
  212. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  213. * This parameter is used to get around the limitations of not being
  214. * able to find the class of the object that recieved the initial
  215. * method call.
  216. * @return object StorableTime
  217. * @access public
  218. * @since 5/3/05
  219. * @static
  220. */
  221. function noon ( $class = 'StorableTime' ) {
  222. return parent::noon( $class );
  223. }
  224. /**
  225. * Answer a new instance representing today
  226. *
  227. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  228. * This parameter is used to get around the limitations of not being
  229. * able to find the class of the object that recieved the initial
  230. * method call.
  231. * @return object StorableTime
  232. * @access public
  233. * @since 5/12/05
  234. * @static
  235. */
  236. function today ( $class = 'StorableTime' ) {
  237. return parent::today( $class );
  238. }
  239. /**
  240. * Answer a new instance representing tomorow
  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 StorableTime
  247. * @access public
  248. * @since 5/12/05
  249. * @static
  250. */
  251. function tomorrow ( $class = 'StorableTime' ) {
  252. return parent::tomorrow( $class );
  253. }
  254. /**
  255. * Create a new instance from Date and Time objects
  256. *
  257. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  258. * This parameter is used to get around the limitations of not being
  259. * able to find the class of the object that recieved the initial
  260. * method call.
  261. * @return object StorableTime
  262. * @access public
  263. * @since 5/12/05
  264. * @static
  265. */
  266. function withDateAndTime ( $aDate, $aTime, $class = 'StorableTime' ) {
  267. return parent::withDateAndTime( $aDate, $aTime, $class );
  268. }
  269. /**
  270. * Create a new instance for a given Julian Day Number.
  271. *
  272. * @param integer $aJulianDayNumber
  273. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  274. * This parameter is used to get around the limitations of not being
  275. * able to find the class of the object that recieved the initial
  276. * method call.
  277. * @return object StorableTime
  278. * @access public
  279. * @since 5/2/05
  280. * @static
  281. */
  282. function withJulianDayNumber ( $aJulianDayNumber, $class = 'StorableTime' ) {
  283. return parent::withJulianDayNumber($aJulianDayNumber, $class);
  284. }
  285. /**
  286. * Create a new instance.
  287. *
  288. * @param integer $anIntYear
  289. * @param integer $anIntDayOfYear
  290. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  291. * This parameter is used to get around the limitations of not being
  292. * able to find the class of the object that recieved the initial
  293. * method call.
  294. * @access public
  295. * @static
  296. * @since 5/4/05
  297. */
  298. function withYearDay ( $anIntYear, $anIntDayOfYear, $class = 'StorableTime') {
  299. return parent::withYearDay ( $anIntYear, $anIntDayOfYear, $class );
  300. }
  301. /**
  302. * Create a new instance.
  303. *
  304. * @param integer $anIntYear
  305. * @param integer $anIntDayOfYear
  306. * @param integer $anIntHour
  307. * @param integer $anIntMinute
  308. * @param integer $anIntSecond
  309. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  310. * This parameter is used to get around the limitations of not being
  311. * able to find the class of the object that recieved the initial
  312. * method call.
  313. * @return object StorableTime
  314. * @access public
  315. * @static
  316. * @since 5/4/05
  317. */
  318. function withYearDayHourMinuteSecond ( $anIntYear, $anIntDayOfYear,
  319. $anIntHour, $anIntMinute, $anIntSecond, $class = 'StorableTime' )
  320. {
  321. return parent::withYearDayHourMinuteSecond ( $anIntYear, $anIntDayOfYear,
  322. $anIntHour, $anIntMinute, $anIntSecond, $class);
  323. }
  324. /**
  325. * Create a new instance.
  326. *
  327. * @param integer $anIntYear
  328. * @param integer $anIntDayOfYear
  329. * @param integer $anIntHour
  330. * @param integer $anIntMinute
  331. * @param integer $anIntSecond
  332. * @param object Duration $aDurationOffset
  333. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  334. * This parameter is used to get around the limitations of not being
  335. * able to find the class of the object that recieved the initial
  336. * method call.
  337. * @return object StorableTime
  338. * @access public
  339. * @static
  340. * @since 5/4/05
  341. */
  342. function withYearDayHourMinuteSecondOffset ( $anIntYear, $anIntDayOfYear,
  343. $anIntHour, $anIntMinute, $anIntSecond, $aDurationOffset, $class = 'StorableTime' )
  344. {
  345. return parent::withYearDayHourMinuteSecondOffset ( $anIntYear, $anIntDayOfYear,
  346. $anIntHour, $anIntMinute, $anIntSecond, $aDurationOffset, $class);
  347. }
  348. /**
  349. * Create a new instance.
  350. *
  351. * @param integer $anIntYear
  352. * @param integer $anIntOrStringMonth
  353. * @param integer $anIntDay
  354. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  355. * This parameter is used to get around the limitations of not being
  356. * able to find the class of the object that recieved the initial
  357. * method call.
  358. * @access public
  359. * @return object StorableTime
  360. * @static
  361. * @since 5/4/05
  362. */
  363. function withYearMonthDay ( $anIntYear, $anIntOrStringMonth, $anIntDay,
  364. $class = 'StorableTime' )
  365. {
  366. return parent::withYearMonthDay ( $anIntYear, $anIntOrStringMonth, $anIntDay,
  367. $class);
  368. }
  369. /**
  370. * Create a new instance.
  371. *
  372. * @param integer $anIntYear
  373. * @param integer $anIntOrStringMonth
  374. * @param integer $anIntDay
  375. * @param integer $anIntHour
  376. * @param integer $anIntMinute
  377. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  378. * This parameter is used to get around the limitations of not being
  379. * able to find the class of the object that recieved the initial
  380. * method call.
  381. * @return object StorableTime
  382. * @access public
  383. * @static
  384. * @since 5/4/05
  385. */
  386. function withYearMonthDayHourMinute ( $anIntYear, $anIntOrStringMonth,
  387. $anIntDay, $anIntHour, $anIntMinute, $class = 'StorableTime' )
  388. {
  389. return parent::withYearMonthDayHourMinute ( $anIntYear, $anIntOrStringMonth,
  390. $anIntDay, $anIntHour, $anIntMinute, $class);
  391. }
  392. /**
  393. * Create a new instance.
  394. *
  395. * @param integer $anIntYear
  396. * @param integer $anIntOrStringMonth
  397. * @param integer $anIntDay
  398. * @param integer $anIntHour
  399. * @param integer $anIntMinute
  400. * @param integer $anIntSecond
  401. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  402. * This parameter is used to get around the limitations of not being
  403. * able to find the class of the object that recieved the initial
  404. * method call.
  405. * @return object StorableTime
  406. * @access public
  407. * @static
  408. * @since 5/4/05
  409. */
  410. function withYearMonthDayHourMinuteSecond ( $anIntYear, $anIntOrStringMonth,
  411. $anIntDay, $anIntHour, $anIntMinute, $anIntSecond, $class = 'StorableTime' )
  412. {
  413. return parent::withYearMonthDayHourMinuteSecond ( $anIntYear, $anIntOrStringMonth,
  414. $anIntDay, $anIntHour, $anIntMinute, $anIntSecond, $class);
  415. }
  416. /**
  417. * Create a new instance.
  418. *
  419. * @param integer $anIntYear
  420. * @param integer $anIntOrStringMonth
  421. * @param integer $anIntDay
  422. * @param integer $anIntHour
  423. * @param integer $anIntMinute
  424. * @param integer $anIntSecond
  425. * @param object Duration $aDurationOffset
  426. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  427. * This parameter is used to get around the limitations of not being
  428. * able to find the class of the object that recieved the initial
  429. * method call.
  430. * @return object StorableTime
  431. * @access public
  432. * @static
  433. * @since 5/4/05
  434. */
  435. function withYearMonthDayHourMinuteSecondOffset ( $anIntYear,
  436. $anIntOrStringMonth, $anIntDay, $anIntHour, $anIntMinute,
  437. $anIntSecond, $aDurationOffset, $class = 'StorableTime' )
  438. {
  439. return parent::withYearMonthDayHourMinuteSecondOffset ( $anIntYear,
  440. $anIntOrStringMonth, $anIntDay, $anIntHour, $anIntMinute,
  441. $anIntSecond, $aDurationOffset, $class);
  442. }
  443. /**
  444. * Answer a new instance representing yesterday
  445. *
  446. * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE.
  447. * This parameter is used to get around the limitations of not being
  448. * able to find the class of the object that recieved the initial
  449. * method call.
  450. * @return object StorableTime
  451. * @access public
  452. * @since 5/12/05
  453. * @static
  454. */
  455. function yesterday ( $class = 'StorableTime' ) {
  456. return parent::yesterday($class);
  457. }
  458. /*******************************************************
  459. * Instance Methods
  460. *********************************************************/
  461.  
  462. /**
  463. * Takes an existing {@link SelectQuery} and adds a table join and some columns so that
  464. * when it is executed the actual data can be retrieved from the row. The join condition must
  465. * be "fk_data = data_id_field", since the field "fk_data" is already part of the DataManager's
  466. * table structure.
  467. * @access public
  468. * @return void
  469. */
  470. function alterQuery( $query ) {
  471. $query->addTable("dm_time",LEFT_JOIN,"dm_time.id = fk_data");
  472. $query->addColumn("jdn","time_jdn","dm_time");
  473. $query->addColumn("seconds","time_seconds","dm_time");
  474. }
  475. /**
  476. * Inserts a new row into the Database with the data contained in the object.
  477. * @param integer $dbID The {@link DBHandler} database ID to query.
  478. * @access public
  479. * @return integer Returns the new ID of the data stored.
  480. */
  481. function insert($dbID) {
  482. $idManager = Services::getService("Id");
  483. $newID =$idManager->createId();
  484. $query = new InsertQuery();
  485. $query->setTable("dm_time");
  486. $query->setColumns(array("id","jdn", "seconds"));
  487. $dbHandler = Services::getService("DatabaseManager");
  488. // Convert to UTC for storage
  489. $utc =$this->asUTC();
  490. $utcTime =$utc->asTime();
  491. $query->addRowOfValues(array(
  492. "'".addslashes($newID->getIdString())."'",
  493. "'".addslashes($utc->julianDayNumber())."'",
  494. "'".addslashes($utcTime->asSeconds())."'"));
  495. $result =$dbHandler->query($query, $dbID);
  496. if (!$result || $result->getNumberOfRows() != 1) {
  497. throwError( new UnknownDBError("StorableTime") );
  498. return false;
  499. }
  500. return $newID->getIdString();
  501. }
  502. /**
  503. * Uses the ID passed and updates the database row with
  504. * new data.
  505. * @param integer $dbID The {@link DBHandler} database ID to query.
  506. * @param integer $dataID The ID in the database of the data to be updated.
  507. * @access public
  508. * @return void
  509. */
  510. function update($dbID, $dataID) {
  511. if (!$dataID) return false;
  512. $query = new UpdateQuery();
  513. $query->setTable("dm_time");
  514. $query->setColumns(array("jdn", "seconds"));
  515. $query->setWhere("id='".addslashes($dataID)."'");
  516. // Convert to UTC for storage
  517. $utc =$this->asUTC();
  518. $utcTime =$utc->asTime();
  519. $query->setValues(array(
  520. "'".addslashes($utc->julianDayNumber())."'",
  521. "'".addslashes($utcTime->asSeconds())."'"));
  522. $dbHandler = Services::getService("DatabaseManager");
  523. $result =$dbHandler->query($query, $dbID);
  524. if (!$result) {
  525. throwError( new UnknownDBError("StorableTime") );
  526. return false;
  527. }
  528. return true;
  529. }
  530. /**
  531. * Deletes the data row from the appropriate table.
  532. * @param integer $dbID The {@link DBHandler} database ID to query.
  533. * @param integer $dataID The ID in the database of the data to be deleted.
  534. * @access public
  535. * @return void
  536. */
  537. function prune($dbID, $dataID) {
  538. if (!$dataID) return;
  539. // delete ourselves from our data table
  540. $query = new DeleteQuery;
  541. $query->setTable("dm_time");
  542. $query->setWhere("id='".addslashes($dataID)."'");
  543. $dbHandler = Services::getService("DatabaseManager");
  544. $res =$dbHandler->query($query, $dbID);
  545. if (!$res) throwError( new UnknownDBError("StorablePrimitive"));
  546. }
  547.  
  548. /*******************************************************
  549. * Conversion Methods
  550. *********************************************************/
  551.  
  552. /**
  553. * Convert this object to a StorableBlob
  554. *
  555. * @return object
  556. * @access public
  557. * @since 6/9/06
  558. */
  559. function asABlob () {
  560. return Blob::fromString($this->asString());
  561. }
  562. /**
  563. * Convert this object to a StorableString
  564. *
  565. * @return object
  566. * @access public
  567. * @since 6/9/06
  568. */
  569. function asAString () {
  570. return String::fromString($this->asString());
  571. }
  572. /**
  573. * Convert this object to a StorableShortString
  574. *
  575. * @return object
  576. * @access public
  577. * @since 6/9/06
  578. */
  579. function asAShortString () {
  580. return String::fromString($this->asString());
  581. }
  582. /**
  583. * Convert this object to a StorableTime
  584. *
  585. * @return object
  586. * @access public
  587. * @since 6/9/06
  588. */
  589. function asADateTime () {
  590. return $this;
  591. }
  592. /**
  593. * Convert this object to a StorableInteger
  594. *
  595. * @return object
  596. * @access public
  597. * @since 6/9/06
  598. */
  599. function asAInteger() {
  600. $tstamp =$this->asTimestamp();
  601. return Integer::withValue($tstamp->asUnixTimeStamp());
  602. }
  603. /**
  604. * Convert this object to a StorableFloat
  605. *
  606. * @return object
  607. * @access public
  608. * @since 6/9/06
  609. */
  610. function asAFloat () {
  611. $tstamp =$this->asTimestamp();
  612. return Float::withValue($tstamp->asUnixTimeStamp());
  613. }
  614. }

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