A pedant that hangs out in the dark corner-cases of the web.

Wednesday, December 15, 2010

Conspicuous Features Missing From Google Chrome

Things that I miss when I use Google's Chrome browser:

Things that I miss when I use Firefox:

  • speed!

Things that I miss when I use Internet Explorer:

  • not using Internet Explorer

Saturday, December 11, 2010

US Federal Holidays in PowerShell

Here's how to determine whether a date is a US federal holiday using PowerShell.

function Test-USFederalHoliday
{
<#
.Synopsis
Returns the name of the holiday of a date, if it is a U.S. federal holiday.
.Description
The following holidays are checked:
* New Year's Day, January 1 (± 1 day, if observed)
* Martin Luther King, Jr. Day, Third Monday in January
* President's Day, Third Monday in February
* Memorial Day, Last Monday in May
* Independence Day, July 4 (± 1 day, if observed)
* Labor Day, First Monday in September
* Columbus Day, Second Monday in October
* Veteran's Day, November 11 (±1 day, if observed)
* Thanksgiving Day, Fourth Thursday in November
* Christmas Day, December 25 (±1 day, if observed)
#>
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true)][datetime]$Date,
[Parameter(Mandatory=$false,HelpMessage='Are Saturday holidays observed on Friday?')][switch]$SatToFri,
[Parameter(Mandatory=$false,HelpMessage='Are Sunday holidays observed on Monday?')][switch]$SunToMon
)
$MMdd= '{0:MMdd}' -f $Date
switch($Date.DayOfWeek)
{
'Monday'
{
switch -regex ($MMdd)
{
'^01(?:1[5-9]|2[01])$' {return 'Martin Luther King, Jr. Day'}
'^02(?:1[5-9]|2[01])$' {return 'Presidents Day'}
'^05(?:2[5-9]|3[01])$' {return 'Memorial Day'}
'^090[1-7]$' {return 'Labor Day'}
'^10(?:0[89]|1[01-4])$' {return 'Columbus Day'}
}
if($SunToMon)
{
switch($MMdd)
{
'0102' {return 'New Year''s Day (Observed)'}
'0705' {return 'Independence Day (Observed)'}
'1112' {return 'Veteran''s Day (Observed)'}
'1226' {return 'Christmas Day (Observed)'}
}
}
}
'Thursday'
{
if($MMdd -match '^112[2-8]$') {return 'Thanksgiving Day'}
}
'Friday'
{
if($SatToFri)
{
switch($MMdd)
{
'1231' {return 'New Year''s Day (Observed)'}
'0703' {return 'Independence Day (Observed)'}
'1110' {return 'Veteran''s Day (Observed)'}
'1224' {return 'Christmas Day (Observed)'}
}
}
}
}
switch($MMdd)
{
'0101' {return 'New Year''s Day'}
'0704' {return 'Independence Day'}
'1111' {return 'Veteran''s Day'}
'1225' {return 'Christmas Day'}
}
}

US Federal Holidays in F#

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"
| 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

You can really see the power of active pattern matching for the Memorial Day case.