Skip to content

[Ignore] UTCDateTime conversion now includes milliseconds #1963

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
5 changes: 3 additions & 2 deletions src/Jenssegers/Mongodb/Auth/DatabaseTokenRepository.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Jenssegers\Mongodb\Eloquent;

use Carbon\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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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