MWTimestamp.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * Creation and parsing of MW-style timestamps.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @since 1.20
  22. * @author Tyler Romeo, 2012
  23. */
  24. use Wikimedia\Timestamp\ConvertibleTimestamp;
  25. /**
  26. * Library for creating and parsing MW-style timestamps. Based on the JS
  27. * library that does the same thing.
  28. *
  29. * @since 1.20
  30. */
  31. class MWTimestamp extends ConvertibleTimestamp {
  32. /**
  33. * Get a timestamp instance in GMT
  34. *
  35. * @param bool|string $ts Timestamp to set, or false for current time
  36. * @return MWTimestamp The instance
  37. */
  38. public static function getInstance( $ts = false ) {
  39. return new static( $ts );
  40. }
  41. /**
  42. * Get the timestamp in a human-friendly relative format, e.g., "3 days ago".
  43. *
  44. * Determine the difference between the timestamp and the current time, and
  45. * generate a readable timestamp by returning "<N> <units> ago", where the
  46. * largest possible unit is used.
  47. *
  48. * @since 1.20
  49. * @since 1.22 Uses Language::getHumanTimestamp to produce the timestamp
  50. * @deprecated since 1.26 Use Language::getHumanTimestamp directly
  51. *
  52. * @param MWTimestamp|null $relativeTo The base timestamp to compare to (defaults to now)
  53. * @param User|null $user User the timestamp is being generated for
  54. * (or null to use main context's user)
  55. * @param Language|null $lang Language to use to make the human timestamp
  56. * (or null to use main context's language)
  57. * @return string Formatted timestamp
  58. */
  59. public function getHumanTimestamp(
  60. MWTimestamp $relativeTo = null, User $user = null, Language $lang = null
  61. ) {
  62. if ( $lang === null ) {
  63. $lang = RequestContext::getMain()->getLanguage();
  64. }
  65. return $lang->getHumanTimestamp( $this, $relativeTo, $user );
  66. }
  67. /**
  68. * Adjust the timestamp depending on the given user's preferences.
  69. *
  70. * @since 1.22
  71. *
  72. * @param User $user User to take preferences from
  73. * @return DateInterval Offset that was applied to the timestamp
  74. */
  75. public function offsetForUser( User $user ) {
  76. global $wgLocalTZoffset;
  77. $option = $user->getOption( 'timecorrection' );
  78. $data = explode( '|', $option, 3 );
  79. // First handle the case of an actual timezone being specified.
  80. if ( $data[0] == 'ZoneInfo' ) {
  81. try {
  82. $tz = new DateTimeZone( $data[2] );
  83. } catch ( Exception $e ) {
  84. $tz = false;
  85. }
  86. if ( $tz ) {
  87. $this->timestamp->setTimezone( $tz );
  88. return new DateInterval( 'P0Y' );
  89. }
  90. $data[0] = 'Offset';
  91. }
  92. $diff = 0;
  93. // If $option is in fact a pipe-separated value, check the
  94. // first value.
  95. if ( $data[0] == 'System' ) {
  96. // First value is System, so use the system offset.
  97. if ( $wgLocalTZoffset !== null ) {
  98. $diff = $wgLocalTZoffset;
  99. }
  100. } elseif ( $data[0] == 'Offset' ) {
  101. // First value is Offset, so use the specified offset
  102. $diff = (int)$data[1];
  103. } else {
  104. // $option actually isn't a pipe separated value, but instead
  105. // a comma separated value. Isn't MediaWiki fun?
  106. $data = explode( ':', $option );
  107. if ( count( $data ) >= 2 ) {
  108. // Combination hours and minutes.
  109. $diff = abs( (int)$data[0] ) * 60 + (int)$data[1];
  110. if ( (int)$data[0] < 0 ) {
  111. $diff *= -1;
  112. }
  113. } else {
  114. // Just hours.
  115. $diff = (int)$data[0] * 60;
  116. }
  117. }
  118. $interval = new DateInterval( 'PT' . abs( $diff ) . 'M' );
  119. if ( $diff < 1 ) {
  120. $interval->invert = 1;
  121. }
  122. $this->timestamp->add( $interval );
  123. return $interval;
  124. }
  125. /**
  126. * Generate a purely relative timestamp, i.e., represent the time elapsed between
  127. * the given base timestamp and this object.
  128. *
  129. * @param MWTimestamp|null $relativeTo Relative base timestamp (defaults to now)
  130. * @param User|null $user Use to use offset for
  131. * @param Language|null $lang Language to use
  132. * @param array $chosenIntervals Intervals to use to represent it
  133. * @return string Relative timestamp
  134. */
  135. public function getRelativeTimestamp(
  136. MWTimestamp $relativeTo = null,
  137. User $user = null,
  138. Language $lang = null,
  139. array $chosenIntervals = []
  140. ) {
  141. if ( $relativeTo === null ) {
  142. $relativeTo = new self;
  143. }
  144. if ( $user === null ) {
  145. $user = RequestContext::getMain()->getUser();
  146. }
  147. if ( $lang === null ) {
  148. $lang = RequestContext::getMain()->getLanguage();
  149. }
  150. $ts = '';
  151. $diff = $this->diff( $relativeTo );
  152. if ( Hooks::run(
  153. 'GetRelativeTimestamp',
  154. [ &$ts, &$diff, $this, $relativeTo, $user, $lang ]
  155. ) ) {
  156. $seconds = ( ( ( $diff->days * 24 + $diff->h ) * 60 + $diff->i ) * 60 + $diff->s );
  157. $ts = wfMessage( 'ago', $lang->formatDuration( $seconds, $chosenIntervals ) )
  158. ->inLanguage( $lang )->text();
  159. }
  160. return $ts;
  161. }
  162. /**
  163. * Get the localized timezone message, if available.
  164. *
  165. * Premade translations are not shipped as format() may return whatever the
  166. * system uses, localized or not, so translation must be done through wiki.
  167. *
  168. * @since 1.27
  169. * @return Message The localized timezone message
  170. */
  171. public function getTimezoneMessage() {
  172. $tzMsg = $this->format( 'T' ); // might vary on DST changeover!
  173. $key = 'timezone-' . strtolower( trim( $tzMsg ) );
  174. $msg = wfMessage( $key );
  175. if ( $msg->exists() ) {
  176. return $msg;
  177. }
  178. return new RawMessage( $tzMsg );
  179. }
  180. /**
  181. * Get a timestamp instance in the server local timezone ($wgLocaltimezone)
  182. *
  183. * @since 1.22
  184. * @param bool|string $ts Timestamp to set, or false for current time
  185. * @return MWTimestamp The local instance
  186. */
  187. public static function getLocalInstance( $ts = false ) {
  188. global $wgLocaltimezone;
  189. $timestamp = new self( $ts );
  190. $timestamp->setTimezone( $wgLocaltimezone );
  191. return $timestamp;
  192. }
  193. }