Skip to content

Conversation

@jackdcasey
Copy link
Contributor

@jackdcasey jackdcasey commented Mar 23, 2020

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

@ghost ghost assigned TravisEz13 Mar 23, 2020
@iSazonov 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 iSazonov added this to the 7.1.0-preview.1 milestone Mar 23, 2020
@TravisEz13 TravisEz13 requested a review from SteveL-MSFT March 23, 2020 20:58
@jackdcasey jackdcasey requested a review from iSazonov April 5, 2020 07:21
@jackdcasey
Copy link
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 iSazonov requested a review from daxian-dbw April 7, 2020 12:02
@iSazonov
Copy link
Collaborator

iSazonov commented Apr 7, 2020

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

@jackdcasey
Copy link
Contributor Author

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

@TravisEz13 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 TravisEz13 merged commit 46071b7 into PowerShell:master Apr 7, 2020
@iSazonov
Copy link
Collaborator

iSazonov commented Apr 8, 2020

@jackdcasey Thanks for your contribution!

@jackdcasey
Copy link
Contributor Author

@iSazonov Thank you for all the help!

@ghost
Copy link

ghost commented Apr 23, 2020

🎉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
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
Collaborator

@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