Last active
June 19, 2022 10:56
-
-
Save hmemcpy/e4f13d8edb4f6351c66a59b84640fdb7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.time.temporal.TemporalAdjusters | |
import java.time.{DayOfWeek, LocalDate, YearMonth} | |
import scala.collection.mutable.ListBuffer | |
import enumeratum.values.{IntEnum, IntEnumEntry} | |
case class Holiday(name: String, date: LocalDate) { | |
val observed: LocalDate = | |
date.getDayOfWeek match { | |
case DayOfWeek.SATURDAY => date.minusDays(1) | |
case DayOfWeek.SUNDAY => date.plusDays(1) | |
case _ => date | |
} | |
} | |
object BankHolidays { | |
sealed abstract class Occurrence(val value: Int) extends IntEnumEntry | |
object Occurrence extends IntEnum[Occurrence] { | |
case object First extends Occurrence(1) | |
case object Second extends Occurrence(2) | |
case object Third extends Occurrence(3) | |
case object Fourth extends Occurrence(4) | |
case object Last extends Occurrence(5) | |
val values = findValues | |
} | |
def all(year: Int): List[Holiday] = { | |
val thirdMondayInJanuary = findDay(year, Month.JANUARY, DayOfWeek.MONDAY, Occurrence.Third) | |
val thirdMondayInFebruary = findDay(year, Month.FEBRUARY, DayOfWeek.MONDAY, Occurrence.Third) | |
val lastMondayInMay = findDay(year, Month.MAY, DayOfWeek.MONDAY, Occurrence.Last) | |
val firstMondayInSeptember = findDay(year, Month.SEPTEMBER, DayOfWeek.MONDAY, Occurrence.First) | |
val secondMondayInOctober = findDay(year, Month.OCTOBER, DayOfWeek.MONDAY, Occurrence.Second) | |
val fourthThursdayInNovember = findDay(year, Month.NOVEMBER, DayOfWeek.THURSDAY, Occurrence.Fourth) | |
val holidays = new ListBuffer[Holiday]() | |
.addOne(Holiday("New Year's Day", LocalDate.of(year, Month.JANUARY, 1))) | |
.addOne(Holiday("Martin Luther King, Jr. Day", thirdMondayInJanuary)) | |
.addOne(Holiday("Washington's Birthday (Presidents Day)", thirdMondayInFebruary)) | |
.addOne(Holiday("Memorial Day", lastMondayInMay)) | |
if (year >= 2021) | |
holidays | |
.addOne(Holiday("Juneteenth", LocalDate.of(year, Month.JUNE, 19))) | |
holidays | |
.addOne(Holiday("Independence Day", LocalDate.of(year, Month.JULY, 4))) | |
.addOne(Holiday("Labor Day", firstMondayInSeptember)) | |
.addOne(Holiday("Columbus Day", secondMondayInOctober)) | |
.addOne(Holiday("Veterans Day", LocalDate.of(year, Month.NOVEMBER, 11))) | |
.addOne(Holiday("Thanksgiving Day", fourthThursdayInNovember)) | |
.addOne(Holiday("Christmas Day", LocalDate.of(year, Month.DECEMBER, 25))) | |
holidays.sortBy(_.date).result() | |
} | |
private def findDay(year: Int, month: java.time.Month, day: DayOfWeek, occurrence: Occurrence) = { | |
val ym = YearMonth.of(year, month) | |
val date = ym.atDay(1) | |
date.`with`(TemporalAdjusters.dayOfWeekInMonth(occurrence.value, day)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment