Skip to content

Commit

Permalink
Merge pull request #580 from mmateu/master
Browse files Browse the repository at this point in the history
Synchronize date output between calev-core and systemd-analyze
  • Loading branch information
eikek authored May 27, 2024
2 parents 9df2ae8 + 77e8bed commit 37f2ab6
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ object Value {

def contains(n: Int): Boolean =
n >= start && n <= end &&
(rep == None || Single(start, rep).contains(n))
(rep.isEmpty || Single(start, rep).contains(n))

def asString: String =
rep match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,38 @@ object DefaultTrigger extends Trigger {
}

case _ =>
val (ref, comp) = calc.components
val prevFlag = calc.flag
val (ref: Int, comp: Component) = calc.components
val prevFlag: Flag = calc.flag

if (comp.contains(ref) && prevFlag != Flag.First)
if (comp.contains(ref) && prevFlag != Flag.First) {
run(calc.copy(flag = Flag.Exact).nextPos)
else
comp.findFirst(ref + 1, calc.maxValue) match {
} else
comp.findFirst(min = ref + 1, max = calc.maxValue) match {
case Some(v) =>
run(calc.set(v, Flag.Next).atStartBelowCurrent.nextPos)

case None =>
val n =
comp.findFirst(calc.minValue, calc.maxValue).getOrElse(calc.minValue)
run(calc.set(n, Flag.First).nextPos)
if (calc.isMonth) {
comp.findFirst(min = calc.minValue, max = calc.maxValue) match {
case Some(v) =>
run(calc.set(v, Flag.First).atStartBelowCurrent.nextPos)
case None =>
val pos = calc
.set(
comp
.findFirst(calc.minValue, calc.maxValue)
.getOrElse(calc.minValue),
Flag.First
)
.nextPos
run(pos)
}
} else {
val n =
comp.findFirst(calc.minValue, calc.maxValue).getOrElse(calc.minValue)
run(calc.set(n, Flag.First).nextPos)
}

}
}

Expand All @@ -112,7 +130,7 @@ object DefaultTrigger extends Trigger {
}

case class DateTime(date: Date, time: Time) {
def toLocalDateTime =
def toLocalDateTime: LocalDateTime =
LocalDateTime.of(date.toLocalDate, time.toLocalTime)

def toZonedDateTime(zone: ZoneId): ZonedDateTime =
Expand Down Expand Up @@ -171,7 +189,7 @@ object DefaultTrigger extends Trigger {
}

case class Calc(flag: Flag, date: DateTime, pos: DateTime.Pos, ce: CalEvent) {
def components =
def components: (Int, Component) =
pos match {
case DateTime.Pos.Sec =>
(date.time.second, ce.time.seconds)
Expand Down Expand Up @@ -228,7 +246,7 @@ object DefaultTrigger extends Trigger {
def set(value: Int, flag: Flag): Calc =
set(value, pos).copy(flag = flag)

private def set(value: Int, pos: DateTime.Pos): Calc =
def set(value: Int, pos: DateTime.Pos): Calc =
pos match {
case DateTime.Pos.Sec =>
copy(date = date.copy(time = date.time.copy(second = value)))
Expand All @@ -243,6 +261,9 @@ object DefaultTrigger extends Trigger {
case DateTime.Pos.Year =>
copy(date = date.copy(date = date.date.copy(year = value)))
}

def isMonth: Boolean =
pos == DateTime.Pos.Month
}

object Calc {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,66 @@ class CalEventTest extends FunSuite {
assertEquals(next, ZonedDateTime.parse("2024-02-01T00:00+01:00"))
}

test(
"expression for the daily run on some month with potential next run on next year should start at the beginning of the month next year"
) {
val expression = CalEvent.unsafe("*-2-* 10:00:00")

val next = expression.nextElapse(LocalDateTime.parse("2024-03-20T10:01:00"))
assertEquals(
next.get,
LocalDateTime.parse("2025-02-01T10:00")
)
}

test(
"expression for the daily run on some month with ref date before (1 month before) aforementioned month should start at the beginning of the month of the same year"
) {
val expression = CalEvent.unsafe("*-2-* 10:00:00")

val next = expression.nextElapse(LocalDateTime.parse("2024-01-20T10:01:00"))
assertEquals(
next.get,
LocalDateTime.parse("2024-02-01T10:00")
)
}

test(
"expression for the daily run on some month with ref date before (few months before) aforementioned month should start at the beginning of the month of the same year"
) {
val expression = CalEvent.unsafe("*-4-* 10:00:00")

val next = expression.nextElapse(LocalDateTime.parse("2024-01-20T10:01:00"))
assertEquals(
next.get,
LocalDateTime.parse("2024-04-01T10:00")
)
}

test(
"expression for the daily run on some month with ref date before (on the same month) aforementioned month should start at the beginning of the month of the same year"
) {
val expression = CalEvent.unsafe("*-4-25 10:00:00")

val next = expression.nextElapse(LocalDateTime.parse("2024-04-20T10:01:00"))
assertEquals(
next.get,
LocalDateTime.parse("2024-04-25T10:00")
)
}

test(
"expression for the monthly run on some month with potential next run on next year should start at the beginning of the month next year"
) {
val expression = CalEvent.unsafe("*-4-25 10:00:00")

val next = expression.nextElapse(LocalDateTime.parse("2024-08-20T10:01:00"))
assertEquals(
next.get,
LocalDateTime.parse("2025-04-25T10: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 37f2ab6

Please sign in to comment.