Skip to content

Commit 6d16a8a

Browse files
authored
Merge pull request #27 from AndrewMast/feat/is-now-or-past-future
Add `isNowOrFuture()` and `isNowOrPast()` methods
2 parents eeba459 + 924e42e commit 6d16a8a

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

src/Carbon/CarbonInterface.php

+24
Original file line numberDiff line numberDiff line change
@@ -2889,6 +2889,30 @@ public static function isModifiableUnit($unit): bool;
28892889
*/
28902890
public static function isMutable(): bool;
28912891

2892+
/**
2893+
* Determines if the instance is now or in the future, ie. greater (after) than or equal to now.
2894+
*
2895+
* @example
2896+
* ```
2897+
* Carbon::now()->isNowOrFuture(); // true
2898+
* Carbon::now()->addHours(5)->isNowOrFuture(); // true
2899+
* Carbon::now()->subHours(5)->isNowOrFuture(); // false
2900+
* ```
2901+
*/
2902+
public function isNowOrFuture(): bool;
2903+
2904+
/**
2905+
* Determines if the instance is now or in the past, ie. less (before) than or equal to now.
2906+
*
2907+
* @example
2908+
* ```
2909+
* Carbon::now()->isNowOrPast(); // true
2910+
* Carbon::now()->subHours(5)->isNowOrPast(); // true
2911+
* Carbon::now()->addHours(5)->isNowOrPast(); // false
2912+
* ```
2913+
*/
2914+
public function isNowOrPast(): bool;
2915+
28922916
/**
28932917
* Determines if the instance is in the past, ie. less (before) than now.
28942918
*

src/Carbon/Traits/Comparison.php

+30
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,36 @@ public function isPast(): bool
460460
return $this->lessThan($this->nowWithSameTz());
461461
}
462462

463+
/**
464+
* Determines if the instance is now or in the future, ie. greater (after) than or equal to now.
465+
*
466+
* @example
467+
* ```
468+
* Carbon::now()->isNowOrFuture(); // true
469+
* Carbon::now()->addHours(5)->isNowOrFuture(); // true
470+
* Carbon::now()->subHours(5)->isNowOrFuture(); // false
471+
* ```
472+
*/
473+
public function isNowOrFuture(): bool
474+
{
475+
return $this->greaterThanOrEqualTo($this->nowWithSameTz());
476+
}
477+
478+
/**
479+
* Determines if the instance is now or in the past, ie. less (before) than or equal to now.
480+
*
481+
* @example
482+
* ```
483+
* Carbon::now()->isNowOrPast(); // true
484+
* Carbon::now()->subHours(5)->isNowOrPast(); // true
485+
* Carbon::now()->addHours(5)->isNowOrPast(); // false
486+
* ```
487+
*/
488+
public function isNowOrPast(): bool
489+
{
490+
return $this->lessThanOrEqualTo($this->nowWithSameTz());
491+
}
492+
463493
/**
464494
* Determines if the instance is a leap year.
465495
*

tests/Carbon/IsTest.php

+30
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,36 @@ public function testNowIsPastFalse()
240240
$this->assertFalse(Carbon::now()->isPast());
241241
}
242242

243+
public function testIsNowOrFutureTrue()
244+
{
245+
$this->assertTrue(Carbon::now()->addSecond()->isNowOrFuture());
246+
}
247+
248+
public function testIsNowOrFutureFalse()
249+
{
250+
$this->assertFalse(Carbon::now()->subSecond()->isNowOrFuture());
251+
}
252+
253+
public function testNowIsNowOrFutureTrue()
254+
{
255+
$this->assertTrue(Carbon::now()->isNowOrFuture());
256+
}
257+
258+
public function testIsNowOrPastTrue()
259+
{
260+
$this->assertTrue(Carbon::now()->subSecond()->isNowOrPast());
261+
}
262+
263+
public function testIsNowOrPastFalse()
264+
{
265+
$this->assertFalse(Carbon::now()->addSecond()->isNowOrPast());
266+
}
267+
268+
public function testNowIsNowOrPastTrue()
269+
{
270+
$this->assertTrue(Carbon::now()->isNowOrPast());
271+
}
272+
243273
public function testIsLeapYearTrue()
244274
{
245275
$this->assertTrue(Carbon::createFromDate(2016, 1, 1)->isLeapYear());

tests/CarbonImmutable/IsTest.php

+30
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,36 @@ public function testNowIsPastFalse()
237237
$this->assertFalse(Carbon::now()->isPast());
238238
}
239239

240+
public function testIsNowOrFutureTrue()
241+
{
242+
$this->assertTrue(Carbon::now()->addSecond()->isNowOrFuture());
243+
}
244+
245+
public function testIsNowOrFutureFalse()
246+
{
247+
$this->assertFalse(Carbon::now()->subSecond()->isNowOrFuture());
248+
}
249+
250+
public function testNowIsNowOrFutureTrue()
251+
{
252+
$this->assertTrue(Carbon::now()->isNowOrFuture());
253+
}
254+
255+
public function testIsNowOrPastTrue()
256+
{
257+
$this->assertTrue(Carbon::now()->subSecond()->isNowOrPast());
258+
}
259+
260+
public function testIsNowOrPastFalse()
261+
{
262+
$this->assertFalse(Carbon::now()->addSecond()->isNowOrPast());
263+
}
264+
265+
public function testNowIsNowOrPastTrue()
266+
{
267+
$this->assertTrue(Carbon::now()->isNowOrPast());
268+
}
269+
240270
public function testIsLeapYearTrue()
241271
{
242272
$this->assertTrue(Carbon::createFromDate(2016, 1, 1)->isLeapYear());

0 commit comments

Comments
 (0)