From 00f63e9d516081215c4189354cda4f4d247628eb Mon Sep 17 00:00:00 2001 From: iSazonov Date: Mon, 11 Sep 2017 17:18:48 +0300 Subject: [PATCH 1/8] Correct %j format in Get-Date -UFormat Add leading zeros. Before fix: ```powershell >Get-date -Date 1/1/0030 -uformat %j 1 ``` After the fix: ```powershell >Get-date -Date 1/1/0030 -uformat %j 001 ``` --- .../commands/utility/GetDateCommand.cs | 2 +- .../Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs index 9f9da1ed28a..76a49e7a4d7 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs @@ -396,7 +396,7 @@ private string UFormatDateString(DateTime dateTime) break; case 'j': - sb.Append(dateTime.DayOfYear); + sb.Append(StringUtil.Format("{0:000}", dateTime.DayOfYear)); break; case 'k': 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 9c18379671a..88a50d295ad 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 @@ -30,7 +30,7 @@ Describe "Get-Date DRT Unit Tests" -Tags "CI" { } 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" + 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 1Jan00120010100AM12:00:00 AM" } It "using -uformat 'StTuUVwWxXyYZ' produces the correct output" { From b9b284ad2ecd9edd9b5c715c282e59f6ad6a9dec Mon Sep 17 00:00:00 2001 From: iSazonov Date: Tue, 12 Sep 2017 13:13:57 +0300 Subject: [PATCH 2/8] Correct %s format in Get-Date -UFormat Removed fraction part of seconds. Before fix: ```powershell > get-date 2016-01-1 -UFormat %s -Millisecond 1 1451606400.001 ``` After the fix: ```powershell > get-date 2016-01-1 -UFormat %s -Millisecond 1 1451606400 ``` --- .../commands/utility/GetDateCommand.cs | 5 +++-- .../Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs index 76a49e7a4d7..39a3aa9a35c 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs @@ -324,12 +324,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(); @@ -436,7 +437,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 88a50d295ad..6807c3bbfa8 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 @@ -33,8 +33,8 @@ Describe "Get-Date DRT Unit Tests" -Tags "CI" { 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 1Jan00120010100AM12: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 @( From 3ed038e66c9b2043a1b580be16fe3539b642a4c6 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Tue, 12 Sep 2017 14:28:48 +0300 Subject: [PATCH 3/8] Correct %k format in Get-Date -UFormat Single digits are preceded by a blank. Before fix: ```powershell > get-date 2016-01-1 -UFormat %k 01 ``` After the fix: ```powershell > get-date 2016-01-1 -UFormat %k 1 ``` --- .../commands/utility/GetDateCommand.cs | 2 +- .../Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs index 39a3aa9a35c..ed1a27d2250 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs @@ -401,7 +401,7 @@ private string UFormatDateString(DateTime dateTime) break; case 'k': - sb.Append("{0:HH}"); + sb.Append(StringUtil.Format("{0,2:0}", dateTime.Hour)); break; case 'l': 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 6807c3bbfa8..472f70c0df7 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 @@ -30,7 +30,7 @@ Describe "Get-Date DRT Unit Tests" -Tags "CI" { } 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 1Jan00120010100AM12:00:00 AM" + Get-date -Date 1/1/0030 -uformat %a%A%b%B%c%C%d%D%e%h%H%I%k%j%m%M%p%r | Should be "TueTuesdayJanJanuaryTue Jan 1 00:00:00 003000101/01/30 1Jan0012 00010100AM12:00:00 AM" } It "using -uformat 'sStTuUVwWxXyYZ' produces the correct output" { From fa289eef6448c47381523e8af1b69dcf3376a9cf Mon Sep 17 00:00:00 2001 From: iSazonov Date: Tue, 12 Sep 2017 14:43:05 +0300 Subject: [PATCH 4/8] Correct %l format in Get-Date -UFormat Single digits are preceded by a blank. Before fix: ```powershell > get-date 2016-01-1 -UFormat %l 00 ``` After the fix: ```powershell > get-date 2016-01-1 -UFormat %l 0 ``` --- .../commands/utility/GetDateCommand.cs | 2 +- .../Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs index ed1a27d2250..c6f4e4bbb16 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs @@ -405,7 +405,7 @@ private string UFormatDateString(DateTime dateTime) break; case 'l': - sb.Append("{0:hh}"); + sb.Append(StringUtil.Format("{0,2:0}", dateTime.Hour%12)); break; case 'M': 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 472f70c0df7..413d53ef8dd 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 @@ -29,8 +29,8 @@ 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%k%j%m%M%p%r | Should be "TueTuesdayJanJanuaryTue Jan 1 00:00:00 003000101/01/30 1Jan0012 00010100AM12: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 Jan 1 00:00:00 003000101/01/30 1Jan0012 0 00010100AM12:00:00 AM" } It "using -uformat 'sStTuUVwWxXyYZ' produces the correct output" { From 84e88ad28b2fa2389e7bb420b0661da114c1bccb Mon Sep 17 00:00:00 2001 From: iSazonov Date: Tue, 12 Sep 2017 14:54:59 +0300 Subject: [PATCH 5/8] Correct %c format in Get-Date -UFormat Add leading zero: now day of the month is from 01 through 31. Before fix: ```powershell >Get-Date -UFormat %c Thu Sep 7 17:49:48 2017 ``` After the fix: ```powershell >Get-Date -UFormat %c Thu Sep 07 17:49:48 2017 ``` --- .../commands/utility/GetDateCommand.cs | 4 +--- .../Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs index c6f4e4bbb16..a41597eb62a 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs @@ -371,9 +371,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:MMM} {0:dd} {0:HH}:{0:mm}:{0:ss} {0:yyyy}"); break; case 'D': 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 413d53ef8dd..3772f1d717a 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 @@ -30,7 +30,7 @@ Describe "Get-Date DRT Unit Tests" -Tags "CI" { } 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 Jan 1 00:00:00 003000101/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 Jan 01 00:00:00 003000101/01/30 1Jan0012 0 00010100AM12:00:00 AM" } It "using -uformat 'sStTuUVwWxXyYZ' produces the correct output" { From 19747bf002b3607b93747b1bb837cd71fe80416a Mon Sep 17 00:00:00 2001 From: iSazonov Date: Tue, 26 Sep 2017 12:19:06 +0300 Subject: [PATCH 6/8] Fix %c format --- .../commands/utility/GetDateCommand.cs | 2 +- .../Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs index a41597eb62a..6ec190765f8 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs @@ -371,7 +371,7 @@ private string UFormatDateString(DateTime dateTime) break; case 'c': - sb.Append("{0:ddd} {0:MMM} {0:dd} {0:HH}:{0:mm}:{0:ss} {0:yyyy}"); + sb.Append("{0:ddd} {0:dd} {0:MMM} {0:HH}:{0:mm}:{0:ss} {0:yyyy}"); break; case 'D': 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 3772f1d717a..f1c70854f75 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 @@ -30,7 +30,7 @@ Describe "Get-Date DRT Unit Tests" -Tags "CI" { } 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 Jan 01 00:00:00 003000101/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 00:00:00 003000101/01/30 1Jan0012 0 00010100AM12:00:00 AM" } It "using -uformat 'sStTuUVwWxXyYZ' produces the correct output" { From c6e46b0304a47a9abe97ab5078c9da37e96a83ab Mon Sep 17 00:00:00 2001 From: iSazonov Date: Thu, 15 Mar 2018 09:53:26 +0300 Subject: [PATCH 7/8] Fix %c --- .../commands/utility/GetDateCommand.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs index c2141530cdb..0ce4e3f873e 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs @@ -361,7 +361,7 @@ private string UFormatDateString(DateTime dateTime) break; case 'c': - sb.Append("{0:ddd} {0:dd} {0:MMM} {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': From 604316ede16fb6f0c198c7970957e26d8fe97520 Mon Sep 17 00:00:00 2001 From: iSazonov Date: Fri, 16 Mar 2018 15:16:16 +0300 Subject: [PATCH 8/8] Fix test for %c --- .../Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 e1cfcaf1dbe..cb60b47a15c 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 @@ -32,7 +32,7 @@ Describe "Get-Date DRT Unit Tests" -Tags "CI" { } 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 00:00:00 003000101/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 0 00010100AM12:00:00 AM" } It "using -uformat 'sStTuUVwWxXyYZ' produces the correct output" {