Office holidays

Office holidays are a great thing. They usually occur on public holidays. There’s a web site – www.officeholidays.com – you can use to discover the public holidays in your country. 133 countries are available – http://www.officeholidays.com/countries/index.php.

You can also use PowerShell to extract the information

$uri = "http://www.officeholidays.com/countries/united_kingdom/index.php"
$html = Invoke-WebRequest -Uri $uri
$holidays = ($html.ParsedHtml.getElementsByTagName("table") | 
where ClassName -eq 'list-table' | 
select -ExpandProperty InnerText) -split "`n"

$holidays.Count

$hols = foreach ($holiday in $holidays[1..($holidays.Count -1)]){
   $x = $holiday -split " "
   $y = $x[0] -split "day"
   
   $props = [ordered]@{
     DayOfWeek = "$($y[0])day"
     Day = $x[1]
     Month = $y[1]
     Holiday = $x[4..($x.Count-1)] -join " "
   }
   
   New-Object -TypeName PSObject -Property $props
 }

$hols | Format-Table -AutoSize –Wrap

Create a string to hold the uri of the country you want. You may need to use the website to discover how they format your country name. Use Invoke-WebRequest to get the html containing the holidays. You then need to get the inner text of the html.

DayDateHolidayComments
SundayJanuary 01 Jan 1 New Years Day First Monday if 1st is Saturday or Sunday
MondayJanuary 02 Jan 2 Day after New Years Day Scotland Only
MondayJanuary 02 Jan 2 New Years Day (observed) Except Scotland
TuesdayJanuary 03 Jan 3 New Year's Day (in lieu) Scotland Only
TuesdayFebruary 28 Feb 28 Pancake Tuesday Shrove Tuesday. Not a Public Holiday
WednesdayMarch 01 Mar 1 St Davids Day Wales Only. Not a public holiday
FridayMarch 17 Mar 17 St Patricks Day Northern Ireland Only
SundayMarch 26 Mar 26 Mothering Sunday Not a National Holiday
FridayApril 14 Apr 14 Good Friday Friday before Easter Sunday
MondayApril 17 Apr 17 Easter Monday Except Scotland
SundayApril 23 Apr 23 St George's Day England Only. Not a public holiday
MondayMay 01 May 1 Early May Bank Holiday First Monday in May
MondayMay 29 May 29 Spring Bank Holiday Last Monday in May
SundayJune 18 Jun 18 Father's Day 3rd Sunday in June. Not a public holiday
WednesdayJuly 12 Jul 12 Battle of the Boyne Northern Ireland Only
MondayAugust 07 Aug 7 August Bank Holiday Scotland Only. First Monday in August
MondayAugust 28 Aug 28 August Bank Holiday Last Monday in August (except Scotland)
SundayNovember 05 Nov 5 Guy Fawkes Night England Only. Not a public holiday
SundayNovember 12 Nov 12 Remembrance Sunday Not a public holiday. Sunday closest to 11 November
ThursdayNovember 30 Nov 30 St Andrews Day Scotland Only. If November 30 falls on a weekend, the next Monday is a bank holiday instead
MondayDecember 25 Dec 25 Christmas Day
TuesdayDecember 26 Dec 26 Boxing Day

Its a single string but does contain new line characters so you can split it into an array.

Iterate through the array – skip the first line – and extract the day of the week, the day, month and holiday details. Create an object for output. Display the objects to see your holidays.

DayOfWeek Day Month Holiday 
--------- --- ----- ------- 
Sunday 01 January New Years Day First Monday if 1st is Saturday or Sunday 
Monday 02 January Day after New Years Day Scotland Only 
Monday 02 January New Years Day (observed) Except Scotland 
Tuesday 03 January New Year's Day (in lieu) Scotland Only 
Tuesday 28 February Pancake Tuesday Shrove Tuesday. Not a Public Holiday 
Wednesday 01 March St Davids Day Wales Only. Not a public holiday 
Friday 17 March St Patricks Day Northern Ireland Only 
Sunday 26 March Mothering Sunday Not a National Holiday 
Friday 14 April Good Friday Friday before Easter Sunday 
Monday 17 April Easter Monday Except Scotland 
Sunday 23 April St George's Day England Only. Not a public holiday 
Monday 01 May Early May Bank Holiday First Monday in May 
Monday 29 May Spring Bank Holiday Last Monday in May 
Sunday 18 June Father's Day 3rd Sunday in June. Not a public holiday 
Wednesday 12 July Battle of the Boyne Northern Ireland Only 
Monday 07 August August Bank Holiday Scotland Only. First Monday in 
 August 
Monday 28 August August Bank Holiday Last Monday in August (except 
 Scotland) 
Sunday 05 November Guy Fawkes Night England Only. Not a public holiday 
Sunday 12 November Remembrance Sunday Not a public holiday. Sunday closest 
 to 11 November 
Thursday 30 November St Andrews Day Scotland Only. If November 30 falls on a 
 weekend, the next Monday is a bank holiday instead 
Monday 25 December Christmas Day 
Tuesday 26 December Boxing Day

I’ve used some rather ugly, brute force string handling. There should be a better way to extracting the data from the string but I need to think about it

Advertisements
This entry was posted in Powershell. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s