From aada0a80b761e31875db38f5eb69fd1d34403402 Mon Sep 17 00:00:00 2001 From: Paal Braathen Date: Tue, 14 Jan 2020 19:26:24 +0100 Subject: [PATCH] Use ISOWeek for week numbers in Get-Date (#11536) --- .../commands/utility/GetDateCommand.cs | 29 +------------------ .../Get-Date.Tests.ps1 | 17 ++++++++++- 2 files changed, 17 insertions(+), 29 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs index 066497950a..0b04096c93 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs @@ -474,34 +474,7 @@ namespace Microsoft.PowerShell.Commands break; case 'V': - // .Net Core doesn't implement ISO 8601. - // So we use workaround from https://blogs.msdn.microsoft.com/shawnste/2006/01/24/iso-8601-week-of-year-format-in-microsoft-net/ - // with corrections from comments - - // Culture doesn't matter since we specify start day of week - var calender = CultureInfo.InvariantCulture.Calendar; - var day = calender.GetDayOfWeek(dateTime); - var normalizedDatetime = dateTime; - - switch (day) - { - case DayOfWeek.Monday: - case DayOfWeek.Tuesday: - case DayOfWeek.Wednesday: - normalizedDatetime = dateTime.AddDays(3); - break; - - case DayOfWeek.Friday: - case DayOfWeek.Saturday: - case DayOfWeek.Sunday: - normalizedDatetime = dateTime.AddDays(-3); - break; - } - - // FirstFourDayWeek and DayOfWeek.Monday is from ISO 8601 - sb.Append(StringUtil.Format("{0:00}", calender.GetWeekOfYear(normalizedDatetime, - CalendarWeekRule.FirstFourDayWeek, - DayOfWeek.Monday))); + sb.Append(StringUtil.Format("{0:00}", ISOWeek.GetWeekOfYear(dateTime))); break; case 'W': diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 index 48a52d7940..6980d18028 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 @@ -52,6 +52,11 @@ Describe "Get-Date DRT Unit Tests" -Tags "CI" { # The 'week of year' test cases is from https://en.wikipedia.org/wiki/ISO_week_date It "using -uformat 'V' produces the correct output" -TestCases @( + @{date="1998-01-02"; week = "01"}, + @{date="1998-01-03"; week = "01"}, + @{date="2003-01-03"; week = "01"}, + @{date="2004-01-02"; week = "01"}, + @{date="2004-01-03"; week = "01"}, @{date="2005-01-01"; week = "53"}, @{date="2005-01-02"; week = "53"}, @{date="2005-12-31"; week = "52"}, @@ -67,11 +72,21 @@ Describe "Get-Date DRT Unit Tests" -Tags "CI" { @{date="2008-12-30"; week = "01"}, @{date="2008-12-31"; week = "01"}, @{date="2009-01-01"; week = "01"}, + @{date="2009-01-02"; week = "01"}, + @{date="2009-01-03"; week = "01"}, @{date="2009-12-31"; week = "53"}, @{date="2010-01-01"; week = "53"}, @{date="2010-01-02"; week = "53"}, @{date="2010-01-03"; week = "53"}, - @{date="2010-01-04"; week = "01"} + @{date="2010-01-04"; week = "01"}, + @{date="2014-01-03"; week = "01"}, + @{date="2015-01-02"; week = "01"}, + @{date="2015-01-03"; week = "01"}, + @{date="2020-01-03"; week = "01"}, + @{date="2025-01-03"; week = "01"}, + @{date="2026-01-02"; week = "01"}, + @{date="2026-01-03"; week = "01"}, + @{date="2031-01-03"; week = "01"} ) { param($date, $week) Get-date -Date $date -uformat %V | Should -BeExactly $week