Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/Extensions/DateTimeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Xefi\Faker\Extensions;

use DateTime;
use DateTimeImmutable;
use DateTimeInterface;

class DateTimeExtension extends Extension
{
Expand Down Expand Up @@ -38,25 +40,30 @@ class DateTimeExtension extends Extension
'America/Toronto',
];

protected function formatTimestamp(DateTime|int|string $timestamp): int
protected function formatTimestamp(DateTimeInterface|int|string $timestamp): int
{
if (is_int($timestamp)) {
return $timestamp;
}

if ($timestamp instanceof DateTime) {
if ($timestamp instanceof DateTimeInterface) {
return $timestamp->getTimestamp();
}

return strtotime($timestamp);
}

public function dateTime(DateTime|int|string $fromTimestamp = '-30 years', DateTime|int|string $toTimestamp = 'now'): DateTime
public function dateTime(DateTimeInterface|int|string $fromTimestamp = '-30 years', DateTimeInterface|int|string $toTimestamp = 'now'): DateTime
{
return new DateTime('@'.$this->timestamp($fromTimestamp, $toTimestamp));
}

public function timestamp(DateTime|int|string $fromTimestamp = '-30 years', DateTime|int|string $toTimestamp = 'now'): int
public function dateTimeImmutable(DateTimeInterface|int|string $fromTimestamp = '-30 years', DateTimeInterface|int|string $toTimestamp = 'now'): DateTimeImmutable
{
return new DateTimeImmutable('@'.$this->timestamp($fromTimestamp, $toTimestamp));
}

public function timestamp(DateTimeInterface|int|string $fromTimestamp = '-30 years', DateTimeInterface|int|string $toTimestamp = 'now'): int
{
return $this->randomizer->getInt($this->formatTimestamp($fromTimestamp), $this->formatTimestamp($toTimestamp));
}
Expand Down
20 changes: 20 additions & 0 deletions tests/Unit/Extensions/DatetimeExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Xefi\Faker\Tests\Unit\Extensions;

use DateTime;
use DateTimeImmutable;

final class DatetimeExtensionTest extends TestCase
{
Expand Down Expand Up @@ -71,6 +72,25 @@ public function testDateTime(): void
$toDateTime = new DateTime('now');

foreach ($results as $result) {
$this->assertInstanceOf(DateTime::class, $result);
$this->assertGreaterThanOrEqual($fromDateTime->getTimestamp(), $result->getTimestamp());
$this->assertLessThanOrEqual($toDateTime->getTimestamp(), $result->getTimestamp());
}
}

public function testDateTimeImmutable(): void
{
$results = [];

for ($i = 0; $i < 50; $i++) {
$results[] = $this->faker->dateTimeImmutable('-30 years', 'now');
}

$fromDateTime = new DateTimeImmutable('-30 years');
$toDateTime = new DateTimeImmutable('now');

foreach ($results as $result) {
$this->assertInstanceOf(DateTimeImmutable::class, $result);
$this->assertGreaterThanOrEqual($fromDateTime->getTimestamp(), $result->getTimestamp());
$this->assertLessThanOrEqual($toDateTime->getTimestamp(), $result->getTimestamp());
}
Expand Down
Loading