WordPress.org

Make WordPress Core

Changeset 45856


Ignore:
Timestamp:
08/19/2019 09:05:42 PM (4 months ago)
Author:
SergeyBiryukov
Message:

Date/Time: Use PHP DateTime class API in current_time().

Only use the legacy WP timestamp approach (a sum of timestamp and timezone offset) for timestamp and U formats without the $gmt flag.

Otherwise, make sure the function returns correct local time for any format.

Props Rarst, jdgrimes.
Fixes #40653.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.php

    r45854 r45856  
    4747
    4848/**
    49  * Retrieve the current time based on specified type.
     49 * Retrieves the current time based on specified type.
    5050 *
    5151 * The 'mysql' type will return the time in the format for MySQL DATETIME field.
    52  * The 'timestamp' type will return the current timestamp.
     52 * The 'timestamp' type will return the current timestamp or a sum of timestamp
     53 * and timezone offset, depending on `$gmt`.
    5354 * Other strings will be interpreted as PHP date formats (e.g. 'Y-m-d').
    5455 *
     
    6465 */
    6566function current_time( $type, $gmt = 0 ) {
    66     switch ( $type ) {
    67         case 'mysql':
    68             return ( $gmt ) ? gmdate( 'Y-m-d H:i:s' ) : gmdate( 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ) );
    69         case 'timestamp':
    70             return ( $gmt ) ? time() : time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
    71         default:
    72             return ( $gmt ) ? gmdate( $type ) : gmdate( $type, time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) );
    73     }
     67    // Don't use non-GMT timestamp, unless you know the difference and really need to.
     68    if ( 'timestamp' === $type || 'U' === $type ) {
     69        return $gmt ? time() : time() + (int) ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
     70    }
     71
     72    if ( 'mysql' === $type ) {
     73        $type = 'Y-m-d H:i:s';
     74    }
     75
     76    $timezone = $gmt ? new DateTimeZone( 'UTC' ) : wp_timezone();
     77    $datetime = new DateTime( 'now', $timezone );
     78
     79    return $datetime->format( $type );
    7480}
    7581
  • trunk/tests/phpunit/tests/date/currentTime.php

    r43594 r45856  
    77class Tests_Date_CurrentTime extends WP_UnitTestCase {
    88
     9    /**
     10     * @ticket 37440
     11     */
    912    public function test_should_work_with_changed_timezone() {
    10 
    1113        $format          = 'Y-m-d H:i:s';
    1214        $timezone_string = 'America/Regina';
     
    2224        $this->assertEquals( $datetime->format( $format ), current_time( $format ) );
    2325    }
     26
     27    /**
     28     * @ticket 40653
     29     */
     30    public function test_should_return_wp_timestamp() {
     31        update_option( 'timezone_string', 'Europe/Kiev' );
     32        $timestamp = time();
     33        $datetime  = new DateTime( '@' . $timestamp );
     34        $datetime->setTimezone( wp_timezone() );
     35        $wp_timestamp = $timestamp + $datetime->getOffset();
     36
     37        $this->assertEquals( $timestamp, current_time( 'timestamp', true ), '', 2 );
     38        $this->assertEquals( $timestamp, current_time( 'U', true ), '', 2 );
     39        $this->assertEquals( $wp_timestamp, current_time( 'timestamp' ), '', 2 );
     40        $this->assertEquals( $wp_timestamp, current_time( 'U' ), '', 2 );
     41        $this->assertInternalType( 'int', current_time( 'timestamp' ) );
     42    }
     43
     44    /**
     45     * @ticket 40653
     46     */
     47    public function test_should_return_correct_local_time() {
     48        update_option( 'timezone_string', 'Europe/Kiev' );
     49        $timestamp      = time();
     50        $datetime_local = new DateTime( '@' . $timestamp );
     51        $datetime_local->setTimezone( wp_timezone() );
     52        $datetime_utc = new DateTime( '@' . $timestamp );
     53        $datetime_utc->setTimezone( new DateTimeZone( 'UTC' ) );
     54
     55        $this->assertEquals( $datetime_local->format( DATE_W3C ), current_time( DATE_W3C ), '', 2 );
     56        $this->assertEquals( $datetime_utc->format( DATE_W3C ), current_time( DATE_W3C, true ), '', 2 );
     57    }
    2458}
Note: See TracChangeset for help on using the changeset viewer.