PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/ezlocale/classes/ezdatetime.php

https://github.com/zerustech/ezpublish
PHP | 502 lines | 295 code | 39 blank | 168 comment | 62 complexity | d95138af7beff4d2fa94a0dc86d0fed0 MD5 | raw file
  1. <?php
  2. /**
  3. * File containing the eZDateTime class.
  4. *
  5. * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  6. * @license For full copyright and license information view LICENSE file distributed with this source code.
  7. * @version //autogentag//
  8. * @package lib
  9. */
  10. /*!
  11. \class eZDateTime ezdatetime.php
  12. \ingroup eZLocale
  13. \brief Locale aware date and time handler
  14. eZDateTime handles 24 hour time values in hours, minutes and seconds
  15. and date values.
  16. The datetime stored as a timestamp with the number of seconds since the epoch.
  17. See PHP function date() and time() for more information.
  18. A new instance of eZDateTime will automaticly use the current locale and current datetime,
  19. if you however want a different locale use the setLocale() function. The current locale can be
  20. fetched with locale().
  21. Change the time directly with setHour(), setMinute(), setSecond() and setHMS().
  22. Change the date directly with setYear(), setMonth(), setDay() and setMDY().
  23. You can also adjust the date time relative to it's current value by using
  24. adjustDateTime(). Use timeStamp() to get the current timestamp value or
  25. year(), month(), day(), hour(), minute() and second() for the respective
  26. values.
  27. When creating new datetimes you're advised to use the static create()
  28. function which returns a new eZDateTime object. You can also create a copy
  29. with the duplicate() function.
  30. Time checking is done with the isGreaterThan() and isEqualTo() functions.
  31. Text output is done with toString() which can return a long string (default) or
  32. short string representation according to the current locale.
  33. Example:
  34. \code
  35. $us_locale = eZLocale::instance( 'us' );
  36. $dt1 = new eZDateTime();
  37. $dt2 = eZDateTime::create();
  38. $dt2->setLocale( $us_locale );
  39. $dt2->adjustDateTime( -8, 0, 0, 1, 2, 3 );
  40. $dt3 = $dt1->duplicate();
  41. print( $dt1->toString() );
  42. print( $dt2->toString( true ) );
  43. print( $dt1->isEqualTo( $dt3 ) ? 'true' : 'false' ); // Prints 'true'
  44. \endcode
  45. \sa eZDate, eZTime, eZLocale
  46. */
  47. class eZDateTime
  48. {
  49. /**
  50. * Creates a new datetime object with default locale, if $datetime is not supplied the current datetime is used.
  51. *
  52. * @param eZDate|eZTime|bool $datetime
  53. */
  54. public function __construct( $datetime = false )
  55. {
  56. if ( $datetime instanceof eZDate )
  57. {
  58. $arr = getdate( $datetime->timeStamp() );
  59. $arr2 = getdate( $this->DateTime );
  60. $datetime = mktime( $arr2['hours'], $arr2['minutes'], $arr2['seconds'],
  61. $arr['mon'], $arr['mday'], $arr['year'] );
  62. }
  63. else if ( $datetime instanceof eZTime )
  64. {
  65. $arr2 = getdate( $datetime->timeStamp() );
  66. $arr = getdate( $this->DateTime );
  67. $datetime = mktime( $arr2['hours'], $arr2['minutes'], $arr2['seconds'],
  68. $arr['mon'], $arr['mday'], $arr['year'] );
  69. }
  70. else if ( $datetime === false )
  71. {
  72. $datetime = time();
  73. }
  74. $this->DateTime = intval( $datetime );
  75. $this->Locale = eZLocale::instance();
  76. $this->IsValid = $datetime !== null;
  77. }
  78. function attributes()
  79. {
  80. return array( 'timestamp',
  81. 'hour',
  82. 'minute',
  83. 'second',
  84. 'year',
  85. 'month',
  86. 'day',
  87. 'is_valid' );
  88. }
  89. function hasAttribute( $name )
  90. {
  91. return in_array( $name, $this->attributes() );
  92. }
  93. function attribute( $name )
  94. {
  95. if ( $name == 'timestamp' )
  96. {
  97. return $this->timeStamp();
  98. }
  99. else if ( $name == 'hour' )
  100. {
  101. return $this->hour();
  102. }
  103. else if ( $name == 'minute' )
  104. {
  105. return $this->minute();
  106. }
  107. else if ( $name == 'second' )
  108. {
  109. return $this->second();
  110. }
  111. else if ( $name == 'day' )
  112. {
  113. return $this->day();
  114. }
  115. else if ( $name == 'year' )
  116. {
  117. return $this->year();
  118. }
  119. else if ( $name == 'month' )
  120. {
  121. return $this->month();
  122. }
  123. else if ( $name == 'is_valid' )
  124. {
  125. return $this->isValid();
  126. }
  127. else
  128. {
  129. eZDebug::writeError( "Attribute '$name' does not exist", __METHOD__ );
  130. return false;
  131. }
  132. }
  133. /*!
  134. \return true if the date has valid data.
  135. */
  136. function isValid()
  137. {
  138. return $this->IsValid;
  139. }
  140. /*!
  141. Sets the locale to $locale which is used in text output.
  142. */
  143. function setLocale( $locale )
  144. {
  145. $this->Locale = $locale;
  146. }
  147. /*!
  148. Returns the current locale.
  149. */
  150. function locale()
  151. {
  152. return $this->Locale;
  153. }
  154. /*!
  155. Returns the current time zone.
  156. */
  157. function timeZone()
  158. {
  159. return date( 'T', $this->DateTime );
  160. }
  161. /*!
  162. Returns the timestamp value, this is the number of seconds since the epoch.
  163. \note The value is returned as a reference and should not be modified.
  164. */
  165. function timeStamp( )
  166. {
  167. return $this->DateTime;
  168. }
  169. function setTimeStamp( $stamp )
  170. {
  171. $this->DateTime = $stamp;
  172. $this->IsValid = $stamp !== null;
  173. }
  174. /*!
  175. \static
  176. Returns the current date and time as a UNIX timestamp
  177. */
  178. static function currentTimeStamp()
  179. {
  180. return time();
  181. }
  182. /*!
  183. Creates an eZDate object of this datetime with the same date and locale.
  184. Returns a reference to the object.
  185. */
  186. function toDate()
  187. {
  188. $date = new eZDate( $this->DateTime );
  189. $date->setLocale( $this->Locale );
  190. return $date;
  191. }
  192. /*!
  193. Creates an eZTime object of this datetime with the same time and locale.
  194. Returns a reference to the object.
  195. */
  196. function toTime()
  197. {
  198. $time = new eZTime( $this->DateTime );
  199. $time->setLocale( $this->Locale );
  200. return $time;
  201. }
  202. /*!
  203. Returns the year element.
  204. */
  205. function year()
  206. {
  207. return date( 'Y', $this->DateTime );
  208. }
  209. /*!
  210. Returns the month element.
  211. */
  212. function month()
  213. {
  214. return date( 'm', $this->DateTime );
  215. }
  216. /*!
  217. Returns the day element.
  218. */
  219. function day()
  220. {
  221. return date( 'd', $this->DateTime );
  222. }
  223. /*!
  224. Returns the hour element.
  225. */
  226. function hour()
  227. {
  228. return date( 'G', $this->DateTime );
  229. }
  230. /*!
  231. Returns the minute element.
  232. */
  233. function minute()
  234. {
  235. return date( 'i', $this->DateTime );
  236. }
  237. /*!
  238. Returns the second element.
  239. */
  240. function second()
  241. {
  242. return date( 's', $this->DateTime );
  243. }
  244. /*!
  245. Sets the year leaving the other elements untouched.
  246. */
  247. function setYear( $year )
  248. {
  249. $arr = getdate( $this->DateTime );
  250. $this->DateTime = mktime( $arr['hours'], $arr['minutes'], $arr['seconds'],
  251. $arr['mon'], $arr['mday'], $year );
  252. }
  253. /*!
  254. Sets the month leaving the other elements untouched.
  255. */
  256. function setMonth( $month )
  257. {
  258. $arr = getdate( $this->DateTime );
  259. $this->DateTime = mktime( $arr['hours'], $arr['minutes'], $arr['seconds'],
  260. $month, $arr['mday'], $arr['year'] );
  261. }
  262. /*!
  263. Sets the day leaving the other elements untouched.
  264. */
  265. function setDay( $day )
  266. {
  267. $arr = getdate( $this->DateTime );
  268. $this->DateTime = mktime( $arr['hours'], $arr['minutes'], $arr['seconds'],
  269. $arr['mon'], $day, $arr['year'] );
  270. }
  271. /*!
  272. Sets the hour leaving the other elements untouched.
  273. */
  274. function setHour( $hour )
  275. {
  276. $arr = getdate( $this->DateTime );
  277. $this->DateTime = mktime( $hour, $arr['minutes'], $arr['seconds'],
  278. $arr['mon'], $arr['mday'], $arr['year'] );
  279. }
  280. /*!
  281. Sets the minute leaving the other elements untouched.
  282. */
  283. function setMinute( $min )
  284. {
  285. $arr = getdate( $this->DateTime );
  286. $this->DateTime = mktime( $arr['hours'], $min, $arr['seconds'],
  287. $arr['mon'], $arr['mday'], $arr['year'] );
  288. }
  289. /*!
  290. Sets the second leaving the other elements untouched.
  291. */
  292. function setSecond( $sec )
  293. {
  294. $arr = getdate( $this->DateTime );
  295. $this->DateTime = mktime( $arr['hours'], $arr['minutes'], $sec,
  296. $arr['mon'], $arr['mday'], $arr['year'] );
  297. }
  298. /*!
  299. Sets all hour, minute and second elements leaving the other elements untouched.
  300. */
  301. function setHMS( $hour, $min = 0, $sec = 0 )
  302. {
  303. $arr = getdate( $this->DateTime );
  304. $this->DateTime = mktime( $hour, $min, $sec,
  305. $arr['mon'], $arr['mday'], $arr['year'] );
  306. }
  307. /*!
  308. Sets all hour, minute and second elements leaving the other elements untouched.
  309. */
  310. function setMDYHMS( $month, $day, $year, $hour, $min, $sec = 0 )
  311. {
  312. $this->DateTime = mktime( $hour, $min, $sec, $month, $day, $year );
  313. }
  314. /*!
  315. Sets the year, month and day elements. If $day or $year is omitted or set 0
  316. they will get a value taken from the current time.
  317. */
  318. function setMDY( $month, $day = 0, $year = 0 )
  319. {
  320. $arr = getdate( $this->DateTime );
  321. if ( $year != 0 )
  322. $date = mktime( $arr['hours'], $arr['minutes'], $arr['seconds'],
  323. $month, $day, $year );
  324. else if ( $day != 0 )
  325. $date = mktime( $arr['hours'], $arr['minutes'], $arr['seconds'],
  326. $month, $day );
  327. else
  328. $date = mktime( $arr['hours'], $arr['minutes'], $arr['seconds'],
  329. $month );
  330. $this->DateTime = $date;
  331. }
  332. /*!
  333. Adjusts the datetime relative to it's current value. This is useful for adding/subtracting
  334. hours, minutes, seconds, years, months or days to an existing datetime.
  335. */
  336. function adjustDateTime( $hour, $minute = 0, $second = 0, $month = 0, $day = 0, $year = 0 )
  337. {
  338. $arr = getdate( $this->DateTime );
  339. $date = mktime( $hour + $arr['hours'], $minute + $arr['minutes'], $second + $arr['seconds'],
  340. $month + $arr['mon'], $day + $arr['mday'], $year + $arr['year'] );
  341. $this->DateTime = $date;
  342. }
  343. /*!
  344. Returns true if this object has a datetime greater than $datetime. $datetime can be specified as
  345. a timestamp value or as an eZDateTime, eZDate or eZTime object. If $equal is true it returns true if
  346. they are equal as well.
  347. \note If $datetime is either eZDate or eZTime it will create temporary objects with toDate() and
  348. toTime() and use these for comparison.
  349. */
  350. function isGreaterThan( &$datetime, $equal = false )
  351. {
  352. if ( $datetime instanceof eZDate )
  353. {
  354. $d1 = $this->toDate();
  355. return $d1->isGreaterThan( $datetime, $equal );
  356. }
  357. else if ( $datetime instanceof eZTime )
  358. {
  359. $t1 = $this->toTime();
  360. return $t1->isGreaterThan( $datetime, $equal );
  361. }
  362. else
  363. {
  364. $dt1 = $this->timeStamp();
  365. $dt2 = $datetime instanceof eZDateTime ? $datetime->timeStamp() : $datetime;
  366. if ( $dt1 > $dt2 )
  367. return true;
  368. else if ( $equal and $dt1 == $dt2 )
  369. return true;
  370. else
  371. return false;
  372. }
  373. }
  374. /*!
  375. Returns true if this object is equal to $date. $date can be specified as
  376. a timestamp value or as an eZDateTime, eZDate or eZTime object.
  377. \note If $datetime is either eZDate or eZTime it will create temporary objects with toDate() and
  378. toTime() and use these for comparison.
  379. */
  380. function isEqualTo( &$datetime )
  381. {
  382. if ( $datetime instanceof eZDate )
  383. {
  384. $d1 = $this->toDate();
  385. return $d1->isEqualTo( $datetime );
  386. }
  387. else if ( $datetime instanceof eZTime )
  388. {
  389. $t1 = $this->toTime();
  390. return $t1->isEqualTo( $datetime );
  391. }
  392. else
  393. {
  394. $dt1 = $this->timeStamp();
  395. $dt2 = $datetime instanceof eZDateTime ? $datetime->timeStamp() : $datetime;
  396. return $dt1 == $dt2;
  397. }
  398. }
  399. /*!
  400. Creates a new eZDate object with the time values $hour, $minute and $second,
  401. date values $month, $day and $year and returns a reference to it.
  402. Any value can be ommitted or set to -1 to use the current date or time value.
  403. */
  404. static function create( $hour = -1, $minute = -1, $second = -1, $month = -1, $day = -1, $year = -1 )
  405. {
  406. if ( $year != -1 )
  407. $datetime = mktime( $hour, $minute, $second, $month, $day, $year );
  408. else if ( $day != -1 )
  409. $datetime = mktime( $hour, $minute, $second, $month, $day );
  410. else if ( $month != -1 )
  411. $datetime = mktime( $hour, $minute, $second, $month );
  412. else if ( $second != -1 )
  413. $datetime = mktime( $hour, $minute, $second );
  414. else if ( $minute != -1 )
  415. $datetime = mktime( $hour, $minute );
  416. else if ( $hour != -1 )
  417. $datetime = mktime( $hour );
  418. else
  419. $datetime = time();
  420. return new eZDateTime( $datetime );
  421. }
  422. /*!
  423. \deprecated This function is deprecated in PHP5, use the PHP5 clone keyword instead
  424. Creates an exact copy of this object and returns it.
  425. */
  426. function duplicate()
  427. {
  428. $copy = clone $this;
  429. return $copy;
  430. }
  431. /*!
  432. Creates a string representation of the date using the current locale and returns it.
  433. If $short is true a short representation is used.
  434. */
  435. function toString( $short = false )
  436. {
  437. if ( $short )
  438. $str = $this->Locale->formatShortDate( $this->DateTime ) . ' ' .
  439. $this->Locale->formatShortTime( $this->DateTime );
  440. else
  441. $str = $this->Locale->formatDate( $this->DateTime ) . ' ' .
  442. $this->Locale->formatTime( $this->DateTime );
  443. return $str;
  444. }
  445. /// Locale object, is just a reference to minimize memory usage.
  446. public $Locale;
  447. /// The current datetime as a timestamp
  448. public $DateTime;
  449. public $IsValid;
  450. }
  451. ?>