Make WordPress Core

Ticket #41782: build-mysql-datetime.patch

File build-mysql-datetime.patch, 4.1 KB (added by Rarst, 6 years ago)

Needs wp_timezone() merged.

  • src/wp-includes/date.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    865865         * @return string|false A MySQL format date/time or false on failure
    866866         */
    867867        public function build_mysql_datetime( $datetime, $default_to_max = false ) {
    868                 $now = current_time( 'timestamp' );
    869868
    870869                if ( ! is_array( $datetime ) ) {
    871870
     
    907906
    908907                        // If no match is found, we don't support default_to_max.
    909908                        if ( ! is_array( $datetime ) ) {
    910                                 // @todo Timezone issues here possibly
    911                                 return gmdate( 'Y-m-d H:i:s', strtotime( $datetime, $now ) );
     909                                $wp_timezone = wp_timezone();
     910                                $dt          = date_create( $datetime, $wp_timezone ); // Assume local time zone if not provided.
     911
     912                                if ( false === $dt ) {
     913                                        return gmdate( 'Y-m-d H:i:s', false );
     914                                }
     915
     916                                return $dt->setTimezone( $wp_timezone )->format( 'Y-m-d H:i:s' );
    912917                        }
    913918                }
    914919
    915920                $datetime = array_map( 'absint', $datetime );
    916921
    917922                if ( ! isset( $datetime['year'] ) ) {
    918                         $datetime['year'] = gmdate( 'Y', $now );
     923                        $datetime['year'] = current_time( 'Y' );
    919924                }
    920925
    921926                if ( ! isset( $datetime['month'] ) ) {
  • tests/phpunit/tests/date/query.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    509509                $this->assertSame( $expected, $found );
    510510        }
    511511
    512         public function test_build_mysql_datetime_default_to_max_true() {
    513                 $q = new WP_Date_Query( array() );
     512        /**
     513         * @dataProvider mysql_datetime_input_provider
     514         *
     515         * @param array|string $datetime       Array or string date input.
     516         * @param string       $result         Expected built result.
     517         * @param bool         $default_to_max Flag to default missing values to max.
     518         */
     519        public function test_build_mysql_datetime( $datetime, $result, $default_to_max = false ) {
    514520
    515                 $found = $q->build_mysql_datetime(
    516                         array(
    517                                 'year' => 2011,
    518                         ),
    519                         true
    520                 );
    521                 $this->assertSame( '2011-12-31 23:59:59', $found );
    522         }
    523 
    524         public function test_build_mysql_datetime_default_to_max_false() {
    525521                $q = new WP_Date_Query( array() );
    526522
    527                 $found = $q->build_mysql_datetime(
    528                         array(
    529                                 'year' => 2011,
    530                         ),
    531                         false
    532                 );
    533                 $this->assertSame( '2011-01-01 00:00:00', $found );
     523                $found = $q->build_mysql_datetime( $datetime, $default_to_max );
     524
     525                $message = "Expected {$result}, got {$found}";
     526                $this->assertEquals( strtotime( $result ), strtotime( $found ), $message, 3 );
    534527        }
    535528
    536         public function test_build_mysql_datetime_default_to_max_default_to_false() {
    537                 $q = new WP_Date_Query( array() );
     529        public function mysql_datetime_input_provider() {
    538530
    539                 $found = $q->build_mysql_datetime(
    540                         array(
    541                                 'year' => 2011,
    542                         ),
    543                         false
    544                 );
    545                 $this->assertSame( '2011-01-01 00:00:00', $found );
     531                update_option( 'timezone_string', 'Europe/Kiev' );
     532
     533                $now       = new DateTimeImmutable( 'now', wp_timezone() );
     534                $yesterday = $now->modify( '-1 day' );
     535                $utc       = new DateTimeZone( 'UTC' );
     536
     537                return [
     538                        [ '-1 day', $yesterday->format( 'Y-m-d H:i:s' ) ],
     539                        [ $yesterday->format( DATE_RFC3339 ), $yesterday->format( 'Y-m-d H:i:s' ) ],
     540                        [ $yesterday->setTimezone( $utc )->format( DATE_RFC3339 ), $yesterday->format( 'Y-m-d H:i:s' ) ],
     541                        [ [], current_time( 'Y' ) . '-01-01 00:00:00' ],
     542                        [ [ 'year' => 2011 ], '2011-12-31 23:59:59', true ],
     543                        [ [ 'year' => 2011 ], '2011-01-01 00:00:00' ],
     544                        [ '2011', '2011-01-01 00:00:00' ],
     545                        [ '2011-02', '2011-02-01 00:00:00' ],
     546                        [ '2011-02-03', '2011-02-03 00:00:00' ],
     547                        [ '2011-02-03 13:30', '2011-02-03 13:30:00' ],
     548                        [ '2011-02-03 13:30:35', '2011-02-03 13:30:35' ],
     549                ];
    546550        }
    547551
    548552        public function test_build_time_query_insufficient_time_values() {