MWTimestamp: Allow providing a DateTime object directly
authorKunal Mehta <legoktm@member.fsf.org>
Mon, 22 Aug 2016 02:17:01 +0000 (19:17 -0700)
committerKunal Mehta <legoktm@member.fsf.org>
Mon, 22 Aug 2016 02:17:01 +0000 (19:17 -0700)
For cases where you already have a DateTime object on hand and want to
use MWTimestamp's formatting code. Since MWTimestamp stores DateTime
objects internally, just set it to the $timestamp member variable.

Change-Id: Ie60392e32743d4d082d2c9347ef68418d5eb86ad

includes/MWTimestamp.php

index 4964fe6..defdc96 100644 (file)
@@ -56,10 +56,14 @@ class MWTimestamp {
         *
         * @since 1.20
         *
-        * @param bool|string|int|float $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 );
+               }
        }
 
        /**