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 @@ -393,7 +393,7 @@ private string UFormatDateString(DateTime dateTime)
break;

case 'l':
sb.Append(StringUtil.Format("{0,2:0}", dateTime.Hour%12));
sb.Append("{0,2:%h}");
break;

case 'M':
Expand Down Expand Up @@ -425,7 +425,7 @@ private string UFormatDateString(DateTime dateTime)
break;

case 's':
sb.Append(StringUtil.Format("{0:0}", dateTime.Subtract(epoch).TotalSeconds));
sb.Append(StringUtil.Format("{0:0}", dateTime.ToUniversalTime().Subtract(epoch).TotalSeconds));
break;

case 'T':
Expand All @@ -449,7 +449,34 @@ private string UFormatDateString(DateTime dateTime)
break;

case 'V':
sb.Append((dateTime.DayOfYear / 7) + 1);
// .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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the MSDN blog post, the "cheat" is to add 3 days for Mon-Wed, why are you subtracting 3 days for Fri-Sun?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Below in the blog post comments (Keno) contain the fix.
Test values from Wikipedia article pass tests only with the fix.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, got it. Thanks.

break;
}

// FirstFourDayWeek and DayOfWeek.Monday is from ISO 8601
sb.Append(StringUtil.Format("{0:00}",calender.GetWeekOfYear(normalizedDatetime,
CalendarWeekRule.FirstFourDayWeek,
DayOfWeek.Monday)));
break;

case 'G':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,52 @@ Describe "Get-Date DRT Unit Tests" -Tags "CI" {
Get-date -Date:"Jan 1, 2020" -Format:"MMM-dd-yy" | Should -Be "Jan-01-20"
}

It "using -uformat produces the correct output" {
Get-date -Date:"Jan 1, 2020" -UFormat:"%s" | Should -Be "1577836800"
It "using -uformat %s produces the correct output" {
$seconds = Get-date -Date:"Jan 1, 2020Z" -UFormat:"%s"

$seconds | Should -Be "1577836800"
if ($isLinux) {
$seconds | Should -Be (date --date='01/01/2020 UTC' +%s)
}
}

It "using -uformat 'ymdH' produces the correct output" {
Get-date -Date 0030-01-01T00:00:00 -uformat %y/%m/%d-%H | Should -Be "30/01/01-00"
}

It "using -uformat 'aAbBcCdDehHIkljmMpr' produces the correct output" {
Get-date -Date 1/1/0030 -uformat %a%A%b%B%c%C%d%D%e%h%H%I%k%l%j%m%M%p%r | Should -Be "TueTuesdayJanJanuaryTue 01 Jan 0030 00:00:0000101/01/30 1Jan0012 0 00010100AM12:00:00 AM"
Get-date -Date 1/1/0030 -uformat "%a%A%b%B%c%C%d%D%e%h%H%I%k%l%j%m%M%p%r" | Should -Be "TueTuesdayJanJanuaryTue 01 Jan 0030 00:00:0000101/01/30 1Jan0012 0120010100AM12:00:00 AM"
}

It "using -uformat 'sStTuUVwWxXyYZ' produces the correct output" {
Get-date -Date 1/1/0030 -uformat %s%S%T%u%U%V%w%W%x%X%y%Y%% | Should -Be "-612204480000000:00:002012001/01/3000:00:00300030%"
Get-date -Date 1/1/0030 -uformat %S%T%u%U%w%W%x%X%y%Y%% | Should -Be "0000:00:00202001/01/3000:00:00300030%"
}

# 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="2005-01-01"; week = "53"},
@{date="2005-01-02"; week = "53"},
@{date="2005-12-31"; week = "52"},
@{date="2006-01-01"; week = "52"},
@{date="2006-01-02"; week = "01"},
@{date="2006-12-31"; week = "52"},
@{date="2007-01-01"; week = "01"},
@{date="2007-12-30"; week = "52"},
@{date="2007-12-31"; week = "01"},
@{date="2008-01-01"; week = "01"},
@{date="2008-12-28"; week = "52"},
@{date="2008-12-29"; week = "01"},
@{date="2008-12-30"; week = "01"},
@{date="2008-12-31"; week = "01"},
@{date="2009-01-01"; 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"}
) {
param($date, $week)
Get-date -Date $date -uformat %V | Should -BeExactly $week
}

It "Passing '<name>' to -uformat produces a descriptive error" -TestCases @(
Expand Down