Skip to content

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

Merged
Travis Plunk (TravisEz13) merged 11 commits into
PowerShell:masterfrom
jackdcasey:Get-Date_AddFromUnixTime
Apr 7, 2020
Merged

Travis Plunk (TravisEz13) merged 11 commits into
PowerShell:masterfrom
jackdcasey:Get-Date_AddFromUnixTime

Conversation

@jackdcasey

@jackdcasey Jack Casey (jackdcasey) commented Mar 23, 2020 •

Copy link
Copy Markdown
Contributor

PR Summary

My proposal, is to add a -FromUnixTime switch to Get-Date.

This allows the input of a Unix timestamp into Get-Date. This is currently impossible, as the default unit for a numeric input is ticks.

Adding this switch would allow the user to specify if the input is Unix time, while also keeping full previous compatibility with ticks.

Here is an example output with, and without the switch:

> Get-Date -Date 1584947314 -FromUnixTime

 Monday, March 23, 2020 7:08:34 AM

> Get-Date -Date 1584947314

 Monday, January 1, 0001 12:02:38 AM

PR Context

Closes #11719

PR Checklist

@iSazonov Ilya (iSazonov) added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Mar 23, 2020
@iSazonov Ilya (iSazonov) added this to the 7.1.0-preview.1 milestone Mar 23, 2020
@jackdcasey

Copy link
Copy Markdown
Contributor Author

Upon specifying UTC in the tests, I noticed a small inconsistency.

When specifying the DateTime with ticks, or with unix time, the kind was set as Unspecified instead of Utc.

This was causing issues when combined with the -AsUTC parameter. When performing the conversion, the ToUniversalTime() method is called. This caused inconsistencies, as the local offset would be applied. The documentation for this method confirms this behaviour:

Unspecified | The current DateTime object is assumed to be a local time, and the conversion is performed as if Kind were Local.

To get around this, we can set the kind to Utc explicitly when using ticks or unix time. I've made the following change to facilitate this:

dateToUse = Date

to

dateToUse = DateTime.SpecifyKind(Date, DateTimeKind.Utc);

@iSazonov

Copy link
Copy Markdown
Collaborator

Jack Casey (@jackdcasey) Please create new issue in PowerShell-Docs repo for the new parameter and add reference in the PR description. Thanks!

@jackdcasey

Copy link
Copy Markdown
Contributor Author

Opened issue in MicrosoftDocs/PowerShell-Docs: MicrosoftDocs/PowerShell-Docs#5727

@TravisEz13 Travis Plunk (TravisEz13) changed the title Add FromUnixTime switch to Get-Date to allow Unix time input Add -FromUnixTime to Get-Date to allow Unix time input Apr 7, 2020
@TravisEz13
Travis Plunk (TravisEz13) merged commit 46071b7 into PowerShell:master Apr 7, 2020
@iSazonov

Copy link
Copy Markdown
Collaborator

Jack Casey (@jackdcasey) Thanks for your contribution!

@jackdcasey

Copy link
Copy Markdown
Contributor Author

Ilya (@iSazonov) Thank you for all the help!

@ghost

Copy link
Copy Markdown

🎉v7.1.0-preview.2 has been released which incorporates this pull request.:tada:

Handy links:

@ghost ghost mentioned this pull request Apr 23, 2020
@aetos382

Copy link
Copy Markdown
Contributor

Negative UnixTime is not supported?

PS /> Get-Date -Date -1 -FromUnixTime
Get-Date: Cannot bind parameter 'Date'. Cannot convert value "-1" to type "System.DateTime". Error: "String '-1' was not recognized as a valid DateTime."

@iSazonov

Copy link
Copy Markdown
Collaborator

aetos (@aetos382) No. If you have a feature request please open new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add One New DateTime Parsing Format

5 participants