From 395385406c176f9bb1542946672364d19db1081f Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Mon, 23 Mar 2020 01:01:56 -0700 Subject: [PATCH 01/10] Add FromUnixTime switch --- .../commands/utility/GetDateCommand.cs | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 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 066497950aa..291cb5001f7 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs @@ -40,6 +40,12 @@ public DateTime Date } } + /// + /// Gets or sets whether to treat a number input as ticks, or unix time + /// + [Parameter] + public SwitchParameter FromUnixTime; + private DateTime _date; private bool _dateSpecified; @@ -232,7 +238,14 @@ protected override void ProcessRecord() // use passed date object if specified if (_dateSpecified) { - dateToUse = Date; + if (FromUnixTime.IsPresent) + { + dateToUse = UnixTimeToDateTime(Date.Ticks); + } + else + { + dateToUse = Date; + } } // use passed year if specified @@ -547,6 +560,14 @@ private string UFormatDateString(DateTime dateTime) return StringUtil.Format(sb.ToString(), dateTime); } + /// + /// Converts unix time into DateTime + /// + private static DateTime UnixTimeToDateTime(long unixTime) + { + return DateTimeOffset.FromUnixTimeSeconds(unixTime).UtcDateTime; + } + #endregion } From 991a1206310c62d5223c910e77f098cd27d7257d Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Mon, 23 Mar 2020 01:02:27 -0700 Subject: [PATCH 02/10] Add FromUnixTime tests --- .../Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 | 7 +++++++ 1 file changed, 7 insertions(+) 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 668eab407e2..e1cc3c37bc4 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 @@ -277,3 +277,10 @@ Describe "Get-Date -UFormat tests" -Tags "CI" { Get-Date -Date $date -UFormat $format | Should -BeExactly $result } } + +Describe "Get-Date -FromUnixTime tests" -Tags "CI" { + It "-FromUnixTime works" { + Get-Date -Date 1577836800 -FromUnixTime | Should -Be (Get-Date -Date 637134336000000000) + Get-Date -Date 0 -FromUnixTime | Should -Be (Get-Date -Date 621355968000000000) + } +} From 201799475b6ca90a17745ae96528743eb92e7c94 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Mon, 23 Mar 2020 01:22:29 -0700 Subject: [PATCH 03/10] Address typos in documentation text --- .../commands/utility/GetDateCommand.cs | 4 ++-- 1 file 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 291cb5001f7..306acbaafec 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs @@ -41,7 +41,7 @@ public DateTime Date } /// - /// Gets or sets whether to treat a number input as ticks, or unix time + /// Gets or sets whether to treat a number input as ticks, or unix time. /// [Parameter] public SwitchParameter FromUnixTime; @@ -561,7 +561,7 @@ private string UFormatDateString(DateTime dateTime) } /// - /// Converts unix time into DateTime + /// Converts unix time into DateTime. /// private static DateTime UnixTimeToDateTime(long unixTime) { From e951a6dc12a674c0ee57e7edf6481250a4015166 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Mon, 23 Mar 2020 01:50:17 -0700 Subject: [PATCH 04/10] Remove unnecessary function creation --- .../commands/utility/GetDateCommand.cs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs index 306acbaafec..ddde7faf3ae 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs @@ -240,7 +240,7 @@ protected override void ProcessRecord() { if (FromUnixTime.IsPresent) { - dateToUse = UnixTimeToDateTime(Date.Ticks); + dateToUse = DateTimeOffset.FromUnixTimeSeconds(Date.Ticks).UtcDateTime; } else { @@ -560,14 +560,6 @@ private string UFormatDateString(DateTime dateTime) return StringUtil.Format(sb.ToString(), dateTime); } - /// - /// Converts unix time into DateTime. - /// - private static DateTime UnixTimeToDateTime(long unixTime) - { - return DateTimeOffset.FromUnixTimeSeconds(unixTime).UtcDateTime; - } - #endregion } From db607907623d9192495c0d5feba09e51e9104750 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Mon, 23 Mar 2020 01:53:54 -0700 Subject: [PATCH 05/10] Consolidate tests into existing Describe block --- .../Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) 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 e1cc3c37bc4..67cf5969176 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 @@ -172,6 +172,11 @@ Describe "Get-Date" -Tags "CI" { $timeDifference.Milliseconds | Should -BeLessThan 1 $timeDifference.Ticks | Should -BeLessThan 10000 } + + It "-FromUnixTime works" { + Get-Date -Date 1577836800 -FromUnixTime | Should -Be (Get-Date -Date 637134336000000000) + Get-Date -Date 0 -FromUnixTime | Should -Be (Get-Date -Date 621355968000000000) + } } Describe "Get-Date -UFormat tests" -Tags "CI" { @@ -277,10 +282,3 @@ Describe "Get-Date -UFormat tests" -Tags "CI" { Get-Date -Date $date -UFormat $format | Should -BeExactly $result } } - -Describe "Get-Date -FromUnixTime tests" -Tags "CI" { - It "-FromUnixTime works" { - Get-Date -Date 1577836800 -FromUnixTime | Should -Be (Get-Date -Date 637134336000000000) - Get-Date -Date 0 -FromUnixTime | Should -Be (Get-Date -Date 621355968000000000) - } -} From 1aa14687663e9ac5bfba3a342e19c2f0ad3c0ed5 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Mon, 23 Mar 2020 02:21:08 -0700 Subject: [PATCH 06/10] Update comment for clarity --- .../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 ddde7faf3ae..0444a1f8bbd 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs @@ -41,7 +41,7 @@ public DateTime Date } /// - /// Gets or sets whether to treat a number input as ticks, or unix time. + /// Gets or sets whether to treat a numeric input as ticks, or unix time. /// [Parameter] public SwitchParameter FromUnixTime; From 37d22a1bdf20338ffce17868bfa727c5396f5dfc Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Sun, 5 Apr 2020 13:20:25 -0700 Subject: [PATCH 07/10] Add comments to outline test constants --- .../Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 | 4 ++++ 1 file changed, 4 insertions(+) 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 67cf5969176..9831fe637fd 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 @@ -174,7 +174,11 @@ Describe "Get-Date" -Tags "CI" { } It "-FromUnixTime works" { + + # Test conversion of arbitrary date in Unix time: 2020-01-01​T00:00:00.000Z Get-Date -Date 1577836800 -FromUnixTime | Should -Be (Get-Date -Date 637134336000000000) + + # Test converstion of Unix time start date: 1970-01-01​T00:00:00.000Z Get-Date -Date 0 -FromUnixTime | Should -Be (Get-Date -Date 621355968000000000) } } From 18d7bc30b1852d4dcb8ddf6559ef7097aa1c1c8a Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Sun, 5 Apr 2020 23:15:48 -0700 Subject: [PATCH 08/10] Specify UTC DateTime kind in tests --- .../Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 0cc20abb5c0..0cdc794921b 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 @@ -196,10 +196,10 @@ Describe "Get-Date" -Tags "CI" { It "-FromUnixTime works" { # Test conversion of arbitrary date in Unix time: 2020-01-01​T00:00:00.000Z - Get-Date -Date 1577836800 -FromUnixTime | Should -Be (Get-Date -Date 637134336000000000) + Get-Date -Date 1577836800 -FromUnixTime | Should -Be (Get-Date -Date 637134336000000000 -AsUTC) # Test converstion of Unix time start date: 1970-01-01​T00:00:00.000Z - Get-Date -Date 0 -FromUnixTime | Should -Be (Get-Date -Date 621355968000000000) + Get-Date -Date 0 -FromUnixTime | Should -Be (Get-Date -Date 621355968000000000 -AsUTC) } } From c76eebedd3697bca72baf533823018722fa63323 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Sun, 5 Apr 2020 23:17:10 -0700 Subject: [PATCH 09/10] Specify kind as UTC if set with ticks or Unix time --- .../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 4516568cc34..03c3f5caba5 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs @@ -249,7 +249,7 @@ protected override void ProcessRecord() } else { - dateToUse = Date; + dateToUse = DateTime.SpecifyKind(Date, DateTimeKind.Utc); } } From 9c0b42dfa93836f1eb1269201f0f5658c06850b7 Mon Sep 17 00:00:00 2001 From: Jack Casey Date: Mon, 6 Apr 2020 22:23:30 -0700 Subject: [PATCH 10/10] Revert "Specify kind as UTC if set with ticks or Unix time" This reverts commit c76eebedd3697bca72baf533823018722fa63323. --- .../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 03c3f5caba5..4516568cc34 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs @@ -249,7 +249,7 @@ protected override void ProcessRecord() } else { - dateToUse = DateTime.SpecifyKind(Date, DateTimeKind.Utc); + dateToUse = Date; } }