Skip to content

Add One New DateTime Parsing Format #11719

Closed

Description

Yesterday I posted a suggestion that we have a new special format for Get-Date that creates the SecondsSinceEpoch (aka UnixTime) for users.

Additionally I think we should have built into the language the ability to easily parse a UnixEpoch timestamp back into a DateTime.

Various existing parsing that works today:

$aDate = [DateTime]"2020-01-28T17:32:18.4986003-06:00" #  (conversion back from the format of Format "o")
$bDate = [DateTime]"2020-01-28T17:36:39" #  (conversion back from the format of Format "s")
$cDate = [DateTime]"Tue, 28 Jan 2020 17:37:43 GMT" #  (conversion back from the format of Format "r")
$dDate = [DateTime]"2020-01-28 17:38:24Z" #  (conversion back from the format of Format "u")
$eDate = [DateTime]"7/25/2013 6:37:31 PM" 
$fDate = [DateTime]"2013-07-25 14:26:00" 
$gDate = [DateTime]"7/25/2013 14:26:00"
$hDate = [DateTime]"2013-07-25 6:37:31 PM"

But we can't currently put in $AUnixDate = [DateTime]"1374755180" and end up with a valid DateTime object (which would be of UTC kind)

A discussion by Randy James at https://stackoverflow.com/questions/17859421/parse-datetime-in-multiple-formats suggests perhaps something along these lines for processing this in another language:

public DateTime ParseRequestDate()
{
    // https://stackoverflow.com/questions/2883576/how-do-you-convert-epoch-time-in-c

    CultureInfo enUS = new CultureInfo("en-US");

    var dt = "1374755180";
    //var dt = "7/25/2013 6:37:31 PM";
    //var dt = "2013-07-25 14:26:00";

    DateTime dateValue;
    long dtLong;

    // Scenario #1
    if (long.TryParse(dt, out dtLong))
        return dtLong.FromUnixTime();

    // Scenario #2 & #3
    var formatStrings = new string[] { "MM/dd/yyyy hh:mm:ss tt", "yyyy-MM-dd hh:mm:ss" };
    if (DateTime.TryParseExact(dt, formatStrings, enUS, DateTimeStyles.None, out dateValue))
        return dateValue;

    throw new SomeException("Don't know how to parse...");
}

Activity

  1. added
    Issue-Enhancement
    on Jan 29, 2020
  2. iSazonov commented on Jan 30, 2020

    @iSazonov
    Collaborator

    Looks good for me but I'd put the code for 1 scenario in last place.

    Steve Lee (@SteveL-MSFT) Please triage.

  3. mklement0 commented on Feb 1, 2020

    @mklement0
    Contributor

    Using .NET methods, you parse Unix epoch time values as follows (given PowerShell's flexible type conversions, you can also use a string in the following method call):

    PS> [datetimeoffset]::FromUnixTimeSeconds(1374755180).UtcDateTime
    Thursday, July 25, 2013 12:26:20 PM

    As for making that easier:

    Note that PowerShell's casts translate to looking for and invoking, as appropriate, the following behind the scenes:

    • a single-parameter constructor on the target type whose parameter is of the same type as the operand
    • with string operands, a call to .Parse(<operand>[, <invariantCulture>)

    • [datetime] <string> translates to the latter, and it doesn't recognize Unix epoch time, and we shouldn't try to add magic there.

    • With a numeric operand, [datetime] <num> maps onto constructor DateTime(long ticks); ticks are very fine-grained units with a starting point in the year 1 (sic); try [datetime] 0.

      • The same happens when you call Get-Date 0 (short for: Get-Date -Date 0)
      • We shouldn't try to change this logic.

    Therefore, if we want to make this easier, I suggest adding a new parameter to Get-Date, along the lines of -FromUnixTime <long>, which would be mutually exclusive with -Date.

  4. iSazonov commented on Feb 2, 2020

    @iSazonov
    Collaborator

    We shouldn't try to change this logic.

    It would be a breaking change. Michael Klement (@mklement0) Thanks! -FromUnixTime is best compromise to address the request.

  5. scotthardwick commented on Feb 2, 2020

    @scotthardwick
    Author

    Michael Klement (@mklement0) Thank you for suggesting this. I like your idea MUCH better. I think it has the added benefit that by being part of Get-Date, its implementation will be documented in the Get-Date documentation which is where I think most people would look if trying to figure out if PowerShell can convert UnixEpoch times. +1 for -FromUnixTime

  6. SteveL-MSFT commented on Feb 6, 2020

    @SteveL-MSFT
    Member

    Agree with Michael Klement (@mklement0)'s proposal

  7. added
    Up-for-Grabs
    on Feb 6, 2020
  8. jackdcasey commented on Mar 23, 2020

    @jackdcasey
    Contributor

    I believe this would be super handy! I'd be happy to take a swing at it 😁

  9. added
    Resolution-Fixed
    and removed
    Up-for-Grabs
    on Apr 8, 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-EnhancementResolution-FixedWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions