Skip to content

Commit

Permalink
Add LocalDateTime::isFuture() and isPast()
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Apr 17, 2019
1 parent fa89359 commit a1bbf60
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/LocalDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,36 @@ public function isAfterOrEqualTo(LocalDateTime $that) : bool
return $this->compareTo($that) >= 0;
}

/**
* Returns whether this LocalDateTime is in the future, in the given time-zone, according to the given clock.
*
* If no clock is provided, the system clock is used.
*
* @param TimeZone $timeZone
* @param Clock|null $clock
*
* @return bool
*/
public function isFuture(TimeZone $timeZone, Clock $clock = null) : bool
{
return $this->isAfter(LocalDateTime::now($timeZone, $clock));
}

/**
* Returns whether this LocalDateTime is in the past, in the given time-zone, according to the given clock.
*
* If no clock is provided, the system clock is used.
*
* @param TimeZone $timeZone
* @param Clock|null $clock
*
* @return bool
*/
public function isPast(TimeZone $timeZone, Clock $clock = null) : bool
{
return $this->isBefore(LocalDateTime::now($timeZone, $clock));
}

/**
* Converts this LocalDateTime to a native DateTime object.
*
Expand Down
36 changes: 36 additions & 0 deletions tests/LocalDateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Brick\DateTime\Period;
use Brick\DateTime\TimeZone;
use Brick\DateTime\TimeZoneOffset;
use Brick\DateTime\ZonedDateTime;

/**
* Unit tests for class LocalDateTime.
Expand Down Expand Up @@ -1257,6 +1258,41 @@ public function providerCompareTo() : array
];
}

/**
* @dataProvider providerForPastFuture
*/
public function testIsFuture(int $clockTimestamp, string $localDateTime, string $offset, bool $isFuture)
{
$clock = new FixedClock(Instant::of($clockTimestamp));
$localDateTime = LocalDateTime::parse($localDateTime);
$timeZone = TimeZoneOffset::parse($offset);
$this->assertSame($isFuture, $localDateTime->isFuture($timeZone, $clock));
}

/**
* @dataProvider providerForPastFuture
*/
public function testIsPast(int $clockTimestamp, string $localDateTime, string $offset, bool $isFuture)
{
$clock = new FixedClock(Instant::of($clockTimestamp));
$localDateTime = LocalDateTime::parse($localDateTime);
$timeZone = TimeZoneOffset::parse($offset);
$this->assertSame(! $isFuture, $localDateTime->isPast($timeZone, $clock));
}

/**
* @return array
*/
public function providerForPastFuture()
{
return [
[1234567890, '2009-02-14T00:31:29', '+01:00', false],
[1234567890, '2009-02-14T00:31:31', '+01:00', true],
[2345678901, '2044-04-30T17:28:20', '-08:00', false],
[2345678901, '2044-04-30T17:28:22', '-08:00', true]
];
}

/**
* @dataProvider providerToDateTime
*
Expand Down

0 comments on commit a1bbf60

Please sign in to comment.