Skip to content

Add isNowOrFuture() and isNowOrPast() methods #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
24 changes: 24 additions & 0 deletions src/Carbon/CarbonInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2889,6 +2889,30 @@ public static function isModifiableUnit($unit): bool;
*/
public static function isMutable(): bool;

/**
* Determines if the instance is now or in the future, ie. greater (after) than or equal to now.
*
* @example
* ```
* Carbon::now()->isNowOrFuture(); // true
* Carbon::now()->addHours(5)->isNowOrFuture(); // true
* Carbon::now()->subHours(5)->isNowOrFuture(); // false
* ```
*/
public function isNowOrFuture(): bool;

/**
* Determines if the instance is now or in the past, ie. less (before) than or equal to now.
*
* @example
* ```
* Carbon::now()->isNowOrPast(); // true
* Carbon::now()->subHours(5)->isNowOrPast(); // true
* Carbon::now()->addHours(5)->isNowOrPast(); // false
* ```
*/
public function isNowOrPast(): bool;

/**
* Determines if the instance is in the past, ie. less (before) than now.
*
Expand Down
30 changes: 30 additions & 0 deletions src/Carbon/Traits/Comparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,36 @@ public function isPast(): bool
return $this->lessThan($this->nowWithSameTz());
}

/**
* Determines if the instance is now or in the future, ie. greater (after) than or equal to now.
*
* @example
* ```
* Carbon::now()->isNowOrFuture(); // true
* Carbon::now()->addHours(5)->isNowOrFuture(); // true
* Carbon::now()->subHours(5)->isNowOrFuture(); // false
* ```
*/
public function isNowOrFuture(): bool
{
return $this->greaterThanOrEqualTo($this->nowWithSameTz());
}

/**
* Determines if the instance is now or in the past, ie. less (before) than or equal to now.
*
* @example
* ```
* Carbon::now()->isNowOrPast(); // true
* Carbon::now()->subHours(5)->isNowOrPast(); // true
* Carbon::now()->addHours(5)->isNowOrPast(); // false
* ```
*/
public function isNowOrPast(): bool
{
return $this->lessThanOrEqualTo($this->nowWithSameTz());
}

/**
* Determines if the instance is a leap year.
*
Expand Down
30 changes: 30 additions & 0 deletions tests/Carbon/IsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,36 @@ public function testNowIsPastFalse()
$this->assertFalse(Carbon::now()->isPast());
}

public function testIsNowOrFutureTrue()
{
$this->assertTrue(Carbon::now()->addSecond()->isNowOrFuture());
}

public function testIsNowOrFutureFalse()
{
$this->assertFalse(Carbon::now()->subSecond()->isNowOrFuture());
}

public function testNowIsNowOrFutureTrue()
{
$this->assertTrue(Carbon::now()->isNowOrFuture());
}

public function testIsNowOrPastTrue()
{
$this->assertTrue(Carbon::now()->subSecond()->isNowOrPast());
}

public function testIsNowOrPastFalse()
{
$this->assertFalse(Carbon::now()->addSecond()->isNowOrPast());
}

public function testNowIsNowOrPastTrue()
{
$this->assertTrue(Carbon::now()->isNowOrPast());
}

public function testIsLeapYearTrue()
{
$this->assertTrue(Carbon::createFromDate(2016, 1, 1)->isLeapYear());
Expand Down
30 changes: 30 additions & 0 deletions tests/CarbonImmutable/IsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,36 @@ public function testNowIsPastFalse()
$this->assertFalse(Carbon::now()->isPast());
}

public function testIsNowOrFutureTrue()
{
$this->assertTrue(Carbon::now()->addSecond()->isNowOrFuture());
}

public function testIsNowOrFutureFalse()
{
$this->assertFalse(Carbon::now()->subSecond()->isNowOrFuture());
}

public function testNowIsNowOrFutureTrue()
{
$this->assertTrue(Carbon::now()->isNowOrFuture());
}

public function testIsNowOrPastTrue()
{
$this->assertTrue(Carbon::now()->subSecond()->isNowOrPast());
}

public function testIsNowOrPastFalse()
{
$this->assertFalse(Carbon::now()->addSecond()->isNowOrPast());
}

public function testNowIsNowOrPastTrue()
{
$this->assertTrue(Carbon::now()->isNowOrPast());
}

public function testIsLeapYearTrue()
{
$this->assertTrue(Carbon::createFromDate(2016, 1, 1)->isLeapYear());
Expand Down
Loading