Last active
October 11, 2022 11:49
-
-
Save hmemcpy/38e0e82fdf8b3089b9b021c0ececc4d2 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 enumeratum.values.{IntEnum, IntEnumEntry} | |
import java.time.temporal.TemporalAdjusters | |
import java.time.{DayOfWeek, LocalDate, YearMonth} | |
import scala.annotation.tailrec | |
import scala.collection.mutable.ListBuffer | |
object BankingDays { | |
def nextBankingDay(now: LocalDate, days: Int = 1): LocalDate = { | |
@tailrec | |
def go(candidate: Int, remaining: Int): Int = | |
if (remaining == 0) candidate | |
else { | |
val nextDay = candidate + 1 | |
if (!isBankingDay(now.plusDays(nextDay))) go(nextDay, remaining) | |
else go(nextDay, remaining - 1) | |
} | |
val actualDays = go(0, days) | |
now.plusDays(actualDays) | |
} | |
def isBankingDay(now: LocalDate) = | |
now.getDayOfWeek match { | |
case DayOfWeek.SATURDAY | DayOfWeek.SUNDAY => false | |
case _ => | |
BankHolidays.all(now.getYear).find(isObserved(_, now)) match { | |
case Some(_) => false | |
case None => true | |
} | |
} | |
private def isObserved(holiday: Holiday, date: LocalDate) = | |
// Holidays that are observed on the preceding Friday may still be banking days. | |
// Ignore if Friday, but the actual holiday falls on a Saturday. | |
(holiday.observed.getDayOfWeek, holiday.date.getDayOfWeek) match { | |
case (DayOfWeek.FRIDAY, DayOfWeek.SATURDAY) => false | |
case _ => date == holiday.observed | |
} | |
private[this] 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 | |
} | |
} | |
private[this] object BankHolidays { | |
import java.time.{DayOfWeek, Month} | |
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