powershelldatetime-format

Disable conversion to UTC timezone after deserialization of a response from Invoke-Restmethod


I'm using the Invoke-RestMethod to get the data from REST API. One of the attributes in response is the date. When using Postman or other tools to get the data the date is returned correctly but when I'm using PowerShell (version 5.1.19041.906) and its Invoke-RestMethod like this:

$response = Invoke-RestMethod -Method Get -Uri $url -Headers $requestHeaders

All values from the date attribute are automatically converted to UTC. Is there any way how to disable this shift? I need the original values returned from the API.


Solution

  • Invoke-RestMethod, when given a JSON response, automatically parses it into a [pscustomobject] graph; in a manner of speaking, it has ConvertFrom-Json built in.

    When ConvertFrom-Json does recognize what are invariably string representation of dates in the input JSON, it converts them to [datetime] instances.

    In Windows PowerShell (v5.1, the latest and final version) and up to PowerShell (Core) 7.4.x, you get NO control over what kind of [datetime] instances are constructed, as reflected in their .Kind property:

    Update:

    While Windows PowerShell will see no new features, in PowerShell 7.5+,[1] based on the feature request in GitHub issue #13598, ConvertFrom-Json will have a -DateKind parameter, so as to allow explicitly requesting the date kind of interest, and to alternatively construct [datetimeoffset] instances, which are generally superior to [datetime].
    In the absence of a -DateKind argument in a given call, the (PowerShell (Core)) behavior described above will continue to apply.


    Workaround:

    The following snippet walks a [pscustomobject] graph, as returned from Invoke-RestMethod and explicitly converts any [datetime] instances encountered to Local instances in place (Unspecified instances are treated as Local):

    # Call Invoke-RestMethod to retrieve and parse a web service's JSON response.
    $fromJson = Invoke-RestMethod ... 
    
    # Convert any [datetime] instances in the object graph that aren't already 
    # local dates (whose .Kind value isn't already 'Local') to local ones.
    & {
      # Helper script block that walks the object graph.
      $sb = {
        foreach ($el in $args[0]) { # iterate over elements (if an array)
          foreach ($prop in $el.psobject.Properties) {
            # iterate over properties
            if ($dt = $prop.Value -as [datetime]) {
              switch ($dt.Kind) {
                'Utc' { $prop.Value = $dt.ToLocalTime() }
                # Note: calling .ToLocalTime() is not an option, because it interprets
                #       an 'Unspecified' [datetime] as UTC.
                'Unspecified' { $prop.Value = [datetime]::new($dt.Ticks, 'Local') }
              }
            }
            elseif ($prop.Value -is [Array] -or $prop.Value -is [System.Management.Automation.PSCustomObject]) { 
              & $sb $prop.Value # recurse
            }
          }
        }
      }
      # Start walking.
      & $sb $args[0]
    } $fromJson
    
    # Output the transformed-in-place object graph
    # that now contains only Local [datetime] instances.
    $fromJson
    

    [1] See GitHub PR #20925. It should first become available in PowerShell 7.5.0-preview.3, the next 7.5 preview version to be released as of this writing.