WordPress.org

Make WordPress Core

Ticket #40653: current-time1.patch

File current-time1.patch, 3.7 KB (added by Rarst, 7 weeks ago)

Improved test.

  • src/wp-includes/functions.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    4949 * Retrieve 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 and timezone offset, depending on `$gmt`.
    5353 * Other strings will be interpreted as PHP date formats (e.g. 'Y-m-d').
    5454 *
    5555 * If $gmt is set to either '1' or 'true', then both types will use GMT time.
     
    6060 * @param string   $type Type of time to retrieve. Accepts 'mysql', 'timestamp', or PHP date
    6161 *                       format string (e.g. 'Y-m-d').
    6262 * @param int|bool $gmt  Optional. Whether to use GMT timezone. Default false.
     63 *
    6364 * @return int|string Integer if $type is 'timestamp', string otherwise.
    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 ) );
     67
     68    // Don't use non-gmt timestamp, unless you know the difference and really need to.
     69        if ( 'timestamp' === $type || 'U' === $type ) {
     70                return ( $gmt ) ? time() : time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
    7371        }
     72
     73        if ( 'mysql' === $type ) {
     74                $type = 'Y-m-d H:i:s';
     75        }
     76
     77        $timezone = $gmt ? new DateTimeZone( 'UTC' ) : wp_timezone();
     78        $datetime = new DateTime( 'now', $timezone );
     79
     80        return $datetime->format( $type );
    7481}
    7582
    7683/**
  • tests/phpunit/tests/date/currentTime.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    2121                $this->assertEquals( gmdate( $format ), current_time( $format, true ) );
    2222                $this->assertEquals( $datetime->format( $format ), current_time( $format ) );
    2323        }
     24
     25        public function test_should_return_wp_timestamp() {
     26
     27                update_option( 'timezone_string', 'Europe/Kiev' );
     28                $timestamp = time();
     29                $datetime  = new DateTime( '@' . $timestamp );
     30                $datetime->setTimezone( wp_timezone() );
     31                $wp_timestamp = $timestamp + $datetime->getOffset();
     32
     33                $this->assertEquals( $timestamp, current_time( 'timestamp', true ), '', 2 );
     34                $this->assertEquals( $timestamp, current_time( 'U', true ), '', 2 );
     35                $this->assertEquals( $wp_timestamp, current_time( 'timestamp' ), '', 2 );
     36                $this->assertEquals( $wp_timestamp, current_time( 'U' ), '', 2 );
     37        }
     38
     39        public function test_should_return_correct_local_time() {
     40
     41                update_option( 'timezone_string', 'Europe/Kiev' );
     42                $timestamp      = time();
     43                $datetime_local = new DateTime( '@' . $timestamp );
     44                $datetime_local->setTimezone( wp_timezone() );
     45                $datetime_utc = new DateTime( '@' . $timestamp );
     46                $datetime_utc->setTimezone( new DateTimeZone( 'UTC' ) );
     47
     48                $this->assertEquals( $datetime_local->format( DATE_W3C ), current_time( DATE_W3C ), '', 2 );
     49                $this->assertEquals( $datetime_utc->format( DATE_W3C ), current_time( DATE_W3C, true ), '', 2 );
     50        }
    2451}