sol*_*ous 4 powershell datetime-format
我正在使用 Invoke-RestMethod 从 REST API 获取数据。响应的属性之一是日期。当使用 Postman 或其他工具获取数据时,日期会正确返回,但是当我使用 PowerShell(版本 5.1.19041.906)及其 Invoke-RestMethod 时,如下所示:
$response = Invoke-RestMethod -Method Get -Uri $url -Headers $requestHeaders
Run Code Online (Sandbox Code Playgroud)
日期属性中的所有值都会自动转换为 UTC。有什么办法可以禁用这种转变吗?我需要从 API 返回的原始值。
Invoke-RestMethod,当给出 JSON 响应时,自动将其解析为[pscustomobject]图表;从某种意义上说,它内置了.ConvertFrom-Json
当识别出输入 JSON 中日期的字符串ConvertFrom-Json表示形式时,会将它们转换为实例。[datetime]
在Windows PowerShell(v5.1,最新也是最终版本)和PowerShell (Core) 7.2中,您无法控制构造何种类型的[datetime]实例,如其.Kind属性所示:
在需要自定义日期字符串格式(例如)的Windows PowerShell中,您总是会获得实例。"\/Date(1633984531266)\/"Utc
在PowerShell (Core) 7+中,它还识别 ISO 8601 日期时间字符串(的变体)字符串值(例如"2021-10-11T13:27:12.3318432-04:00"),该.Kind值取决于字符串值的具体情况:
Z(表示 UTC),您将获得一个Utc实例。-04:00您将获得一个Local实例(即使偏移值为00:00)
Unspecified实例。虽然Windows PowerShell不会看到任何新功能,但PowerShell(核心)还是有希望的:GitHub 问题 #13598建议向 中添加一个-DateTimeKind参数ConvertFrom-Json,以便允许显式请求兴趣类型,或者构建[datetimeoffset]实例,这是更好的选择。
解决方法:
Invoke-WebRequest响应的.Content属性执行您自己的解析,如Mathias R. Jessen指出的那样。以下代码片段遍历一个[pscustomobject]图表,从返回并将遇到的Invoke-RestMethod任何[datetime]Local实例显式转换为就地实例(Unspecified实例被视为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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
651 次 |
| 最近记录: |