Skip to content

Commit

Permalink
Fixing more Datetime timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
luanfreitasdev committed Feb 14, 2024
1 parent 37f458d commit 04c0a04
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/Logs/LaravelLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ protected function parseText(array &$matches = []): void

preg_match(static::regexPattern(), array_shift($firstLineSplit), $matches);

$this->datetime = Carbon::parse($matches[1], config('log-viewer.timezone', config('app.timezone', 'UTC')));
$this->datetime = Carbon::parse($matches[1])?->setTimezone(
config('log-viewer.timezone', config('app.timezone', 'UTC')) ?? 'UTC'
);

// $matches[2] contains microseconds, which is already handled
// $matches[3] contains timezone offset, which is already handled
Expand Down
5 changes: 4 additions & 1 deletion src/Logs/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ public static function matches(string $text, ?int &$timestamp = null, ?string &$

if ($result) {
try {
$timestamp = static::parseDateTime($matches[static::$regexDatetimeKey] ?? null)?->timestamp;
$datetime = static::parseDateTime($matches[static::$regexDatetimeKey] ?? null);
$timezone = config('log-viewer.timezone', config('app.timezone', 'UTC')) ?? 'UTC';
$timestamp = $datetime?->setTimezone($timezone)->timestamp;

$level = $matches[static::$regexLevelKey] ?? '';
} catch (\Exception $exception) {
// not a valid datetime, so we can't match this log. Perhaps it's a different but similar log type.
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/LogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
$log = new LaravelLog($text, 'laravel.log', 0, 0);

assertEquals($tz, $log->datetime->timezoneName);
$expectedTime = \Carbon\Carbon::parse('2022-11-07 17:51:33', $tz)->toDateTimeString();
$expectedTime = \Carbon\Carbon::parse('2022-11-07 17:51:33', 'UTC')->setTimezone($tz)->toDateTimeString();
assertEquals($expectedTime, $log->datetime->toDateTimeString());
});

Expand Down

0 comments on commit 04c0a04

Please sign in to comment.