Skip to content

Instantly share code, notes, and snippets.

@pathikrit
Last active June 25, 2022 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pathikrit/4a768583fdb2da9ea851806eb7ad94d4 to your computer and use it in GitHub Desktop.
Save pathikrit/4a768583fdb2da9ea851806eb7ad94d4 to your computer and use it in GitHub Desktop.
Chron.scala
// A simply port of Google Task's recurring task scheduler to Scala 3 ADT:
// https://tasks.google.com/embed/?origin=https://calendar.google.com&fullWidth=1
import java.time._
enum Cadence:
case Daily
case Weekly(on: Set[DayOfWeek] = Set(DayOfWeek.SUNDAY))
case Monthly(on: Set[DayOfMonth] = Set(DayOfMonth.Day(1)))
case Yearly(on: Set[MonthDay] = Set(MonthDay.of(1, 1)))
enum DayOfMonth:
case Day(day: Int)
case LastDay(daysFromEnd: Int = 0)
case First(day: DayOfWeek)
case Second(day: DayOfWeek)
case Third(day: DayOfWeek)
case Fourth(day: DayOfWeek)
case Last(day: DayOfWeek)
enum End:
case Never
case On(date: LocalDate)
case After(occurences: Int)
case class Schedule(
every: Int,
cadence: Cadence,
at: LocalTime = LocalTime.MIDNIGHT,
starts: LocalDate = LocalDate.now(),
ends: End = End.Never
)
// Example usage to schedule every 2 months on the 2nd day of month and the last Friday
Schedule(
every = 2,
cadence = Cadence.Monthly(
on = Set(
DayOfMonth.Day(2),
DayOfMonth.Last(DayOfWeek.FRIDAY)
)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment