Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public DateTime Date
}
}

/// <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;

Expand Down Expand Up @@ -237,7 +243,14 @@ protected override void ProcessRecord()
// 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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-01​T00:00:00.000Z
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 -AsUTC)
}
}

Describe "Get-Date -UFormat tests" -Tags "CI" {
Expand Down