Skip to content

Commit 16dc9dc

Browse files
committed
apply suggestions from the review
1 parent 31099f4 commit 16dc9dc

File tree

3 files changed

+3
-21
lines changed

3 files changed

+3
-21
lines changed

system/Database/BaseConnection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1959,7 +1959,7 @@ protected function convertTimezoneToOffset(string $timezone): string
19591959
return sprintf('%+03d:%02d', $hours, $minutes);
19601960
} catch (Exception $e) {
19611961
// If timezone conversion fails, log and return UTC
1962-
log_message('error', "Invalid timezone '{$timezone}': {$e->getMessage()}. Falling back to UTC.");
1962+
log_message('error', "Invalid timezone '{$timezone}'. Falling back to UTC. {$e->getMessage()}.");
19631963

19641964
return '+00:00';
19651965
}

tests/system/Database/BaseConnectionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ public function testConvertTimezoneToOffsetWithInvalidTimezone(): void
385385

386386
$result = $this->getPrivateMethodInvoker($db, 'convertTimezoneToOffset')('Invalid/Timezone');
387387
$this->assertSame('+00:00', $result);
388+
$this->assertLogged('error', "Invalid timezone 'Invalid/Timezone'. Falling back to UTC. DateTimeZone::__construct(): Unknown or bad timezone (Invalid/Timezone).");
388389
}
389390

390391
public function testGetSessionTimezoneWithFalse(): void

tests/system/Database/Live/ConnectTest.php

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use CodeIgniter\Config\Factories;
1717
use CodeIgniter\Database\SQLite3\Connection;
1818
use CodeIgniter\Exceptions\RuntimeException;
19-
use CodeIgniter\I18n\Time;
2019
use CodeIgniter\Test\CIUnitTestCase;
2120
use CodeIgniter\Test\DatabaseTestTrait;
2221
use Config\Database;
@@ -188,7 +187,7 @@ public function testTimezoneAutoSyncWithAppTimezone(): void
188187

189188
$appConfig = config('App');
190189
$appTimezone = $appConfig->appTimezone ?? 'UTC';
191-
$expectedOffset = $this->convertTimezoneToOffset($appTimezone);
190+
$expectedOffset = $this->getPrivateMethodInvoker($db, 'convertTimezoneToOffset')($appTimezone);
192191

193192
$this->assertSame($expectedOffset, $timezone);
194193
}
@@ -221,22 +220,4 @@ private function getDatabaseTimezone($db, string $driver): string
221220
throw new RuntimeException("Unsupported driver: {$driver}");
222221
}
223222
}
224-
225-
/**
226-
* Helper method to convert timezone to offset (mirrors BaseConnection logic)
227-
*/
228-
private function convertTimezoneToOffset(string $timezone): string
229-
{
230-
if (preg_match('/^[+-]\d{2}:\d{2}$/', $timezone)) {
231-
return $timezone;
232-
}
233-
234-
$time = Time::now($timezone);
235-
$offset = $time->getOffset();
236-
237-
$hours = (int) ($offset / 3600);
238-
$minutes = abs((int) (($offset % 3600) / 60));
239-
240-
return sprintf('%+03d:%02d', $hours, $minutes);
241-
}
242223
}

0 commit comments

Comments
 (0)