diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs index 9668376a900..0ce4e3f873e 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs @@ -314,12 +314,13 @@ protected override void ProcessRecord() } } // EndProcessing + private static readonly DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + /// /// This is more an implementation of the UNIX strftime /// private string UFormatDateString(DateTime dateTime) { - DateTime epoch = DateTime.Parse("January 1, 1970", System.Globalization.CultureInfo.InvariantCulture); int offset = 0; StringBuilder sb = new StringBuilder(); @@ -360,9 +361,7 @@ private string UFormatDateString(DateTime dateTime) break; case 'c': - sb.Append("{0:ddd} {0:MMM} "); - sb.Append(StringUtil.Format("{0,2} ", dateTime.Day)); - sb.Append("{0:HH}:{0:mm}:{0:ss} {0:yyyy}"); + sb.Append("{0:ddd} {0:dd} {0:MMM} {0:yyyy} {0:HH}:{0:mm}:{0:ss}"); break; case 'D': @@ -386,15 +385,15 @@ private string UFormatDateString(DateTime dateTime) break; case 'j': - sb.Append(dateTime.DayOfYear); + sb.Append(StringUtil.Format("{0:000}", dateTime.DayOfYear)); break; case 'k': - sb.Append("{0:HH}"); + sb.Append(StringUtil.Format("{0,2:0}", dateTime.Hour)); break; case 'l': - sb.Append("{0:hh}"); + sb.Append(StringUtil.Format("{0,2:0}", dateTime.Hour%12)); break; case 'M': @@ -426,7 +425,7 @@ private string UFormatDateString(DateTime dateTime) break; case 's': - sb.Append(dateTime.Subtract(epoch).TotalSeconds); + sb.Append(StringUtil.Format("{0:0}", dateTime.Subtract(epoch).TotalSeconds)); break; case 'T': 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 c111d721bf2..cb60b47a15c 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 @@ -31,12 +31,12 @@ Describe "Get-Date DRT Unit Tests" -Tags "CI" { Get-date -Date 0030-01-01T00:00:00 -uformat %y/%m/%d-%H | Should be "30/01/01-00" } - It "using -uformat 'aAbBcCdDehHIjmMpr' produces the correct output" { - Get-date -Date 1/1/0030 -uformat %a%A%b%B%c%C%d%D%e%h%H%I%j%m%M%p%r | Should be "TueTuesdayJanJanuaryTue Jan 1 00:00:00 003000101/01/30 1Jan001210100AM12:00:00 AM" + 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" } - It "using -uformat 'StTuUVwWxXyYZ' produces the correct output" { - Get-date -Date 1/1/0030 -uformat %S%T%u%U%V%w%W%x%X%y%Y%% | Should be "0000:00:002012001/01/3000:00:00300030%" + 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%" } It "Passing '' to -uformat produces a descriptive error" -TestCases @(