Skip to content

Commit

Permalink
Organized examples with notes and results
Browse files Browse the repository at this point in the history
  • Loading branch information
SamErde committed Feb 28, 2024
1 parent afb06c6 commit f5ebe13
Showing 1 changed file with 59 additions and 34 deletions.
93 changes: 59 additions & 34 deletions Snippets/Get Patch Tuesday Variations.ps1
Original file line number Diff line number Diff line change
@@ -1,32 +1,51 @@
<#
https://twitter.com/SamErde/status/1648023633913167875
.SYNOPSIS
This script demonstrates several different ways to find Patch Tuesday (the 2nd Tuesday of the month).
.DESCRIPTION
This script demonstrates several different ways to find Patch Tuesday (the 2nd Tuesday of the month).
It's a fun exercise to see how different people approach the same problem, and how some solutions are
more readable and understandable than others--but beauty is in the eye of the beholder!
.NOTES
File Name : Get Patch Tuesday Variations.ps1
Author : Sam Erde (@SamErde) and friends on Twitter/X
Read the thread that inspired these examples at <https://twitter.com/SamErde/status/1648023633913167875>.
#>

# Find the first day of the moth
$FirstDayOfMonth = (Get-Date -Day 1).Date

# This one works but is too complex to understand. I don't even remember!
$PatchTuesday1 = $FirstDayOfMonth.AddDays( ([DayOfWeek]'Tuesday' - $FirstDayOfMonth.DayOfWeek + 7) % 7 + 7)


# $PatchTuesday = # ...one of the following examples...



# This one works but is too complex to understand. I don't even remember how or why it works!
$PatchTuesday = $FirstDayOfMonth.AddDays( ([DayOfWeek]'Tuesday' - $FirstDayOfMonth.DayOfWeek + 7) % 7 + 7)
$PatchTuesday



# This one checks the first 16 days of the month, grabs each instance of a Tuesday, and keeps the 2nd one (index of 1). Still weird and complex.
$PatchTuesday2 = (0..16 | ForEach-Object {$FirstDayOfMonth.AddDays($_) } | Where-Object {$_.DayOfWeek -like "Tuesday"})[1]
$PatchTuesday = (0..16 | ForEach-Object {$FirstDayOfMonth.AddDays($_) } | Where-Object {$_.DayOfWeek -like "Tuesday"})[1]
$PatchTuesday



# This one from Mike F. Robbins is the most readable and understandable. It checks if the first Tuesday is in the first week, and if not, it adds 7 days to it.
while ($FirstDayOfMonth.DayOfWeek -ne 'Tuesday') {
$FirstDayOfMonth = $FirstDayOfMonth.AddDays(1)
$Day = $FirstDayOfMonth
while ($Day.DayOfWeek -ne 'Tuesday') {
$Day = $Day.AddDays(1)
}
$FirstDayOfMonth.AddDays(7)
$PatchTuesday = $Day.AddDays(7)
$PatchTuesday


# This example from Kit @smallfoxx might make sense to some people and not others. It helps to step through it with debug to see the values as it runs.
if ($FirstDayOfMonth.DayOfWeek -gt [DayOfWeek]"Tuesday") {
$Shift=[DayOfWeek]"Tuesday" + 7
}
else {
$Shift=[DayOfWeek]"Tuesday"
}
$FirstDayOfMonth.AddDays($Shift - $FirstDayOfMonth.DayOfWeek)

# And here's one from Anthony J Fontanez (ajf8729) that forgoes complex logic for a longer, but simple switch statement.
# And here's one from Anthony J Fontanez (@ajf8729) that forgoes complex logic for a simple "hard-coded" switch statement.
switch ( (Get-Date -Day 1).DayOfWeek ) {
'Tuesday' { 8 }
'Monday' { 9 }
Expand All @@ -36,7 +55,6 @@ switch ( (Get-Date -Day 1).DayOfWeek ) {
'Thursday' { 13 }
'Wednesday' { 14 }
}

# I wrote the day into the date, et voila! Patch Tuesday.
switch ( (Get-Date -Day 1).DayOfWeek ) {
'Tuesday' { $Day = 8 }
Expand All @@ -47,11 +65,14 @@ switch ( (Get-Date -Day 1).DayOfWeek ) {
'Thursday' { $Day = 13 }
'Wednesday' { $Day = 14 }
}
$PatchTuesdaySwitch = (Get-Date -Day $Day).Date
$PatchTuesday = (Get-Date -Day $Day).Date
$PatchTuesday



# Here's a sample from James Orlando (@Jorlando82).
# Here's a similar one from James Orlando (@Jorlando82) that also gets the 1st day of the month in a slightly different way.
$FirstDay = (Get-Date).AddDays(-((Get-Date).AddDays(-1)).Day)
Switch ($FirstDay.DayOfWeek) {
$PatchTuesday = Switch ($FirstDay.DayOfWeek) {
"Monday" { $FirstDay.AddDays(8) }
"Tuesday" { $FirstDay.AddDays(7) }
"Wednesday"{ $FirstDay.AddDays(13) }
Expand All @@ -60,6 +81,22 @@ Switch ($FirstDay.DayOfWeek) {
"Saturday" { $FirstDay.AddDays(10) }
"Sunday" { $FirstDay.AddDays(9) }
}
$PatchTuesday



# This example from Kit (@smallfoxx) works great but still challenges my ability to explain in a simple way.
if ($FirstDayOfMonth.DayOfWeek -gt [DayOfWeek]"Tuesday") {
$Shift=[DayOfWeek]"Tuesday" + 7
}
else {
$Shift=[DayOfWeek]"Tuesday"
}
# Use the shift to find the 1st Tuesday and then add 7 for the 2nd Tuesday.
$PatchTuesday = $FirstDayOfMonth.AddDays($Shift - $FirstDayOfMonth.DayOfWeek + 7)
$PatchTuesday



# Then kit (@smallfoxx) took it further and made it a function.
function WeekdayInMonth {
Expand All @@ -78,24 +115,12 @@ function WeekdayInMonth {
}
$FirstDay.AddDays($Shift)
}

function PatchTuesday {
param(
[datetime]$Date = (Get-Date)
)

WeekdayInMonth -Date $Date -Weekday [DayOfWeek]::Tuesday -WeekNumber 2
}
$PatchTuesdayFunction = PatchTuesday





<#
-----
#>
$PatchTuesday
$PatchTuesday1
$PatchTuesday2
$PatchTuesdaySwitch
$PatchTuesdayFunction
$PatchTuesday = PatchTuesday

0 comments on commit f5ebe13

Please sign in to comment.