diff --git a/src/Traits/HasIntervals.php b/src/Traits/HasIntervals.php index d071ea9..d184aef 100644 --- a/src/Traits/HasIntervals.php +++ b/src/Traits/HasIntervals.php @@ -152,6 +152,38 @@ public function untilMidnightTonight(): static ); } + public function untilEndOfMinute(): static + { + $now = new DateTimeImmutable; + + $endOfMinuteTimestamp = $now->setTime( + (int) $now->format('H'), + (int) $now->format('i'), + 59, + )->getTimestamp(); + + return $this->everySeconds( + seconds: $endOfMinuteTimestamp - $this->getCurrentTimestamp(), + timeToLiveKey: 'end_of_minute' + ); + } + + public function untilEndOfHour(): static + { + $now = new DateTimeImmutable; + + $endOfHourTimestamp = $now->setTime( + (int) $now->format('H'), + 59, + 59, + )->getTimestamp(); + + return $this->everySeconds( + seconds: $endOfHourTimestamp - $this->getCurrentTimestamp(), + timeToLiveKey: 'end_of_hour' + ); + } + /** * Get the current timestamp */ diff --git a/tests/Unit/LimitTest.php b/tests/Unit/LimitTest.php index 2c086fd..e77bd59 100644 --- a/tests/Unit/LimitTest.php +++ b/tests/Unit/LimitTest.php @@ -68,3 +68,35 @@ expect($limit->getRemainingSeconds())->toEqual(60); }); + +test('you can create a limiter until end of minute', function () { + $now = new DateTimeImmutable; + + $endOfMinuteTimestamp = $now->setTime( + (int) $now->format('H'), + (int) $now->format('i'), + 59, + )->getTimestamp(); + + $seconds = $endOfMinuteTimestamp - (new DateTimeImmutable)->getTimestamp(); + + $limit = Limit::allow(10)->untilEndOfMinute(); + + expect($limit->getReleaseInSeconds())->toEqual($seconds); +}); + +test('you can create a limiter until end of hour', function () { + $now = new DateTimeImmutable; + + $endOfHourTimestamp = $now->setTime( + (int) $now->format('H'), + 59, + 59, + )->getTimestamp(); + + $seconds = $endOfHourTimestamp - (new DateTimeImmutable)->getTimestamp(); + + $limit = Limit::allow(10)->untilEndOfHour(); + + expect($limit->getReleaseInSeconds())->toEqual($seconds); +});