Skip to content

Commit b668fb4

Browse files
committed
Use the current timezone when reading an UTCDateTime
1 parent 053a893 commit b668fb4

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

Diff for: src/Query/Builder.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Carbon\CarbonPeriod;
1010
use Closure;
1111
use DateTimeInterface;
12+
use DateTimeZone;
1213
use Illuminate\Database\Query\Builder as BaseBuilder;
1314
use Illuminate\Database\Query\Expression;
1415
use Illuminate\Support\Arr;
@@ -40,6 +41,7 @@
4041
use function call_user_func_array;
4142
use function count;
4243
use function ctype_xdigit;
44+
use function date_default_timezone_get;
4345
use function dd;
4446
use function dump;
4547
use function end;
@@ -1662,7 +1664,8 @@ private function aliasIdForResult(array|object $values): array|object
16621664

16631665
foreach ($values as $key => $value) {
16641666
if ($value instanceof UTCDateTime) {
1665-
$values[$key] = Date::instance($value->toDateTime());
1667+
$values[$key] = Date::instance($value->toDateTime())
1668+
->setTimezone(new DateTimeZone(date_default_timezone_get()));
16661669
} elseif (is_array($value) || $value instanceof stdClass) {
16671670
$values[$key] = $this->aliasIdForResult($value);
16681671
}
@@ -1677,7 +1680,8 @@ private function aliasIdForResult(array|object $values): array|object
16771680

16781681
foreach (get_object_vars($values) as $key => $value) {
16791682
if ($value instanceof UTCDateTime) {
1680-
$values->{$key} = Date::instance($value->toDateTime());
1683+
$values->{$key} = Date::instance($value->toDateTime())
1684+
->setTimezone(new DateTimeZone(date_default_timezone_get()));
16811685
} elseif (is_array($value) || $value instanceof stdClass) {
16821686
$values->{$key} = $this->aliasIdForResult($value);
16831687
}

Diff for: tests/AuthTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
namespace MongoDB\Laravel\Tests;
66

7+
use Carbon\Carbon;
78
use Illuminate\Auth\Passwords\PasswordBroker;
89
use Illuminate\Support\Facades\Auth;
910
use Illuminate\Support\Facades\DB;
1011
use Illuminate\Support\Facades\Hash;
11-
use MongoDB\BSON\UTCDateTime;
1212
use MongoDB\Laravel\Tests\Models\User;
1313

1414
use function bcrypt;
@@ -63,7 +63,7 @@ function ($actualUser, $actualToken) use ($user, &$token) {
6363
$reminder = DB::table('password_reset_tokens')->first();
6464
$this->assertEquals('[email protected]', $reminder->email);
6565
$this->assertNotNull($reminder->token);
66-
$this->assertInstanceOf(UTCDateTime::class, $reminder->created_at);
66+
$this->assertInstanceOf(Carbon::class, $reminder->created_at);
6767

6868
$credentials = [
6969
'email' => '[email protected]',

Diff for: tests/QueueTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public function testFailedJobLogging()
197197
$this->assertSame('test_connection', $failedJob->connection);
198198
$this->assertSame('test_queue', $failedJob->queue);
199199
$this->assertSame('test_payload', $failedJob->payload);
200-
$this->assertEquals(new UTCDateTime(Carbon::now()), $failedJob->failed_at);
200+
$this->assertEquals(Carbon::now(), $failedJob->failed_at);
201201
$this->assertStringStartsWith('Exception: test_exception in ', $failedJob->exception);
202202
}
203203
}

0 commit comments

Comments
 (0)