Skip to content

Commit 092f3b2

Browse files
authored
add untilEndOfMinute and untilEndOfHour intervals (#24)
* add untilEndOfHour and untilEndOfMinute intervals * run tests
1 parent c1367dd commit 092f3b2

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/Traits/HasIntervals.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,38 @@ public function untilMidnightTonight(): static
152152
);
153153
}
154154

155+
public function untilEndOfMinute(): static
156+
{
157+
$now = new DateTimeImmutable;
158+
159+
$endOfMinuteTimestamp = $now->setTime(
160+
(int) $now->format('H'),
161+
(int) $now->format('i'),
162+
59,
163+
)->getTimestamp();
164+
165+
return $this->everySeconds(
166+
seconds: $endOfMinuteTimestamp - $this->getCurrentTimestamp(),
167+
timeToLiveKey: 'end_of_minute'
168+
);
169+
}
170+
171+
public function untilEndOfHour(): static
172+
{
173+
$now = new DateTimeImmutable;
174+
175+
$endOfHourTimestamp = $now->setTime(
176+
(int) $now->format('H'),
177+
59,
178+
59,
179+
)->getTimestamp();
180+
181+
return $this->everySeconds(
182+
seconds: $endOfHourTimestamp - $this->getCurrentTimestamp(),
183+
timeToLiveKey: 'end_of_hour'
184+
);
185+
}
186+
155187
/**
156188
* Get the current timestamp
157189
*/

tests/Unit/LimitTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,35 @@
6868

6969
expect($limit->getRemainingSeconds())->toEqual(60);
7070
});
71+
72+
test('you can create a limiter until end of minute', function () {
73+
$now = new DateTimeImmutable;
74+
75+
$endOfMinuteTimestamp = $now->setTime(
76+
(int) $now->format('H'),
77+
(int) $now->format('i'),
78+
59,
79+
)->getTimestamp();
80+
81+
$seconds = $endOfMinuteTimestamp - (new DateTimeImmutable)->getTimestamp();
82+
83+
$limit = Limit::allow(10)->untilEndOfMinute();
84+
85+
expect($limit->getReleaseInSeconds())->toEqual($seconds);
86+
});
87+
88+
test('you can create a limiter until end of hour', function () {
89+
$now = new DateTimeImmutable;
90+
91+
$endOfHourTimestamp = $now->setTime(
92+
(int) $now->format('H'),
93+
59,
94+
59,
95+
)->getTimestamp();
96+
97+
$seconds = $endOfHourTimestamp - (new DateTimeImmutable)->getTimestamp();
98+
99+
$limit = Limit::allow(10)->untilEndOfHour();
100+
101+
expect($limit->getReleaseInSeconds())->toEqual($seconds);
102+
});

0 commit comments

Comments
 (0)