diff --git a/src/LocalDateTime.php b/src/LocalDateTime.php index bf6ad36..fe7251a 100644 --- a/src/LocalDateTime.php +++ b/src/LocalDateTime.php @@ -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. * diff --git a/tests/LocalDateTimeTest.php b/tests/LocalDateTimeTest.php index 1711a44..6ee0ce6 100644 --- a/tests/LocalDateTimeTest.php +++ b/tests/LocalDateTimeTest.php @@ -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. @@ -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 *