# asctime
$strtime = $ts;
} else {
- throw new TimestampException( __METHOD__ . " : Invalid timestamp - $ts" );
+ throw new TimestampException( __METHOD__ . ": Invalid timestamp - $ts" );
}
if ( !$strtime ) {
try {
$final = new DateTime( $strtime, new DateTimeZone( 'GMT' ) );
- } catch( Exception $e ) {
- throw new TimestampException( __METHOD__ . ' Invalid timestamp format.' );
+ } catch ( Exception $e ) {
+ throw new TimestampException( __METHOD__ . ': Invalid timestamp format.', $e->getCode(), $e );
}
if ( $final === false ) {
- throw new TimestampException( __METHOD__ . ' Invalid timestamp format.' );
+ throw new TimestampException( __METHOD__ . ': Invalid timestamp format.' );
}
$this->timestamp = $final;
}
*/
public function getTimestamp( $style = TS_UNIX ) {
if ( !isset( self::$formats[$style] ) ) {
- throw new TimestampException( __METHOD__ . ' : Illegal timestamp output type.' );
+ throw new TimestampException( __METHOD__ . ': Illegal timestamp output type.' );
}
$output = $this->timestamp->format( self::$formats[$style] );
* @return DateInterval Offset that was applied to the timestamp
*/
public function offsetForUser( User $user ) {
- global $wgLocalTZOffset;
+ global $wgLocalTZoffset;
$option = $user->getOption( 'timecorrection' );
$data = explode( '|', $option, 3 );
// first value.
if ( $data[0] == 'System' ) {
// First value is System, so use the system offset.
- if ( isset( $wgLocalTZOffset ) ) {
- $diff = $wgLocalTZOffset;
+ if ( isset( $wgLocalTZoffset ) ) {
+ $diff = $wgLocalTZoffset;
}
} elseif ( $data[0] == 'Offset' ) {
// First value is Offset, so use the specified offset
if ( count( $data ) >= 2 ) {
// Combination hours and minutes.
$diff = abs( (int)$data[0] ) * 60 + (int)$data[1];
- if ( (int) $data[0] < 0 ) {
+ if ( (int)$data[0] < 0 ) {
$diff *= -1;
}
} else {
}
}
- $interval = new DateInterval('PT' . abs( $diff ) . 'M');
+ $interval = new DateInterval( 'PT' . abs( $diff ) . 'M' );
if ( $diff < 1 ) {
$interval->invert = 1;
}
public function diff( MWTimestamp $relativeTo ) {
return $this->timestamp->diff( $relativeTo->timestamp );
}
+
+ /**
+ * Set the timezone of this timestamp to the specified timezone.
+ *
+ * @since 1.22
+ * @param String $timezone Timezone to set
+ * @throws TimestampException
+ */
+ public function setTimezone( $timezone ) {
+ try {
+ $this->timestamp->setTimezone( new DateTimeZone( $timezone ) );
+ } catch ( Exception $e ) {
+ throw new TimestampException( __METHOD__ . ': Invalid timezone.', $e->getCode(), $e );
+ }
+ }
+
+ /**
+ * Get the timezone of this timestamp.
+ *
+ * @since 1.22
+ * @return DateTimeZone The timezone
+ */
+ public function getTimezone() {
+ return $this->timestamp->getTimezone();
+ }
+
+ /**
+ * Format the timestamp in a given format.
+ *
+ * @since 1.22
+ * @param string $format Pattern to format in
+ * @return string The formatted timestamp
+ */
+ public function format( $format ) {
+ return $this->timestamp->format( $format );
+ }
+
+ /**
+ * Get a timestamp instance in the server local timezone ($wgLocaltimezone)
+ *
+ * @since 1.22
+ * @param bool|string $ts Timestamp to set, or false for current time
+ * @return MWTimestamp the local instance
+ */
+ public static function getLocalInstance( $ts = false ) {
+ global $wgLocaltimezone;
+ $timestamp = new self( $ts );
+ $timestamp->setTimezone( $wgLocaltimezone );
+ return $timestamp;
+ }
+
+ /**
+ * Get a timestamp instance in GMT
+ *
+ * @since 1.22
+ * @param bool|string $ts Timestamp to set, or false for current time
+ * @return MWTimestamp the instance
+ */
+ public static function getInstance( $ts = false ) {
+ return new self( $ts );
+ }
}
/**