Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -823,19 +823,50 @@ public function inet_aton( $addr ) {
/**
* Method to emulate MySQL DATEDIFF() function.
*
* This function compares two dates value and returns the difference.
* Returns the number of days from the end date to the start date. Only the
* date parts of the values are used. When either value is NULL or is not
* a valid date, NULL is returned.
*
* @param string $start Start date.
* @param string $end End date.
* The result must be an integer. SQLite compares a text value as greater
* than any number, so a numeric string would break comparisons like
* "DATEDIFF(...) > 30".
*
* @return string
* @param string|null $start Start date.
* @param string|null $end End date.
*
* @return int|null
*/
public function datediff( $start, $end ) {
$start_date = new DateTime( $start );
$end_date = new DateTime( $end );
$interval = $end_date->diff( $start_date, false );
$dates = array();
foreach ( array( $start, $end ) as $value ) {
if ( null === $value ) {
return null;
}

/*
* Reject values that can't be parsed, invalid dates like "2019-02-29",
* values without a date like "10:00:00", and relative values like
* "tomorrow". MySQL returns NULL for all of these.
*/
$parts = date_parse( $value );
if (
$parts['error_count'] > 0
|| $parts['warning_count'] > 0
|| false === $parts['year']
|| false === $parts['month']
|| false === $parts['day']
|| isset( $parts['relative'] )
) {
return null;
}

$dates[] = new DateTime(
sprintf( '%04d-%02d-%02d', $parts['year'], $parts['month'], $parts['day'] ),
new DateTimeZone( 'UTC' )
);
}

return $interval->format( '%r%a' );
return (int) $dates[1]->diff( $dates[0] )->format( '%r%a' );
}

/**
Expand Down
61 changes: 61 additions & 0 deletions packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,67 @@ public function testIntervalArithmeticWithUnsupportedUnits() {
);
}

/**
* @dataProvider datediffFunctionValues
*/
public function testDatediffFunction( string $args, ?string $expected ): void {
$result = $this->assertQuery( "SELECT DATEDIFF($args) AS value" );
$this->assertSame( $expected, $result[0]->value );
}

public static function datediffFunctionValues(): array {
// Expected values were taken from MySQL 26.10.
return array(
'dates' => array( "'2020-01-02', '2020-01-01'", '1' ),
'dates reversed' => array( "'2020-01-01', '2020-01-02'", '-1' ),
'same date' => array( "'2020-01-01', '2020-01-01'", '0' ),
'leap year' => array( "'2021-03-01', '2020-02-29'", '366' ),
'mysql docs example 1' => array( "'2007-12-31 23:59:59', '2007-12-30'", '1' ),
'mysql docs example 2' => array( "'2010-11-30 23:59:59', '2010-12-31'", '-31' ),
'times across midnight' => array( "'2020-01-02 00:00:01', '2020-01-01 23:59:59'", '1' ),
'times across midnight 2' => array( "'2020-01-01 23:59:59', '2020-01-02 00:00:01'", '-1' ),
'later time on same date' => array( "'2020-01-01', '2020-01-01 23:59:59'", '0' ),
'fewer than 24 hours' => array( "'2020-01-02 12:00:00', '2020-01-01 13:00:00'", '1' ),
'iso 8601' => array( "'2020-01-02T10:00:00', '2020-01-01'", '1' ),
'fractional seconds' => array( "'2020-01-02 10:00:00.123456', '2020-01-01'", '1' ),
'no delimiters' => array( "'20200102', '2020-01-01'", '1' ),
'slash delimiters' => array( "'2020/01/02', '2020-01-01'", '1' ),
'two-digit year' => array( "'99-01-02', '1999-01-01'", '1' ),
'year zero' => array( "'0000-01-01', '0000-01-02'", '-1' ),
'null start' => array( "NULL, '2020-01-01'", null ),
'null end' => array( "'2020-01-01', NULL", null ),
'zero date' => array( "'0000-00-00', '2020-01-01'", null ),
'invalid day of month' => array( "'2019-02-29', '2019-02-28'", null ),
'invalid month' => array( "'2020-13-01', '2020-01-01'", null ),
'not a date' => array( "'bad', '2020-01-01'", null ),
'empty string' => array( "'', '2020-01-01'", null ),
'time without a date' => array( "'10:00:00', '2020-01-01'", null ),
'date without a year' => array( "'January 2', '2020-01-01'", null ),
'now' => array( "'now', '2020-01-01'", null ),
'relative date' => array( "'tomorrow', '2020-01-01'", null ),
'date with relative part' => array( "'2020-01-02 +1 day', '2020-01-01'", null ),
);
}

public function testDatediffFunctionInNumericComparisons(): void {
$this->assertQuery( 'CREATE TABLE _posts (id INT PRIMARY KEY, post_date DATETIME)' );
$this->assertQuery(
"INSERT INTO _posts (id, post_date) VALUES
(1, '2024-01-31 10:00:00'),
(2, '2024-01-21 23:59:59'),
(3, '2023-12-01 00:00:00')"
);

$result = $this->assertQuery( "SELECT id FROM _posts WHERE DATEDIFF('2024-01-31 09:00:00', post_date) > 30 ORDER BY id" );
$this->assertEquals( array( (object) array( 'id' => '3' ) ), $result );

$result = $this->assertQuery( "SELECT id FROM _posts WHERE DATEDIFF('2024-01-31 09:00:00', post_date) <= 30 ORDER BY id" );
$this->assertEquals( array( (object) array( 'id' => '1' ), (object) array( 'id' => '2' ) ), $result );

$result = $this->assertQuery( "SELECT id FROM _posts WHERE DATEDIFF('2024-01-31 09:00:00', post_date) = 10 ORDER BY id" );
$this->assertEquals( array( (object) array( 'id' => '2' ) ), $result );
}

public function testLeftFunction1Char() {
$result = $this->assertQuery(
'SELECT LEFT("abc", 1) as output'
Expand Down
Loading