Add -FromUnixTime to Get-Date to allow Unix time input (#12179)

This commit is contained in:
Jack Casey 2020-04-07 13:25:03 -07:00 committed by GitHub
parent 3f717c5491
commit 46071b7ff9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View File

@ -40,6 +40,12 @@ namespace Microsoft.PowerShell.Commands
} }
} }
/// <summary>
/// Gets or sets whether to treat a numeric input as ticks, or unix time.
/// </summary>
[Parameter]
public SwitchParameter FromUnixTime;
private DateTime _date; private DateTime _date;
private bool _dateSpecified; private bool _dateSpecified;
@ -237,7 +243,14 @@ namespace Microsoft.PowerShell.Commands
// use passed date object if specified // use passed date object if specified
if (_dateSpecified) if (_dateSpecified)
{ {
dateToUse = Date; if (FromUnixTime.IsPresent)
{
dateToUse = DateTimeOffset.FromUnixTimeSeconds(Date.Ticks).UtcDateTime;
}
else
{
dateToUse = Date;
}
} }
// use passed year if specified // use passed year if specified

View File

@ -192,6 +192,15 @@ Describe "Get-Date" -Tags "CI" {
$timeDifference.Milliseconds | Should -BeLessThan 1 $timeDifference.Milliseconds | Should -BeLessThan 1
$timeDifference.Ticks | Should -BeLessThan 10000 $timeDifference.Ticks | Should -BeLessThan 10000
} }
It "-FromUnixTime works" {
# Test conversion of arbitrary date in Unix time: 2020-01-01T00:00:00.000Z
Get-Date -Date 1577836800 -FromUnixTime | Should -Be (Get-Date -Date 637134336000000000 -AsUTC)
# Test converstion of Unix time start date: 1970-01-01T00:00:00.000Z
Get-Date -Date 0 -FromUnixTime | Should -Be (Get-Date -Date 621355968000000000 -AsUTC)
}
} }
Describe "Get-Date -UFormat tests" -Tags "CI" { Describe "Get-Date -UFormat tests" -Tags "CI" {