Skip to content
Permalink
Browse files

Date/Time: Use `wp_timezone()` in `WP_Date_Query::build_mysql_datetim…

…e()` to address timezone issues.

Improve unit test coverage.

Props Rarst, Biranit, birgire, jave.web, SergeyBiryukov.
Fixes #41782.

git-svn-id: https://develop.svn.wordpress.org/trunk@45876 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information...
SergeyBiryukov committed Aug 22, 2019
1 parent 8d5e4fe commit 4e129d15064924898dd614eee5fa4cf40d9cece9
Showing with 75 additions and 28 deletions.
  1. +12 −6 src/wp-includes/class-wp-date-query.php
  2. +63 −22 tests/phpunit/tests/date/query.php
@@ -853,7 +853,7 @@ public function build_value( $compare, $value ) {
*
* You can pass an array of values (year, month, etc.) with missing parameter values being defaulted to
* either the maximum or minimum values (controlled by the $default_to parameter). Alternatively you can
* pass a string that will be run through strtotime().
* pass a string that will be passed to date_create().
*
* @since 3.7.0
*
@@ -865,8 +865,6 @@ public function build_value( $compare, $value ) {
* @return string|false A MySQL format date/time or false on failure
*/
public function build_mysql_datetime( $datetime, $default_to_max = false ) {
$now = current_time( 'timestamp' );
if ( ! is_array( $datetime ) ) {
/*
@@ -907,15 +905,23 @@ public function build_mysql_datetime( $datetime, $default_to_max = false ) {
// If no match is found, we don't support default_to_max.
if ( ! is_array( $datetime ) ) {
// @todo Timezone issues here possibly
return gmdate( 'Y-m-d H:i:s', strtotime( $datetime, $now ) );
$wp_timezone = wp_timezone();
// Assume local timezone if not provided.
$dt = date_create( $datetime, $wp_timezone );
if ( false === $dt ) {
return gmdate( 'Y-m-d H:i:s', false );
}
return $dt->setTimezone( $wp_timezone )->format( 'Y-m-d H:i:s' );
}
}
$datetime = array_map( 'absint', $datetime );
if ( ! isset( $datetime['year'] ) ) {
$datetime['year'] = gmdate( 'Y', $now );
$datetime['year'] = current_time( 'Y' );
}
if ( ! isset( $datetime['month'] ) ) {
@@ -509,40 +509,81 @@ public function test_build_mysql_datetime_datetime_non_array() {
$this->assertSame( $expected, $found );
}
public function test_build_mysql_datetime_default_to_max_true() {
/**
* @ticket 41782
*
* @dataProvider mysql_datetime_input_provider
*
* @param array|string $datetime Array or string date input.
* @param string $expected Expected built result.
* @param bool $default_to_max Flag to default missing values to max.
*/
public function test_build_mysql_datetime( $datetime, $expected, $default_to_max = false ) {
$q = new WP_Date_Query( array() );
$found = $q->build_mysql_datetime(
array(
'year' => 2011,
),
true
$found = $q->build_mysql_datetime( $datetime, $default_to_max );
$message = "Expected {$expected}, got {$found}";
$this->assertEquals( strtotime( $expected ), strtotime( $found ), $message, 10 );
}
public function mysql_datetime_input_provider() {
return array(
array( '2019-06-04T08:18:24+03:00', '2019-06-04 05:18:24' ),
array( '2019-06-04T05:18:24+00:00', '2019-06-04 05:18:24' ),
array( array(), current_time( 'Y' ) . '-01-01 00:00:00' ),
array( array( 'year' => 2011 ), '2011-12-31 23:59:59', true ),
array( array( 'year' => 2011 ), '2011-01-01 00:00:00' ),
array( '2011', '2011-01-01 00:00:00' ),
array( '2011-02', '2011-02-01 00:00:00' ),
array( '2011-02-03', '2011-02-03 00:00:00' ),
array( '2011-02-03 13:30', '2011-02-03 13:30:00' ),
array( '2011-02-03 13:30:35', '2011-02-03 13:30:35' ),
);
$this->assertSame( '2011-12-31 23:59:59', $found );
}
public function test_build_mysql_datetime_default_to_max_false() {
/**
* @ticket 41782
*
* @dataProvider mysql_datetime_input_provider_custom_timezone
*
* @param array|string $datetime Array or string date input.
* @param string $expected Expected built result.
* @param bool $default_to_max Flag to default missing values to max.
*/
public function test_build_mysql_datetime_with_custom_timezone( $datetime, $expected, $default_to_max = false ) {
update_option( 'timezone_string', 'Europe/Kiev' );
$q = new WP_Date_Query( array() );
$found = $q->build_mysql_datetime(
array(
'year' => 2011,
),
false
$found = $q->build_mysql_datetime( $datetime, $default_to_max );
$message = "Expected {$expected}, got {$found}";
$this->assertEquals( strtotime( $expected ), strtotime( $found ), $message, 10 );
}
public function mysql_datetime_input_provider_custom_timezone() {
return array(
array( '2019-06-04T08:18:24+03:00', '2019-06-04 08:18:24' ),
array( '2019-06-04T05:18:24+00:00', '2019-06-04 08:18:24' ),
);
$this->assertSame( '2011-01-01 00:00:00', $found );
}
public function test_build_mysql_datetime_default_to_max_default_to_false() {
/**
* @ticket 41782
*/
public function test_build_mysql_datetime_with_relative_date() {
update_option( 'timezone_string', 'Europe/Kiev' );
$q = new WP_Date_Query( array() );
$found = $q->build_mysql_datetime(
array(
'year' => 2011,
),
false
);
$this->assertSame( '2011-01-01 00:00:00', $found );
$yesterday = new DateTimeImmutable( '-1 day', wp_timezone() );
$expected = $yesterday->format( 'Y-m-d H:i:s' );
$found = $q->build_mysql_datetime( '-1 day' );
$message = "Expected {$expected}, got {$found}";
$this->assertEquals( strtotime( $expected ), strtotime( $found ), $message, 10 );
}
public function test_build_time_query_insufficient_time_values() {

0 comments on commit 4e129d1

Please sign in to comment.
You can’t perform that action at this time.