Skip to content

Commit ac9c63a

Browse files
committed
feat(sqlite): add CONVERT_TZ()
Add CONVERT_TZ(dt, from_tz, to_tz) as a PHP UDF, so DATE(CONVERT_TZ(...)) grouping queries work instead of failing with "no such function: CONVERT_TZ". A plugin dashboard in this stack groups daily enrollment rows that way, which is what surfaced the gap. Named time zones use PHP's time-zone database, so daylight saving is handled and a conversion succeeds even where MySQL itself would return NULL with empty time-zone tables. Numeric offsets, UTC and SYSTEM are supported too, and the result is 'Y-m-d H:i:s' like MySQL. NULL arguments, invalid datetimes, zero-dates and unknown zones return NULL rather than raising, so a bad value cannot abort a query. Refs #586, #594.
1 parent 70bdaf3 commit ac9c63a

2 files changed

Lines changed: 124 additions & 0 deletions

File tree

‎packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-pdo-user-defined-functions.php‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public static function register_for( $pdo ): self {
7979
'utc_date' => 'utc_date',
8080
'utc_time' => 'utc_time',
8181
'utc_timestamp' => 'utc_timestamp',
82+
'convert_tz' => 'convert_tz',
8283
'version' => 'version',
8384
'reverse' => 'reverse',
8485

@@ -893,6 +894,67 @@ public function utc_timestamp() {
893894
return gmdate( 'Y-m-d H:i:s', time() );
894895
}
895896

897+
/**
898+
* Method to emulate MySQL CONVERT_TZ() function.
899+
*
900+
* Converts a datetime value from one time zone to another, returning
901+
* a 'Y-m-d H:i:s' string like MySQL does. Returns NULL when any
902+
* argument is NULL, when the datetime is not a valid date or datetime,
903+
* or when either time zone is unknown.
904+
*
905+
* Named time zones use PHP's time zone database, so DST is handled and
906+
* conversions succeed even when MySQL's time zone tables are empty
907+
* (where MySQL itself would return NULL). Offsets like '+00:00' and
908+
* aliases 'UTC' and 'SYSTEM' are also supported.
909+
*
910+
* @param string|null $dt Datetime string, e.g. '2026-09-29 12:00:00'.
911+
* @param string|null $from_tz Source time zone, e.g. '+00:00'.
912+
* @param string|null $to_tz Target time zone, e.g. 'America/New_York'.
913+
*
914+
* @return string|null Converted datetime, or NULL.
915+
*/
916+
public function convert_tz( $dt, $from_tz, $to_tz ) {
917+
if ( null === $dt || null === $from_tz || null === $to_tz ) {
918+
return null;
919+
}
920+
921+
$dt = trim( (string) $dt );
922+
$from_tz = trim( (string) $from_tz );
923+
$to_tz = trim( (string) $to_tz );
924+
925+
if ( '' === $dt || '' === $from_tz || '' === $to_tz ) {
926+
return null;
927+
}
928+
929+
if ( 1 !== preg_match( '/^\d{4}-\d{1,2}-\d{1,2}(?:[ T]\d{1,2}:\d{1,2}:\d{1,2}(?:\.\d+)?)?$/', $dt ) ) {
930+
return null;
931+
}
932+
933+
// MySQL zero-dates like '0000-00-00' are not real datetimes.
934+
if ( 1 === preg_match( '/^(\d{4})-(\d{1,2})-(\d{1,2})/', $dt, $matches ) ) {
935+
if ( '0' === ltrim( $matches[2], '0' ) || '0' === ltrim( $matches[3], '0' ) ) {
936+
return null;
937+
}
938+
}
939+
940+
if ( 0 === strcasecmp( $from_tz, 'SYSTEM' ) ) {
941+
$from_tz = date_default_timezone_get();
942+
}
943+
if ( 0 === strcasecmp( $to_tz, 'SYSTEM' ) ) {
944+
$to_tz = date_default_timezone_get();
945+
}
946+
947+
try {
948+
$from_zone = new DateTimeZone( $from_tz );
949+
$to_zone = new DateTimeZone( $to_tz );
950+
$date = new DateTimeImmutable( $dt, $from_zone );
951+
} catch ( Exception $e ) {
952+
return null;
953+
}
954+
955+
return $date->setTimezone( $to_zone )->format( 'Y-m-d H:i:s' );
956+
}
957+
896958
/**
897959
* Method to return MySQL version.
898960
*

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,68 @@ public function testDateAddFunctionWithNegativeAndComputedIntervals() {
10661066
$this->assertEquals( '2024-01-08 10:00:00', $result[0]->output );
10671067
}
10681068

1069+
public function testConvertTzFunction() {
1070+
// Identity conversion.
1071+
$result = $this->assertQuery(
1072+
"SELECT CONVERT_TZ('2026-09-29 12:00:00', '+00:00', '+00:00') as output"
1073+
);
1074+
$this->assertEquals( '2026-09-29 12:00:00', $result[0]->output );
1075+
1076+
// Numeric offset conversion.
1077+
$result = $this->assertQuery(
1078+
"SELECT CONVERT_TZ('2026-09-29 12:00:00', '+00:00', '-05:00') as output"
1079+
);
1080+
$this->assertEquals( '2026-09-29 07:00:00', $result[0]->output );
1081+
1082+
// Half-hour offset conversion.
1083+
$result = $this->assertQuery(
1084+
"SELECT CONVERT_TZ('2026-09-29 12:00:00', '+00:00', '+05:30') as output"
1085+
);
1086+
$this->assertEquals( '2026-09-29 17:30:00', $result[0]->output );
1087+
1088+
// Named time zone conversion observes daylight saving time.
1089+
$result = $this->assertQuery(
1090+
"SELECT CONVERT_TZ('2026-01-15 12:00:00', '+00:00', 'America/New_York') as output"
1091+
);
1092+
$this->assertEquals( '2026-01-15 07:00:00', $result[0]->output );
1093+
1094+
$result = $this->assertQuery(
1095+
"SELECT CONVERT_TZ('2026-07-15 12:00:00', '+00:00', 'America/New_York') as output"
1096+
);
1097+
$this->assertEquals( '2026-07-15 08:00:00', $result[0]->output );
1098+
1099+
// DATE() over CONVERT_TZ() groups per-day analytics correctly.
1100+
$result = $this->assertQuery(
1101+
"SELECT DATE(CONVERT_TZ('2026-09-29 23:30:00', '+00:00', '+05:00')) as output"
1102+
);
1103+
$this->assertEquals( '2026-09-30', $result[0]->output );
1104+
1105+
// UTC alias behaves like +00:00.
1106+
$result = $this->assertQuery(
1107+
"SELECT CONVERT_TZ('2026-09-29 12:00:00', 'UTC', '+00:00') as output"
1108+
);
1109+
$this->assertEquals( '2026-09-29 12:00:00', $result[0]->output );
1110+
1111+
// NULL propagates like MySQL.
1112+
$result = $this->assertQuery(
1113+
"SELECT CONVERT_TZ(NULL, '+00:00', '+00:00') as output"
1114+
);
1115+
$this->assertNull( $result[0]->output );
1116+
1117+
// Invalid datetime returns NULL like MySQL.
1118+
$result = $this->assertQuery(
1119+
"SELECT CONVERT_TZ('bad', '+00:00', '+00:00') as output"
1120+
);
1121+
$this->assertNull( $result[0]->output );
1122+
1123+
// Unknown time zone returns NULL like MySQL.
1124+
$result = $this->assertQuery(
1125+
"SELECT CONVERT_TZ('2026-09-29 12:00:00', 'bad', '+00:00') as output"
1126+
);
1127+
$this->assertNull( $result[0]->output );
1128+
}
1129+
1130+
10691131
public function testIntervalArithmetic() {
10701132
// Expected values were taken from MySQL 8.4.
10711133
$result = $this->assertQuery(

0 commit comments

Comments
 (0)