Skip to content

Commit 7b65135

Browse files
Fix LocalDateTime#nextMatch bug (#27)
Closes #26
1 parent e25db2f commit 7b65135

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ kotlin.code.style=official
22

33
GROUP=io.github.kevincianfarini.cardiologist
44
POM_ARTIFACT_ID=cardiologist
5-
VERSION_NAME=0.2.0
5+
VERSION_NAME=0.3.0
66

77
POM_NAME=Cardiologist
88
POM_DESCRIPTION=Build job schedules with kotlinx-datetime and kotlinx-coroutines.

src/commonMain/kotlin/io/github/kevincianfarini/cardiologist/impl/LocalDateTime.kt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package io.github.kevincianfarini.cardiologist.impl
33
import kotlinx.datetime.LocalDateTime
44
import kotlinx.datetime.Month
55
import kotlinx.datetime.number
6-
import kotlin.math.min
76

87
internal fun LocalDateTime.nextMatch(
98
atSeconds: IntRange = 0..59,
@@ -62,10 +61,6 @@ private fun LocalDateTime.nextMonth(
6261
}
6362
}
6463

65-
private operator fun Month.inc(): Month {
66-
return Month.entries[(ordinal + 1) % 12]
67-
}
68-
6964
private fun LocalDateTime.nextDay(
7065
onDaysOfMonth: IntRange,
7166
inMonths: ClosedRange<Month>,
@@ -106,7 +101,12 @@ private fun LocalDateTime.nextHour(
106101
incrementedHour < hour -> nextDay(onDaysOfMonth, inMonths, increment = true).copy(hour = incrementedHour)
107102
incrementedHour in atHours -> copy(hour = incrementedHour)
108103
incrementedHour < atHours.first -> copy(hour = atHours.first, minute = 0, second = 0, nanosecond = 0)
109-
incrementedHour > atHours.last -> nextDay(onDaysOfMonth, inMonths, increment = true).copy(hour = atHours.first)
104+
incrementedHour > atHours.last -> nextDay(onDaysOfMonth, inMonths, increment = true).copy(
105+
hour = atHours.first,
106+
minute = 0,
107+
second = 0,
108+
nanosecond = 0,
109+
)
110110
else -> error("This should be impossible.")
111111
}
112112
}
@@ -127,7 +127,9 @@ private fun LocalDateTime.nextMinute(
127127
incrementedMinute in atMinutes -> copy(minute = incrementedMinute)
128128
incrementedMinute < atMinutes.first -> copy(minute = atMinutes.first, second = 0, nanosecond = 0)
129129
incrementedMinute > atMinutes.last -> nextHour(atHours, onDaysOfMonth, inMonths, increment = true).copy(
130-
minute = atMinutes.first
130+
minute = atMinutes.first,
131+
second = 0,
132+
nanosecond = 0,
131133
)
132134
else -> error("This should be impossible.")
133135
}
@@ -189,3 +191,7 @@ internal fun LocalDateTime.copy(
189191
second: Int = this.second,
190192
nanosecond: Int = this.nanosecond,
191193
) = LocalDateTime(year, monthNumber, dayOfMonth, hour, minute, second, nanosecond)
194+
195+
private operator fun Month.inc(): Month {
196+
return Month.entries[(ordinal + 1) % 12]
197+
}

src/commonTest/kotlin/io/github/kevincianfarini/cardiologist/impl/LocalDateTimeTest.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ class LocalDateTimeTest {
169169
)
170170
)
171171

172+
@Test fun matches_jan_18_2024_to_jan_19_2024() = assertEquals(
173+
expected = LocalDateTime(year = 2024, monthNumber = 1, dayOfMonth = 19, hour = 0, minute = 0),
174+
actual = LocalDateTime(year = 2024, monthNumber = 1, dayOfMonth = 18, hour = 23, minute = 59, second = 48)
175+
.nextMatch(atSeconds = 0..0, atMinutes = 0..0, atHours = 0..0)
176+
)
177+
172178
@Test fun one_month_gap_for_two_years() {
173179
LocalDateTime(year = 2023, monthNumber = 1, dayOfMonth = 1, hour = 0, minute = 0).assertGap(
174180
assertPeriod = DateTimePeriod(months = 1),

0 commit comments

Comments
 (0)