Skip to content

Commit

Permalink
Merge pull request #1966 from Giacomo92/pr_1534_1
Browse files Browse the repository at this point in the history
[Updated PR#1534] UTCDateTime conversion now includes milliseconds
  • Loading branch information
Smolevich committed Feb 23, 2020
2 parents 3f5b1dc + f3f1441 commit 6b291d7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
5 changes: 3 additions & 2 deletions src/Jenssegers/Mongodb/Auth/DatabaseTokenRepository.php
Expand Up @@ -5,6 +5,7 @@
use DateTime;
use DateTimeZone;
use Illuminate\Auth\Passwords\DatabaseTokenRepository as BaseDatabaseTokenRepository;
use Illuminate\Support\Facades\Date;
use MongoDB\BSON\UTCDateTime;

class DatabaseTokenRepository extends BaseDatabaseTokenRepository
Expand All @@ -17,7 +18,7 @@ protected function getPayload($email, $token)
return [
'email' => $email,
'token' => $this->hasher->make($token),
'created_at' => new UTCDateTime(time() * 1000),
'created_at' => new UTCDateTime(Date::now()->format('Uv')),
];
}

Expand All @@ -37,7 +38,7 @@ protected function tokenExpired($createdAt)
protected function tokenRecentlyCreated($createdAt)
{
$createdAt = $this->convertDateTime($createdAt);

return parent::tokenRecentlyCreated($createdAt);
}

Expand Down
8 changes: 4 additions & 4 deletions src/Jenssegers/Mongodb/Eloquent/Model.php
Expand Up @@ -2,13 +2,13 @@

namespace Jenssegers\Mongodb\Eloquent;

use Illuminate\Support\Carbon;
use DateTime;
use Illuminate\Contracts\Queue\QueueableCollection;
use Illuminate\Contracts\Queue\QueueableEntity;
use Illuminate\Database\Eloquent\Model as BaseModel;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Str;
use Jenssegers\Mongodb\Query\Builder as QueryBuilder;
use MongoDB\BSON\Binary;
Expand Down Expand Up @@ -89,7 +89,7 @@ public function fromDateTime($value)
$value = parent::asDateTime($value);
}

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

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

return parent::asDateTime($value);
Expand All @@ -118,7 +118,7 @@ public function getDateFormat()
*/
public function freshTimestamp()
{
return new UTCDateTime(Carbon::now());
return new UTCDateTime(Date::now()->format('Uv'));
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Jenssegers/Mongodb/Query/Builder.php
Expand Up @@ -294,7 +294,7 @@ public function getFresh($columns = [])
}
}
}

// The _id field is mandatory when using grouping.
if ($group && empty($group['_id'])) {
$group['_id'] = null;
Expand Down Expand Up @@ -930,18 +930,18 @@ protected function compileWheres()
if (is_array($where['value'])) {
array_walk_recursive($where['value'], function (&$item, $key) {
if ($item instanceof DateTime) {
$item = new UTCDateTime($item->getTimestamp() * 1000);
$item = new UTCDateTime($item->format('Uv'));
}
});
} else {
if ($where['value'] instanceof DateTime) {
$where['value'] = new UTCDateTime($where['value']->getTimestamp() * 1000);
$where['value'] = new UTCDateTime($where['value']->format('Uv'));
}
}
} elseif (isset($where['values'])) {
array_walk_recursive($where['values'], function (&$item, $key) {
if ($item instanceof DateTime) {
$item = new UTCDateTime($item->getTimestamp() * 1000);
$item = new UTCDateTime($item->format('Uv'));
}
});
}
Expand Down
11 changes: 6 additions & 5 deletions tests/QueryBuilderTest.php
@@ -1,6 +1,7 @@
<?php
declare(strict_types=1);

use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\DB;
use Jenssegers\Mongodb\Collection;
use Jenssegers\Mongodb\Query\Builder;
Expand Down Expand Up @@ -545,14 +546,14 @@ public function testUpdateSubdocument()
public function testDates()
{
DB::collection('users')->insert([
['name' => 'John Doe', 'birthday' => new UTCDateTime(1000 * strtotime("1980-01-01 00:00:00"))],
['name' => 'Jane Doe', 'birthday' => new UTCDateTime(1000 * strtotime("1981-01-01 00:00:00"))],
['name' => 'Robert Roe', 'birthday' => new UTCDateTime(1000 * strtotime("1982-01-01 00:00:00"))],
['name' => 'Mark Moe', 'birthday' => new UTCDateTime(1000 * strtotime("1983-01-01 00:00:00"))],
['name' => 'John Doe', 'birthday' => new UTCDateTime(Date::parse("1980-01-01 00:00:00")->format('Uv'))],
['name' => 'Jane Doe', 'birthday' => new UTCDateTime(Date::parse("1981-01-01 00:00:00")->format('Uv'))],
['name' => 'Robert Roe', 'birthday' => new UTCDateTime(Date::parse("1982-01-01 00:00:00")->format('Uv'))],
['name' => 'Mark Moe', 'birthday' => new UTCDateTime(Date::parse("1983-01-01 00:00:00")->format('Uv'))],
]);

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

Expand Down

0 comments on commit 6b291d7

Please sign in to comment.