Last active
February 16, 2021 08:24
-
-
Save B4Art/1fc988ca8e89c847e2ac396f7dc9cba2 to your computer and use it in GitHub Desktop.
Get the correct ISO8601 week number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-ISOWeek { | |
Param( | |
[Parameter( | |
# Mandatory, | |
ValueFromPipeline, | |
ValueFromPipelineByPropertyName, | |
HelpMessage = 'Valid Date' | |
)] | |
[Alias('DT', 'DateTime')] | |
[datetime[]]$Dates = (Get-Date), | |
[Validateset('Culture', 'UICulture', 'InvariantCulture')] | |
[string]$Culture | |
) | |
<# | |
First create an integer(0/1) from the boolean, | |
"Is the integer DayOfWeek value greater than zero?". | |
Then Multiply it with 4 or 6 (weekrule = 0 or 2) minus the integer DayOfWeek value. | |
This turns every day (except Sunday) into Thursday. | |
Then return the ISO8601 WeekNumber. | |
#> | |
BEGIN { | |
$Cult = Switch ($Culture) { | |
'UICulture' { | |
Get-UICulture | |
Break | |
} | |
'InvariantCulture' { | |
[CultureInfo]::InvariantCulture | |
Break | |
} | |
default{ | |
Get-Culture | |
} | |
} | |
$WeekRule = $Cult.DateTimeFormat.CalendarWeekRule.value__ | |
$FirstDayOfWeek = $Cult.DateTimeFormat.FirstDayOfWeek.value__ | |
} | |
PROCESS{ | |
ForEach ($DateElement in $Dates) { | |
Try { | |
Write-Verbose "------------------------" | |
Write-Verbose "Try to turn $DateElement into ISO8601Week" | |
$WeekRuleDay = [int]($DateElement.DayOfWeek.Value__ -ge $FirstDayOfWeek ) * ( (6 - $WeekRule) - $DateElement.DayOfWeek.Value__ ) | |
$WeekNumber = $Cult.Calendar.GetWeekOfYear(($DateElement).AddDays($WeekRuleDay), $WeekRule, $FirstDayOfWeek) | |
<# | |
So generate the week of day numbers 1..7 based on culture firstdayofweek value and then pick the day of today as an index | |
#> | |
$DayNumber = (0..6).ForEach( { (($_ - $FirstDayOfWeek + 7) % 7) + 1 })[$DateElement.DayOfWeek.value__] | |
$Properties = @{ | |
Date = Get-Date($DateElement) | |
DayOfWeek = (Get-Date($DateElement)).DayOfWeek | |
WeekRuleDay = $WeekRuleDay | |
WeekNumber = $WeekNumber | |
ISOWeek = "$($DateElement.Year)-W$WeekNumber" | |
ISOWeekDay = "$($DateElement.Year)-W$WeekNumber-$DayNumber" | |
ISO8601Week = "$($DateElement.Year)-W$WeekNumber" | |
ISO8601WeekDay = "$($DateElement.Year)-W$WeekNumber-$DayNumber" | |
} | |
} Catch { | |
Write-Verbose "Could not turn: $DateElement into ISO8601Week" | |
$Properties = @{ | |
Date = $DateElement | |
DayOfWeek = '' | |
WeekRuleDay = '' | |
WeekNumber = '' | |
ISOWeek = '' | |
ISOWeekDay = '' | |
ISO8601Week = '' | |
ISO8601WeekDay = '' | |
} | |
} Finally{ | |
Write-Verbose "Final" | |
$Obj = New-Object -Typename PSCustomObject -Property $Properties | |
Write-Output $Obj | |
} | |
} | |
} | |
END{} | |
} | |
Write-Host " Culture: " -NoNewline | |
(20..31).ForEach( { Get-ISO8601Week("2012-12-$($_)").ISOWeekDay }) + ((1..7).ForEach( { Get-ISO8601Week("2013-1-$($_)").ISOWeekDay })) -join ', ' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment