Powershell – Holiday calculation, for older outlooks


I had the pleasure to make sure, holidays got into the outlook calendar.

The catch is, we are talking Outlook 2007, and it is no longer supported, hence: you cannot automatically import holidays anymore.

Therefor I made a simple Powershell script to create a .hol file, to import.

The way it works is, there is 3 types of dates. This only contains danish holidays – sorry, but you should easily be able to find them for other regions.

A lot of the dates are an offset from easter. These dates I call “floating dates” (or “float”).

Then there is the “Fixed” days like x-mas and such – and finally the “Calculated” as in first sunday in december.

So basically, I make a table with either offsets from Easter, or a fixed date – and then dump them into a file.

So to recap dates:

  • Float :  Days offset from Easter
  • Fixed<day>,<month>
  • Calculated: <day>,<month>

 

Save the script to a .ps1 file. For example Holidays.ps1 and put the year as parameter:  .\Holidays.ps1 2015

 

Param ( 
   [Parameter(Mandatory=$true)]
   [ValidateNotNullOrEmpty()]
   [int]$YEAR
)

 if (!$YEAR) { Exit }  # make a better check if needed 🙂
 $FNAME = “Danske Helligdage-$YEAR.hol”
# Custom holiday object
function NewHday {
  param ([string]$datename, [string]$dateVal, [string]$datetype)
  $Hday = New-Object System.Object
  $Hday | Add-Member -type NoteProperty -name name -value $datename
  $Hday | Add-Member -type NoteProperty -name val -value $dateval
  $Hday | Add-Member -type NoteProperty -name Type -value $datetype
  $Hday
 }

########################################################## Date definitions

 $Hdays = @() # Empty array
       # Here float just means – the date isnt fixed.. not the data type
       # Offset from easter
 $Hdays += NewHday “Palmesøndag”  “-7”  “float”
 $Hdays += NewHday “Skærtorsdag”  “-3”  “float”
 $Hdays += NewHday “Langfredag”   “-2”  “float”
 $Hdays += NewHday “Påske”   “0”  “float”
 $Hdays += NewHday “2. Påskedag”  “1”  “float”
 $Hdays += NewHday “St. Bededag”  “26”  “float”
 $Hdays += NewHday “Kristi Himmelfart”  “39”  “float”
 $Hdays += NewHday “Pinse”   “49”  “float”
 $Hdays += NewHday “2. Pinsedag”  “50”  “float”

      # D.M ( Fixed dates )
 $Hdays += NewHday “Nytårsdag”   “1,1”  “fixed”
 $Hdays += NewHday “Sommertid begynder”  “31,3”  “fixed”
 $Hdays += NewHday “Grundlovsdag”  “5,6”  “fixed”
 $Hdays += NewHday “Sommertid ender”  “27,10” “Fixed”
 $Hdays += NewHday “Juleaftensdag”  “24,12” “fixed”
 $Hdays += NewHday “Juledag”   “25,12” “fixed”
 $Hdays += NewHday “2. juledag”   “26,12” “fixed”
 $Hdays += NewHday “Nytårsaften”  “31,12” “fixed”

 

      # D.M ( Calculated dates )
 [System.DateTime]$date = [string]$Year + “/12/1”
 while($date.DayOfWeek -ne “Sunday”) { $date = $date.adddays(1) }
 $d = [string]$date.day+”,”+$date.month
 $Hdays += NewHday “1. søndag i advent” $d “Calculated”

 $date = $date.adddays(7)
 $d = [string]$date.day+”,”+$date.month
 $Hdays += NewHday “2. søndag i advent” $d “Calculated”

 $date = $date.adddays(7)
 $d = [string]$date.day+”,”+$date.month
 $Hdays += NewHday “3. søndag i advent” $d “Calculated”

 $date = $date.adddays(7)
 $d = [string]$date.day+”,”+$date.month
 $Hdays += NewHday “4. søndag i advent” $d “Calculated”
########################################################## easter calculation.. find the golden number etc.. look it up 🙂 

 $a = $Year % 19
 $b = [Math]::Floor($Year / 100)
 $c = $Year % 100
 $d = [Math]::Floor($b / 4)
 $e = $b % 4
 $f = [Math]::Floor(($b + 8) / 25)
 $g = [Math]::Floor((($b – $f + 1) / 3))
 $h = ((19 * $a) + $b + (-$d) + (-$g) + 15) % 30
 $i = [Math]::Floor($c / 4)
 $k = $c % 4
 $L1 = -($h + $k) #here because powershell is picking up – (subtraction operator) as incorrect toekn
 $L = (32 + (2 * $e) + (2 * $i) + $L1) % 7
 $m = [Math]::Floor(($a + (11 * $h) + (22 * $L)) / 451)
 $v1 = -(7 * $m) #here because powershell is picking up – (subtraction operator) as incorrect toekn
 $month = [Math]::Floor(($h + $L + $v1 + 114) / 31)
 $day = (($h + $L + $v1 + 114) % 31) + 1
 [System.DateTime]$date = “$Year/$month/$day”

 $template = ‘yyyy dd MM’
 
 Write-Host “Easter : ” $date
########################################################## Lets Create the file

 New-Item $FNAME -type file -force

 “[Dansk] “+$hdays.count | Out-File $FNAME -append

 foreach ($holiday in $Hdays)
 {
  if($holiday.type -eq “float”)
  {
   $d = $date.Adddays($holiday.val)
   $NewDate = [string]$d.year+”/”+$d.month+”/”+$d.day
  }
  else
  {
   $d = $holiday.val.Split(“,”)
   $NewDate = “$Year/”+$d[1]+”/”+$d[0]
  }

  $holiday.name +”,”+ $NewDate | Out-File $FNAME -append
 }