Scripts


US Federal Holiday Detection

Here's how to determine whether a date is a US federal holiday using F#. You probably want to curry the first two boolean parameters with the appropriate settings (some departments observe Sunday holidays on Monday, and some observe Saturday holidays on Friday).

open System

let getHoliday sunToMon satToFri (date:DateTime) =
   match date.Month, date.Day, (date.Day-1)/7+1, date.DayOfWeek with
   | 1, 1, _, _ -> "New Year's Day"
   | 1, 2, _, DayOfWeek.Monday when sunToMon -> "New Year's Day (observed)"
   | 1, _, 3, DayOfWeek.Monday -> "Martin Luther King, Jr. Day"
   | 2, _, 3, DayOfWeek.Monday -> "Presidents Day"
   | 5, d, _, DayOfWeek.Monday when d >= 25 -> "Memorial Day" // active pattern matching!
   | 6, 18, _, DayOfWeek.Friday when satToFri -> "Juneteenth (observed)"
   | 6, 19, _, _ -> "Juneteenth Day"
   | 6, 20, _, DayOfWeek.Monday when sunToMon -> "Juneteenth Day (observed)"
   | 7, 3, _, DayOfWeek.Friday when satToFri -> "Independence Day (observed)"
   | 7, 4, _, _ -> "Independence Day"
   | 7, 5, _, DayOfWeek.Monday when sunToMon -> "Independence Day (observed)"
   | 9, _, 1, DayOfWeek.Monday -> "Labor Day"
   | 10, _, 2, DayOfWeek.Monday -> "Columbus Day"
   | 11, 10, _, DayOfWeek.Friday when satToFri -> "Veterans Day (observed)"
   | 11, 11, _, _ -> "Veterans Day"
   | 11, 12, _, DayOfWeek.Monday when sunToMon -> "Veterans Day (observed)"
   | 11, _, 4, DayOfWeek.Thursday -> "Thanksgiving Day"
   | 12, 24, _, DayOfWeek.Friday when satToFri -> "Christmas Day (observed)"
   | 12, 25, _, _ -> "Christmas Day"
   | 12, 26, _, DayOfWeek.Monday when sunToMon -> "Christmas Day (observed)"
   | 12, 31, _, DayOfWeek.Friday when satToFri -> "New Year's Day (observed)"
   | _ -> null
namespace System
val getHoliday : sunToMon:bool -> satToFri:bool -> date:DateTime -> string
val sunToMon : bool
val satToFri : bool
val date : DateTime
Multiple items
[<Struct>] type DateTime = new : year: int * month: int * day: int -> unit + 10 overloads member Add : value: TimeSpan -> DateTime member AddDays : value: float -> DateTime member AddHours : value: float -> DateTime member AddMilliseconds : value: float -> DateTime member AddMinutes : value: float -> DateTime member AddMonths : months: int -> DateTime member AddSeconds : value: float -> DateTime member AddTicks : value: int64 -> DateTime member AddYears : value: int -> DateTime ...
<summary>Represents an instant in time, typically expressed as a date and time of day.</summary>

--------------------
DateTime ()
   (+0 other overloads)
DateTime(ticks: int64) : DateTime
   (+0 other overloads)
DateTime(ticks: int64, kind: DateTimeKind) : DateTime
   (+0 other overloads)
DateTime(year: int, month: int, day: int) : DateTime
   (+0 other overloads)
DateTime(year: int, month: int, day: int, calendar: Globalization.Calendar) : DateTime
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int) : DateTime
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, kind: DateTimeKind) : DateTime
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, calendar: Globalization.Calendar) : DateTime
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int) : DateTime
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int, kind: DateTimeKind) : DateTime
   (+0 other overloads)
property DateTime.Month: int with get
<summary>Gets the month component of the date represented by this instance.</summary>
<returns>The month component, expressed as a value between 1 and 12.</returns>
property DateTime.Day: int with get
<summary>Gets the day of the month represented by this instance.</summary>
<returns>The day component, expressed as a value between 1 and 31.</returns>
property DateTime.DayOfWeek: DayOfWeek with get
<summary>Gets the day of the week represented by this instance.</summary>
<returns>An enumerated constant that indicates the day of the week of this <see cref="T:System.DateTime" /> value.</returns>
type DayOfWeek = | Sunday = 0 | Monday = 1 | Tuesday = 2 | Wednesday = 3 | Thursday = 4 | Friday = 5 | Saturday = 6
<summary>Specifies the day of the week.</summary>
field DayOfWeek.Monday: DayOfWeek = 1
<summary>Indicates Monday.</summary>
val d : int
field DayOfWeek.Friday: DayOfWeek = 5
<summary>Indicates Friday.</summary>
field DayOfWeek.Thursday: DayOfWeek = 4
<summary>Indicates Thursday.</summary>