Skip to content

Commit 846a5bb

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. Review follow-ups addressed: - The zero-date check never matched: it compared ltrim() output with '0', and ltrim('00', '0') is an empty string. Zero-dates and impossible calendar dates ('2026-02-31') were therefore parsed by PHP and rolled over into a real date. checkdate() now rejects both, so the documented NULL behaviour actually holds. - Hours are validated to 00-23 in the datetime pattern, so '25:00:00' is rejected instead of rolling into the next day. - Numeric offsets are limited to MySQL's -13:59 through +14:00. PHP's DateTimeZone also accepts '+15:00' and '-14:00', which MySQL treats as invalid and answers with NULL. Refs #586, #594.
1 parent 70bdaf3 commit 846a5bb

2 files changed

Lines changed: 185 additions & 0 deletions

File tree

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

Lines changed: 86 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,91 @@ 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+
// Hours are limited to 00-23 so PHP cannot roll '25:00:00' over.
930+
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 ) ) {
931+
return null;
932+
}
933+
934+
/*
935+
* Reject zero-dates ('0000-00-00') and impossible calendar dates
936+
* ('2026-02-31'). The previous check compared ltrim() output against
937+
* '0', which never matches, so both were converted and PHP rolled
938+
* them over into a real date.
939+
*/
940+
if ( 1 === preg_match( '/^(\d{4})-(\d{1,2})-(\d{1,2})/', $dt, $matches ) ) {
941+
if ( ! checkdate( (int) $matches[2], (int) $matches[3], (int) $matches[1] ) ) {
942+
return null;
943+
}
944+
}
945+
946+
if ( 0 === strcasecmp( $from_tz, 'SYSTEM' ) ) {
947+
$from_tz = date_default_timezone_get();
948+
}
949+
if ( 0 === strcasecmp( $to_tz, 'SYSTEM' ) ) {
950+
$to_tz = date_default_timezone_get();
951+
}
952+
953+
/*
954+
* MySQL accepts numeric offsets only from -13:59 through +14:00.
955+
* PHP's DateTimeZone is more permissive (+15:00, -14:00), so the
956+
* range is enforced here to match MySQL's NULL for invalid zones.
957+
*/
958+
foreach ( array( $from_tz, $to_tz ) as $zone ) {
959+
if ( 1 === preg_match( '/^([+-])(\d{1,2}):(\d{2})$/', $zone, $offset ) ) {
960+
$minutes = ( (int) $offset[2] * 60 ) + (int) $offset[3];
961+
if (
962+
(int) $offset[3] > 59
963+
|| ( '+' === $offset[1] && $minutes > 14 * 60 )
964+
|| ( '-' === $offset[1] && $minutes > ( 13 * 60 + 59 ) )
965+
) {
966+
return null;
967+
}
968+
}
969+
}
970+
971+
try {
972+
$from_zone = new DateTimeZone( $from_tz );
973+
$to_zone = new DateTimeZone( $to_tz );
974+
$date = new DateTimeImmutable( $dt, $from_zone );
975+
} catch ( Exception $e ) {
976+
return null;
977+
}
978+
979+
return $date->setTimezone( $to_zone )->format( 'Y-m-d H:i:s' );
980+
}
981+
896982
/**
897983
* Method to return MySQL version.
898984
*

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

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,105 @@ 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+
// Zero-dates and impossible calendar dates return NULL instead of
1130+
// being rolled over into a real date.
1131+
$result = $this->assertQuery(
1132+
"SELECT CONVERT_TZ('0000-00-00 00:00:00', '+00:00', '+00:00') as output"
1133+
);
1134+
$this->assertNull( $result[0]->output );
1135+
1136+
$result = $this->assertQuery(
1137+
"SELECT CONVERT_TZ('2026-02-31', '+00:00', '+00:00') as output"
1138+
);
1139+
$this->assertNull( $result[0]->output );
1140+
1141+
// An out-of-range hour is rejected rather than rolled into the next day.
1142+
$result = $this->assertQuery(
1143+
"SELECT CONVERT_TZ('2026-09-29 25:00:00', '+00:00', '+00:00') as output"
1144+
);
1145+
$this->assertNull( $result[0]->output );
1146+
1147+
// MySQL accepts numeric offsets only from -13:59 through +14:00.
1148+
foreach ( array( '+15:00', '-14:00', '+14:01', '-13:60' ) as $invalid ) {
1149+
$result = $this->assertQuery(
1150+
"SELECT CONVERT_TZ('2026-09-29 12:00:00', '$invalid', '+00:00') as output"
1151+
);
1152+
$this->assertNull( $result[0]->output, "offset $invalid" );
1153+
}
1154+
1155+
// The bounds themselves are accepted.
1156+
$result = $this->assertQuery(
1157+
"SELECT CONVERT_TZ('2026-09-29 12:00:00', '+14:00', '+00:00') as output"
1158+
);
1159+
$this->assertEquals( '2026-09-28 22:00:00', $result[0]->output );
1160+
1161+
$result = $this->assertQuery(
1162+
"SELECT CONVERT_TZ('2026-09-29 12:00:00', '-13:59', '+00:00') as output"
1163+
);
1164+
$this->assertEquals( '2026-09-30 01:59:00', $result[0]->output );
1165+
}
1166+
1167+
10691168
public function testIntervalArithmetic() {
10701169
// Expected values were taken from MySQL 8.4.
10711170
$result = $this->assertQuery(

0 commit comments

Comments
 (0)