Skip to content

Commit 6b291d7

Browse files
authored
Merge pull request #1966 from Giacomo92/pr_1534_1
[Updated PR#1534] UTCDateTime conversion now includes milliseconds
2 parents 3f5b1dc + f3f1441 commit 6b291d7

File tree

4 files changed

+17
-15
lines changed

4 files changed

+17
-15
lines changed

src/Jenssegers/Mongodb/Auth/DatabaseTokenRepository.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use DateTime;
66
use DateTimeZone;
77
use Illuminate\Auth\Passwords\DatabaseTokenRepository as BaseDatabaseTokenRepository;
8+
use Illuminate\Support\Facades\Date;
89
use MongoDB\BSON\UTCDateTime;
910

1011
class DatabaseTokenRepository extends BaseDatabaseTokenRepository
@@ -17,7 +18,7 @@ protected function getPayload($email, $token)
1718
return [
1819
'email' => $email,
1920
'token' => $this->hasher->make($token),
20-
'created_at' => new UTCDateTime(time() * 1000),
21+
'created_at' => new UTCDateTime(Date::now()->format('Uv')),
2122
];
2223
}
2324

@@ -37,7 +38,7 @@ protected function tokenExpired($createdAt)
3738
protected function tokenRecentlyCreated($createdAt)
3839
{
3940
$createdAt = $this->convertDateTime($createdAt);
40-
41+
4142
return parent::tokenRecentlyCreated($createdAt);
4243
}
4344

src/Jenssegers/Mongodb/Eloquent/Model.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
namespace Jenssegers\Mongodb\Eloquent;
44

5-
use Illuminate\Support\Carbon;
65
use DateTime;
76
use Illuminate\Contracts\Queue\QueueableCollection;
87
use Illuminate\Contracts\Queue\QueueableEntity;
98
use Illuminate\Database\Eloquent\Model as BaseModel;
109
use Illuminate\Database\Eloquent\Relations\Relation;
1110
use Illuminate\Support\Arr;
11+
use Illuminate\Support\Facades\Date;
1212
use Illuminate\Support\Str;
1313
use Jenssegers\Mongodb\Query\Builder as QueryBuilder;
1414
use MongoDB\BSON\Binary;
@@ -89,7 +89,7 @@ public function fromDateTime($value)
8989
$value = parent::asDateTime($value);
9090
}
9191

92-
return new UTCDateTime($value->getTimestamp() * 1000);
92+
return new UTCDateTime($value->format('Uv'));
9393
}
9494

9595
/**
@@ -99,7 +99,7 @@ protected function asDateTime($value)
9999
{
100100
// Convert UTCDateTime instances.
101101
if ($value instanceof UTCDateTime) {
102-
return Carbon::createFromTimestamp($value->toDateTime()->getTimestamp());
102+
return Date::createFromTimestampMs($value->toDateTime()->format('Uv'));
103103
}
104104

105105
return parent::asDateTime($value);
@@ -118,7 +118,7 @@ public function getDateFormat()
118118
*/
119119
public function freshTimestamp()
120120
{
121-
return new UTCDateTime(Carbon::now());
121+
return new UTCDateTime(Date::now()->format('Uv'));
122122
}
123123

124124
/**

src/Jenssegers/Mongodb/Query/Builder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ public function getFresh($columns = [])
294294
}
295295
}
296296
}
297-
297+
298298
// The _id field is mandatory when using grouping.
299299
if ($group && empty($group['_id'])) {
300300
$group['_id'] = null;
@@ -930,18 +930,18 @@ protected function compileWheres()
930930
if (is_array($where['value'])) {
931931
array_walk_recursive($where['value'], function (&$item, $key) {
932932
if ($item instanceof DateTime) {
933-
$item = new UTCDateTime($item->getTimestamp() * 1000);
933+
$item = new UTCDateTime($item->format('Uv'));
934934
}
935935
});
936936
} else {
937937
if ($where['value'] instanceof DateTime) {
938-
$where['value'] = new UTCDateTime($where['value']->getTimestamp() * 1000);
938+
$where['value'] = new UTCDateTime($where['value']->format('Uv'));
939939
}
940940
}
941941
} elseif (isset($where['values'])) {
942942
array_walk_recursive($where['values'], function (&$item, $key) {
943943
if ($item instanceof DateTime) {
944-
$item = new UTCDateTime($item->getTimestamp() * 1000);
944+
$item = new UTCDateTime($item->format('Uv'));
945945
}
946946
});
947947
}

tests/QueryBuilderTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
declare(strict_types=1);
33

4+
use Illuminate\Support\Facades\Date;
45
use Illuminate\Support\Facades\DB;
56
use Jenssegers\Mongodb\Collection;
67
use Jenssegers\Mongodb\Query\Builder;
@@ -545,14 +546,14 @@ public function testUpdateSubdocument()
545546
public function testDates()
546547
{
547548
DB::collection('users')->insert([
548-
['name' => 'John Doe', 'birthday' => new UTCDateTime(1000 * strtotime("1980-01-01 00:00:00"))],
549-
['name' => 'Jane Doe', 'birthday' => new UTCDateTime(1000 * strtotime("1981-01-01 00:00:00"))],
550-
['name' => 'Robert Roe', 'birthday' => new UTCDateTime(1000 * strtotime("1982-01-01 00:00:00"))],
551-
['name' => 'Mark Moe', 'birthday' => new UTCDateTime(1000 * strtotime("1983-01-01 00:00:00"))],
549+
['name' => 'John Doe', 'birthday' => new UTCDateTime(Date::parse("1980-01-01 00:00:00")->format('Uv'))],
550+
['name' => 'Jane Doe', 'birthday' => new UTCDateTime(Date::parse("1981-01-01 00:00:00")->format('Uv'))],
551+
['name' => 'Robert Roe', 'birthday' => new UTCDateTime(Date::parse("1982-01-01 00:00:00")->format('Uv'))],
552+
['name' => 'Mark Moe', 'birthday' => new UTCDateTime(Date::parse("1983-01-01 00:00:00")->format('Uv'))],
552553
]);
553554

554555
$user = DB::collection('users')
555-
->where('birthday', new UTCDateTime(1000 * strtotime("1980-01-01 00:00:00")))
556+
->where('birthday', new UTCDateTime(Date::parse("1980-01-01 00:00:00")->format('Uv')))
556557
->first();
557558
$this->assertEquals('John Doe', $user['name']);
558559

0 commit comments

Comments
 (0)