Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -474,34 +474,7 @@ private string UFormatDateString(DateTime dateTime)
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':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand All @@ -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
Expand Down