Add -FromUnixTime
to Get-Date
to allow Unix time input (#12179)
This commit is contained in:
parent
3f717c5491
commit
46071b7ff9
@ -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 bool _dateSpecified;
|
||||
|
||||
@ -237,7 +243,14 @@ namespace Microsoft.PowerShell.Commands
|
||||
// use passed date object if specified
|
||||
if (_dateSpecified)
|
||||
{
|
||||
dateToUse = Date;
|
||||
if (FromUnixTime.IsPresent)
|
||||
{
|
||||
dateToUse = DateTimeOffset.FromUnixTimeSeconds(Date.Ticks).UtcDateTime;
|
||||
}
|
||||
else
|
||||
{
|
||||
dateToUse = Date;
|
||||
}
|
||||
}
|
||||
|
||||
// use passed year if specified
|
||||
|
@ -192,6 +192,15 @@ Describe "Get-Date" -Tags "CI" {
|
||||
$timeDifference.Milliseconds | Should -BeLessThan 1
|
||||
$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" {
|
||||
|
Loading…
Reference in New Issue
Block a user