From 877978dae87d18c8bb0a4a0b530cb2f31fc60a39 Mon Sep 17 00:00:00 2001 From: JonPurvis Date: Fri, 28 Nov 2025 00:41:57 +0000 Subject: [PATCH 1/2] add untilEndOfHour and untilEndOfMinute intervals --- src/Traits/HasIntervals.php | 32 ++++++++++++++++++++++++++++++++ tests/Unit/LimitTest.php | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) 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); +}); From 81473e777b576fd94b1d83f31255de4a32fec10c Mon Sep 17 00:00:00 2001 From: JonPurvis Date: Fri, 28 Nov 2025 12:40:39 +0000 Subject: [PATCH 2/2] run tests