Skip to content

Commit

Permalink
Merge pull request #552 from Sheryv/master
Browse files Browse the repository at this point in the history
Fix for DateTimeException: Invalid date 'FEBRUARY 31'
  • Loading branch information
eikek authored Mar 20, 2024
2 parents 9411ae0 + 40a0746 commit c7983f1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ object DefaultTrigger extends Trigger {
}

case class Date(year: Int, month: Int, day: Int) {
def toLocalDate: LocalDate =
LocalDate.of(year, month, day)
def toLocalDate: LocalDate = {
val lastDayOfMonth = YearMonth.of(year, month).atEndOfMonth().getDayOfMonth
LocalDate.of(year, month, math.min(day, lastDayOfMonth))
}

def incYear: Date =
Date(year + 1, month, day)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ trait Trigger {
def next(ref: ZonedDateTime, ev: CalEvent): Option[ZonedDateTime]

def nextRepeat(count: Int)(ref: ZonedDateTime, ev: CalEvent): List[ZonedDateTime] = {
require(count >= 0, "Repeat count cannot be negative")

@annotation.tailrec
def go(result: List[ZonedDateTime], cur: ZonedDateTime, i: Int): List[ZonedDateTime] =
if (i == 0) result
Expand All @@ -23,5 +25,4 @@ trait Trigger {

go(Nil, ref, count).reverse
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ class CalEventTest extends FunSuite {
assertEquals(next.toLocalTime, LocalTime.of(21, 10, 0))
}

test("day after last January is not 31st February") {
val expression = CalEvent(AllWeekdays, date(All, 2.c, All), time(0 #/ 6, 0.c, 0.c))

val next = expression
.nextElapse(OffsetDateTime.parse("2024-01-31T18:00+01:00").toZonedDateTime)
.get

assertEquals(next, ZonedDateTime.parse("2024-02-01T00:00+01:00"))
}

private def zdt(y: Int, month: Int, d: Int, h: Int, min: Int, sec: Int): ZonedDateTime =
ZonedDateTime.of(LocalDate.of(y, month, d), LocalTime.of(h, min, sec), ZoneOffset.UTC)
}

0 comments on commit c7983f1

Please sign in to comment.