X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FMWTimestamp.php;h=defdc967775c1f3b8731d407d7f79dd70ade4182;hb=67cef9f0176a1cddbeb52b9970900ec542a2842e;hp=f2bd6ba56919463e03918b3d8e6e847c0146fed0;hpb=0e1c80e6e178be646e8deb3fa64fac7e7219938b;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/MWTimestamp.php b/includes/MWTimestamp.php index f2bd6ba569..defdc96777 100644 --- a/includes/MWTimestamp.php +++ b/includes/MWTimestamp.php @@ -32,7 +32,7 @@ class MWTimestamp { /** * Standard gmdate() formats for the different timestamp types. */ - private static $formats = array( + private static $formats = [ TS_UNIX => 'U', TS_MW => 'YmdHis', TS_DB => 'Y-m-d H:i:s', @@ -42,7 +42,7 @@ class MWTimestamp { TS_RFC2822 => 'D, d M Y H:i:s', TS_ORACLE => 'd-m-Y H:i:s.000000', // Was 'd-M-y h.i.s A' . ' +00:00' before r51500 TS_POSTGRES => 'Y-m-d H:i:s', - ); + ]; /** * The actual timestamp being wrapped (DateTime object). @@ -56,10 +56,14 @@ class MWTimestamp { * * @since 1.20 * - * @param bool|string $timestamp Timestamp to set, or false for current time + * @param bool|string|int|float|DateTime $timestamp Timestamp to set, or false for current time */ public function __construct( $timestamp = false ) { - $this->setTimestamp( $timestamp ); + if ( $timestamp instanceof DateTime ) { + $this->timestamp = $timestamp; + } else { + $this->setTimestamp( $timestamp ); + } } /** @@ -74,7 +78,8 @@ class MWTimestamp { * @throws TimestampException */ public function setTimestamp( $ts = false ) { - $da = array(); + $m = []; + $da = []; $strtime = ''; // We want to catch 0, '', null... but not date strings starting with a letter. @@ -87,9 +92,9 @@ class MWTimestamp { # TS_EXIF } elseif ( preg_match( '/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/D', $ts, $da ) ) { # TS_MW - } elseif ( preg_match( '/^-?\d{1,13}$/D', $ts ) ) { + } elseif ( preg_match( '/^(-?\d{1,13})(\.\d+)?$/D', $ts, $m ) ) { # TS_UNIX - $strtime = "@$ts"; // http://php.net/manual/en/datetime.formats.compound.php + $strtime = "@{$m[1]}"; // http://php.net/manual/en/datetime.formats.compound.php } elseif ( preg_match( '/^\d{2}-\d{2}-\d{4} \d{2}:\d{2}:\d{2}.\d{6}$/', $ts ) ) { # TS_ORACLE // session altered to DD-MM-YYYY HH24:MI:SS.FF6 $strtime = preg_replace( '/(\d\d)\.(\d\d)\.(\d\d)(\.(\d+))?/', "$1:$2:$3", @@ -105,7 +110,7 @@ class MWTimestamp { $ts, $da ) ) { - #TS_ISO_8601_BASIC + # TS_ISO_8601_BASIC } elseif ( preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)\.*\d*[\+\- ](\d\d)$/', $ts, @@ -202,11 +207,15 @@ class MWTimestamp { * @deprecated since 1.26 Use Language::getHumanTimestamp directly * * @param MWTimestamp|null $relativeTo The base timestamp to compare to (defaults to now) - * @param User|null $user User the timestamp is being generated for (or null to use main context's user) - * @param Language|null $lang Language to use to make the human timestamp (or null to use main context's language) + * @param User|null $user User the timestamp is being generated for + * (or null to use main context's user) + * @param Language|null $lang Language to use to make the human timestamp + * (or null to use main context's language) * @return string Formatted timestamp */ - public function getHumanTimestamp( MWTimestamp $relativeTo = null, User $user = null, Language $lang = null ) { + public function getHumanTimestamp( + MWTimestamp $relativeTo = null, User $user = null, Language $lang = null + ) { if ( $lang === null ) { $lang = RequestContext::getMain()->getLanguage(); } @@ -294,7 +303,7 @@ class MWTimestamp { MWTimestamp $relativeTo = null, User $user = null, Language $lang = null, - array $chosenIntervals = array() + array $chosenIntervals = [] ) { if ( $relativeTo === null ) { $relativeTo = new self; @@ -310,7 +319,7 @@ class MWTimestamp { $diff = $this->diff( $relativeTo ); if ( Hooks::run( 'GetRelativeTimestamp', - array( &$ts, &$diff, $this, $relativeTo, $user, $lang ) + [ &$ts, &$diff, $this, $relativeTo, $user, $lang ] ) ) { $seconds = ( ( ( $diff->days * 24 + $diff->h ) * 60 + $diff->i ) * 60 + $diff->s ); $ts = wfMessage( 'ago', $lang->formatDuration( $seconds, $chosenIntervals ) ) @@ -366,6 +375,26 @@ class MWTimestamp { return $this->timestamp->getTimezone(); } + /** + * Get the localized timezone message, if available. + * + * Premade translations are not shipped as format() may return whatever the + * system uses, localized or not, so translation must be done through wiki. + * + * @since 1.27 + * @return Message The localized timezone message + */ + public function getTimezoneMessage() { + $tzMsg = $this->format( 'T' ); // might vary on DST changeover! + $key = 'timezone-' . strtolower( trim( $tzMsg ) ); + $msg = wfMessage( $key ); + if ( $msg->exists() ) { + return $msg; + } else { + return new RawMessage( $tzMsg ); + } + } + /** * Format the timestamp in a given format. *