diff --git a/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php b/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php index 7b8828e7b..3428e735f 100644 --- a/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php +++ b/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php @@ -79,6 +79,7 @@ public static function register_for( $pdo ): self { 'utc_date' => 'utc_date', 'utc_time' => 'utc_time', 'utc_timestamp' => 'utc_timestamp', + 'convert_tz' => 'convert_tz', 'version' => 'version', 'reverse' => 'reverse', @@ -893,6 +894,91 @@ public function utc_timestamp() { return gmdate( 'Y-m-d H:i:s', time() ); } + /** + * Method to emulate MySQL CONVERT_TZ() function. + * + * Converts a datetime value from one time zone to another, returning + * a 'Y-m-d H:i:s' string like MySQL does. Returns NULL when any + * argument is NULL, when the datetime is not a valid date or datetime, + * or when either time zone is unknown. + * + * Named time zones use PHP's time zone database, so DST is handled and + * conversions succeed even when MySQL's time zone tables are empty + * (where MySQL itself would return NULL). Offsets like '+00:00' and + * aliases 'UTC' and 'SYSTEM' are also supported. + * + * @param string|null $dt Datetime string, e.g. '2026-09-29 12:00:00'. + * @param string|null $from_tz Source time zone, e.g. '+00:00'. + * @param string|null $to_tz Target time zone, e.g. 'America/New_York'. + * + * @return string|null Converted datetime, or NULL. + */ + public function convert_tz( $dt, $from_tz, $to_tz ) { + if ( null === $dt || null === $from_tz || null === $to_tz ) { + return null; + } + + $dt = trim( (string) $dt ); + $from_tz = trim( (string) $from_tz ); + $to_tz = trim( (string) $to_tz ); + + if ( '' === $dt || '' === $from_tz || '' === $to_tz ) { + return null; + } + + // Hours are limited to 00-23 so PHP cannot roll '25:00:00' over. + if ( 1 !== preg_match( '/^\d{4}-\d{1,2}-\d{1,2}(?:[ T](?:[01]?\d|2[0-3]):[0-5]?\d:[0-5]?\d(?:\.\d+)?)?$/', $dt ) ) { + return null; + } + + /* + * Reject zero-dates ('0000-00-00') and impossible calendar dates + * ('2026-02-31'). The previous check compared ltrim() output against + * '0', which never matches, so both were converted and PHP rolled + * them over into a real date. + */ + if ( 1 === preg_match( '/^(\d{4})-(\d{1,2})-(\d{1,2})/', $dt, $matches ) ) { + if ( ! checkdate( (int) $matches[2], (int) $matches[3], (int) $matches[1] ) ) { + return null; + } + } + + if ( 0 === strcasecmp( $from_tz, 'SYSTEM' ) ) { + $from_tz = date_default_timezone_get(); + } + if ( 0 === strcasecmp( $to_tz, 'SYSTEM' ) ) { + $to_tz = date_default_timezone_get(); + } + + /* + * MySQL accepts numeric offsets only from -13:59 through +14:00. + * PHP's DateTimeZone is more permissive (+15:00, -14:00), so the + * range is enforced here to match MySQL's NULL for invalid zones. + */ + foreach ( array( $from_tz, $to_tz ) as $zone ) { + if ( 1 === preg_match( '/^([+-])(\d{1,2}):(\d{2})$/', $zone, $offset ) ) { + $minutes = ( (int) $offset[2] * 60 ) + (int) $offset[3]; + if ( + (int) $offset[3] > 59 + || ( '+' === $offset[1] && $minutes > 14 * 60 ) + || ( '-' === $offset[1] && $minutes > ( 13 * 60 + 59 ) ) + ) { + return null; + } + } + } + + try { + $from_zone = new DateTimeZone( $from_tz ); + $to_zone = new DateTimeZone( $to_tz ); + $date = new DateTimeImmutable( $dt, $from_zone ); + } catch ( Exception $e ) { + return null; + } + + return $date->setTimezone( $to_zone )->format( 'Y-m-d H:i:s' ); + } + /** * Method to return MySQL version. * diff --git a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php index fff5bc6b5..5217e7814 100644 --- a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php +++ b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php @@ -1066,6 +1066,105 @@ public function testDateAddFunctionWithNegativeAndComputedIntervals() { $this->assertEquals( '2024-01-08 10:00:00', $result[0]->output ); } + public function testConvertTzFunction() { + // Identity conversion. + $result = $this->assertQuery( + "SELECT CONVERT_TZ('2026-09-29 12:00:00', '+00:00', '+00:00') as output" + ); + $this->assertEquals( '2026-09-29 12:00:00', $result[0]->output ); + + // Numeric offset conversion. + $result = $this->assertQuery( + "SELECT CONVERT_TZ('2026-09-29 12:00:00', '+00:00', '-05:00') as output" + ); + $this->assertEquals( '2026-09-29 07:00:00', $result[0]->output ); + + // Half-hour offset conversion. + $result = $this->assertQuery( + "SELECT CONVERT_TZ('2026-09-29 12:00:00', '+00:00', '+05:30') as output" + ); + $this->assertEquals( '2026-09-29 17:30:00', $result[0]->output ); + + // Named time zone conversion observes daylight saving time. + $result = $this->assertQuery( + "SELECT CONVERT_TZ('2026-01-15 12:00:00', '+00:00', 'America/New_York') as output" + ); + $this->assertEquals( '2026-01-15 07:00:00', $result[0]->output ); + + $result = $this->assertQuery( + "SELECT CONVERT_TZ('2026-07-15 12:00:00', '+00:00', 'America/New_York') as output" + ); + $this->assertEquals( '2026-07-15 08:00:00', $result[0]->output ); + + // DATE() over CONVERT_TZ() groups per-day analytics correctly. + $result = $this->assertQuery( + "SELECT DATE(CONVERT_TZ('2026-09-29 23:30:00', '+00:00', '+05:00')) as output" + ); + $this->assertEquals( '2026-09-30', $result[0]->output ); + + // UTC alias behaves like +00:00. + $result = $this->assertQuery( + "SELECT CONVERT_TZ('2026-09-29 12:00:00', 'UTC', '+00:00') as output" + ); + $this->assertEquals( '2026-09-29 12:00:00', $result[0]->output ); + + // NULL propagates like MySQL. + $result = $this->assertQuery( + "SELECT CONVERT_TZ(NULL, '+00:00', '+00:00') as output" + ); + $this->assertNull( $result[0]->output ); + + // Invalid datetime returns NULL like MySQL. + $result = $this->assertQuery( + "SELECT CONVERT_TZ('bad', '+00:00', '+00:00') as output" + ); + $this->assertNull( $result[0]->output ); + + // Unknown time zone returns NULL like MySQL. + $result = $this->assertQuery( + "SELECT CONVERT_TZ('2026-09-29 12:00:00', 'bad', '+00:00') as output" + ); + $this->assertNull( $result[0]->output ); + + // Zero-dates and impossible calendar dates return NULL instead of + // being rolled over into a real date. + $result = $this->assertQuery( + "SELECT CONVERT_TZ('0000-00-00 00:00:00', '+00:00', '+00:00') as output" + ); + $this->assertNull( $result[0]->output ); + + $result = $this->assertQuery( + "SELECT CONVERT_TZ('2026-02-31', '+00:00', '+00:00') as output" + ); + $this->assertNull( $result[0]->output ); + + // An out-of-range hour is rejected rather than rolled into the next day. + $result = $this->assertQuery( + "SELECT CONVERT_TZ('2026-09-29 25:00:00', '+00:00', '+00:00') as output" + ); + $this->assertNull( $result[0]->output ); + + // MySQL accepts numeric offsets only from -13:59 through +14:00. + foreach ( array( '+15:00', '-14:00', '+14:01', '-13:60' ) as $invalid ) { + $result = $this->assertQuery( + "SELECT CONVERT_TZ('2026-09-29 12:00:00', '$invalid', '+00:00') as output" + ); + $this->assertNull( $result[0]->output, "offset $invalid" ); + } + + // The bounds themselves are accepted. + $result = $this->assertQuery( + "SELECT CONVERT_TZ('2026-09-29 12:00:00', '+14:00', '+00:00') as output" + ); + $this->assertEquals( '2026-09-28 22:00:00', $result[0]->output ); + + $result = $this->assertQuery( + "SELECT CONVERT_TZ('2026-09-29 12:00:00', '-13:59', '+00:00') as output" + ); + $this->assertEquals( '2026-09-30 01:59:00', $result[0]->output ); + } + + public function testIntervalArithmetic() { // Expected values were taken from MySQL 8.4. $result = $this->assertQuery(