Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
Create UTCDateTime from DateTimeInterface objects (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN authored Jul 12, 2023
1 parent 60a22f4 commit 843904b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/Auth/DatabaseTokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protected function getPayload($email, $token)
return [
'email' => $email,
'token' => $this->hasher->make($token),
'created_at' => new UTCDateTime(Date::now()->format('Uv')),
'created_at' => new UTCDateTime(Date::now()),
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function getDateFormat()
*/
public function freshTimestamp()
{
return new UTCDateTime(Date::now()->format('Uv'));
return new UTCDateTime(Date::now());
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ protected function performUpdate($query, array $options = [])
$options = $this->inheritConnectionOptions($options);

$wheres = $this->compileWheres();
$result = $this->collection->UpdateMany($wheres, $query, $options);
$result = $this->collection->updateMany($wheres, $query, $options);
if (1 == (int) $result->isAcknowledged()) {
return $result->getModifiedCount() ? $result->getModifiedCount() : $result->getUpsertedCount();
}
Expand Down Expand Up @@ -981,18 +981,18 @@ protected function compileWheres(): array
if (is_array($where['value'])) {
array_walk_recursive($where['value'], function (&$item, $key) {
if ($item instanceof DateTimeInterface) {
$item = new UTCDateTime($item->format('Uv'));
$item = new UTCDateTime($item);
}
});
} else {
if ($where['value'] instanceof DateTimeInterface) {
$where['value'] = new UTCDateTime($where['value']->format('Uv'));
$where['value'] = new UTCDateTime($where['value']);
}
}
} elseif (isset($where['values'])) {
array_walk_recursive($where['values'], function (&$item, $key) {
if ($item instanceof DateTimeInterface) {
$item = new UTCDateTime($item->format('Uv'));
$item = new UTCDateTime($item);
}
});
}
Expand Down
16 changes: 8 additions & 8 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,19 +600,19 @@ public function testUpdateSubdocument()
public function testDates()
{
DB::collection('users')->insert([
['name' => 'John Doe', 'birthday' => new UTCDateTime(Date::parse('1980-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.1')->format('Uv'))],
['name' => 'Frank White', 'birthday' => new UTCDateTime(Date::parse('1960-01-01 12:12:12.1')->format('Uv'))],
['name' => 'John Doe', 'birthday' => new UTCDateTime(Date::parse('1980-01-01 00:00:00'))],
['name' => 'Robert Roe', 'birthday' => new UTCDateTime(Date::parse('1982-01-01 00:00:00'))],
['name' => 'Mark Moe', 'birthday' => new UTCDateTime(Date::parse('1983-01-01 00:00:00.1'))],
['name' => 'Frank White', 'birthday' => new UTCDateTime(Date::parse('1960-01-01 12:12:12.1'))],
]);

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

$user = DB::collection('users')
->where('birthday', new UTCDateTime(Date::parse('1960-01-01 12:12:12.1')->format('Uv')))
->where('birthday', new UTCDateTime(Date::parse('1960-01-01 12:12:12.1')))
->first();
$this->assertEquals('Frank White', $user['name']);

Expand All @@ -629,8 +629,8 @@ public function testDates()
public function testImmutableDates()
{
DB::collection('users')->insert([
['name' => 'John Doe', 'birthday' => new UTCDateTime(Date::parse('1980-01-01 00:00:00')->format('Uv'))],
['name' => 'Robert Roe', 'birthday' => new UTCDateTime(Date::parse('1982-01-01 00:00:00')->format('Uv'))],
['name' => 'John Doe', 'birthday' => new UTCDateTime(Date::parse('1980-01-01 00:00:00'))],
['name' => 'Robert Roe', 'birthday' => new UTCDateTime(Date::parse('1982-01-01 00:00:00'))],
]);

$users = DB::collection('users')->where('birthday', '=', new DateTimeImmutable('1980-01-01 00:00:00'))->get();
Expand Down

0 comments on commit 843904b

Please sign in to comment.