0

I'm reading a json from my local transport company. It contains the dep./arrival times in rfc3339 format. Converting them to my local time zone in PS on Windows works like expected, doing the same on PS on Alpine Linux Container (w. correct time zone set) results in different output. Feeling is that during conversion from json the final Z gets lost so get-date sees the time as being already in local time, but why?

Demo code to show the effect:

$hashtable = @{}
$hashtable.add( "departure", "2023-01-27T20:51:00Z") 

get-date($hashtable.departure) -Format "HH:mm"

$json = $hashtable|ConvertTo-Json
$json2 = $json|ConvertFrom-Json

get-date($json2.departure) -Format "HH:mm")

On Windows: 21:51 21:51

On Alpine Linux: 21:51 20:51

  • 1
    In pwsh Core you would need `$json2.departure.ToLocalTime().ToString('HH:mm')` to get the same result. It will convert datetime strings to `datetime` instances // `$json2.departure` is already a `datetime`, no need to `get-date($json2.departure)` – Santiago Squarzon Jan 27 '23 at 21:27
  • `Z` stands for universal, UTC (its Zulu Time) – Santiago Squarzon Jan 27 '23 at 21:29
  • 1
    As an aside, re your syntax: `get-date($hashtable.departure)` -> `get-date $hashtable.departure` - PowerShell commands are invoked like _shell commands_, with whitespace-separated arguments; see [this answer](https://stackoverflow.com/a/65208621/45375) for more information. – mklement0 Jan 27 '23 at 21:36
  • 1
    I hope the linked duplicate explains the behavior you're seeing in more detail; @Santiago has provided an effective solution in his comment. – mklement0 Jan 27 '23 at 21:39

0 Answers0