Skip to content

Get-Date -FromUnixTime should support negative values. #12976

Closed

Description

@aetos382

Summary of the new feature/enhancement

PR #12179 added FromUnixTime parameter to the Get-Date command.
In PowerShell 7.1.0-preview.3, the following command causes an error.

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."

This command should support negative unix time.

On Ubuntu, the following commands will not return an error.

$ date --date "@-1"
Wed Dec 31 23:59:59 UTC 1969

Proposed technical implementation details (optional)

The Date parameter of the Get-Date command has type DateTime. If an integer value is passed for this parameter, it will be interpreted as Ticks. This must be a positive value.

In other words, the following code is invalid.

PS /> [datetime] -1
InvalidArgument: Cannot convert value "-1" to type "System.DateTime". Error: "Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks. (Parameter 'ticks')"

Unix time can take a negative value, meaning that it is before Unix Epoch.

To allow the Date parameter to take a negative integer value, the type of this parameter must be changed to long.
But this is a breaking change.

I think the FromUnixTime parameter should be able to take a value, as was suggested in #11719.

Activity

  1. added
    Issue-Enhancement
    on Jun 17, 2020
  2. added
    Issue-Discussion
    Issue-Question
    and removed
    Issue-Enhancement
    on Jun 17, 2020
  3. iSazonov commented on Jun 17, 2020

    @iSazonov
    Collaborator

    We need to see real scenarios where the feature could be useful.

  4. vexx32 commented on Jun 17, 2020

    @vexx32
    Collaborator

    Rather than take the value in from -Date it almost seems better to just split the parameter set, and have -FromUnixTime take a value rather than be a switch parameter. 🤔

  5. rkeithhill commented on Jun 17, 2020

    @rkeithhill
    Collaborator

    Rain Sallow (/u/ta11ow) (@vexx32) Agreed. BTW that's how I assumed it worked. Because of positional binding, it "appears" to work that way:

    PS. get-date -FromUnixTime 1242342344
    
    Thursday, May 14, 2009 11:05:44 PM
    
  6. iSazonov commented on Jun 17, 2020

    @iSazonov
    Collaborator

    What is a type of the parameter? Signed long?

  7. vexx32 commented on Jun 17, 2020

    @vexx32
    Collaborator

    Yeah, I think that makes the most sense. That does appear to have been the original proposal, too, but that didn't carry through to the initial implementation, it seems.

  8. added
    Resolution-Fixed
    and removed
    Issue-Discussion
    on Aug 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-QuestionResolution-FixedWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions