@@ -13,22 +13,19 @@ object AlarmUtils {
1313 set(Calendar .MILLISECOND , 0 )
1414 }
1515
16- // Drop seconds/millis from 'now' to allow setting an alarm for the current minute without skipping to tomorrow
17- val nowMinute = Calendar .getInstance().apply {
18- set(Calendar .SECOND , 0 )
19- set(Calendar .MILLISECOND , 0 )
20- }
21-
16+ // Use real current time for comparison to ensure strictly future triggers
17+ val now = Calendar .getInstance()
18+
2219 if (daysOfWeek == 0 ) {
23- // One-time alarm: if time already passed today (comparing only up to minute precision) , set for tomorrow
24- if (alarm.before(nowMinute )) {
20+ // One-time alarm: if time already passed today, set for tomorrow
21+ if (! alarm.after(now )) {
2522 alarm.add(Calendar .DAY_OF_MONTH , 1 )
2623 }
2724 return alarm.timeInMillis
2825 }
2926
3027 // Repeating alarm: find the next matching day
31- for (i in 0 .. 6 ) {
28+ for (i in 0 .. 14 ) { // Look further ahead to be safe (though 7 is enough)
3229 val candidate = Calendar .getInstance().apply {
3330 timeInMillis = alarm.timeInMillis
3431 add(Calendar .DAY_OF_MONTH , i)
@@ -37,7 +34,8 @@ object AlarmUtils {
3734 // Our bitmask: bit 0=Sunday, bit 1=Monday, ..., bit 6=Saturday
3835 val dayBit = candidate.get(Calendar .DAY_OF_WEEK ) - 1
3936 if (daysOfWeek and (1 shl dayBit) != 0 ) {
40- if (i == 0 && candidate.before(nowMinute)) continue
37+ // If today, candidate must be strictly in the future
38+ if (i == 0 && ! candidate.after(now)) continue
4139 return candidate.timeInMillis
4240 }
4341 }
0 commit comments