Skip to content

Commit

Permalink
Add implementation for dateTimeInterval and dateTimeThisWeek
Browse files Browse the repository at this point in the history
  • Loading branch information
Bram Ceulemans committed Jan 23, 2022
1 parent f71960a commit d0eae54
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
28 changes: 23 additions & 5 deletions src/Faker/Core/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ protected function getTimestamp($until = 'now')
return strtotime(empty($max) ? 'now' : $max);
}

/**
* Get a DateTime created based on a POSIX-timestamp.
*
* @param int $timestamp the UNIX / POSIX-compatible timestamp
*/
protected function getTimestampDateTime(int $timestamp): \DateTime
{
return new \DateTime('@' . $timestamp);
}

protected function setDefaultTimezone(string $timezone = null): void
{
$this->defaultTimezone = $timezone;
Expand Down Expand Up @@ -72,7 +82,7 @@ protected function setTimezone(\DateTime $dateTime, ?string $timezone): \DateTim
public function dateTime($until = 'now', string $timezone = null): \DateTime
{
return $this->setTimezone(
new \DateTime('@' . $this->unixTime($until)),
$this->getTimestampDateTime($this->unixTime($until)),
$timezone
);
}
Expand All @@ -82,7 +92,7 @@ public function dateTimeAD($until = 'now', string $timezone = null): \DateTime
$min = (PHP_INT_SIZE > 4) ? -62135597361 : -PHP_INT_MAX;

return $this->setTimezone(
new \DateTime('@' . $this->generator->numberBetween($min, $this->getTimestamp($until))),
$this->getTimestampDateTime($this->generator->numberBetween($min, $this->getTimestamp($until))),
$timezone
);
}
Expand All @@ -99,19 +109,27 @@ public function dateTimeBetween($from = '-30 years', $until = 'now', string $tim
$timestamp = $this->generator->numberBetween($start, $end);

return $this->setTimezone(
new \DateTime('@' . $timestamp),
$this->getTimestampDateTime($timestamp),
$timezone
);
}

public function dateTimeInInterval($from = '-30 years', string $interval = '+5 days', string $timezone = null): \DateTime
{
// TODO: Implement dateTimeInInterval() method.
$intervalObject = \DateInterval::createFromDateString($interval);
$datetime = $from instanceof \DateTime ? $from : new \DateTime($from);

$other = (clone $datetime)->add($intervalObject);

$begin = min($datetime, $other);
$end = $datetime === $begin ? $other : $datetime;

return $this->dateTimeBetween($begin, $end, $timezone);
}

public function dateTimeThisWeek($until = 'now', string $timezone = null): \DateTime
{
// TODO: Implement dateTimeThisWeek() method.
return $this->dateTimeBetween('-1 week', $timezone);
}

public function dateTimeThisMonth($until = 'now', string $timezone = null): \DateTime
Expand Down
2 changes: 1 addition & 1 deletion src/Faker/Provider/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public static function dateTimeInInterval($date = '-30 years', $interval = '+5 d
$otherDatetime = clone $datetime;
$otherDatetime->add($intervalObject);

$begin = $datetime > $otherDatetime ? $otherDatetime : $datetime;
$begin = min($datetime, $otherDatetime);
$end = $datetime === $begin ? $otherDatetime : $datetime;

return static::dateTimeBetween(
Expand Down

0 comments on commit d0eae54

Please sign in to comment.