Powershell 将历元时间转换为CDT时间

Powershell 将历元时间转换为CDT时间,powershell,Powershell,目前我正在使用下面的函数转换历元时间 Function convertFrom-epoch { [CmdletBinding()] param ([Parameter(ValueFromPipeline=$true)]$epochdate) if (!$psboundparameters.count) {help -ex convertFrom-epoch | Out-String | Remove-EmptyLines; return} if ((&q

目前我正在使用下面的函数转换历元时间

Function convertFrom-epoch {
    [CmdletBinding()]
    param ([Parameter(ValueFromPipeline=$true)]$epochdate)
    
    if (!$psboundparameters.count) {help -ex convertFrom-epoch | Out-String | Remove-EmptyLines; return}
    if (("$epochdate").length -gt 10 ) {(Get-Date -Date "01/01/1970").AddMilliseconds($epochdate)}
    else {(Get-Date -Date "01/01/1970").AddSeconds($epochdate)}
}

convertFrom-epoch 1597044600
结果是格林尼治时间2020年8月10日星期一07:30:00

有没有办法把时间改成CDT时间而不是GMT时间

Function convertFrom-epoch {
    [CmdletBinding()]
    param ([Parameter(ValueFromPipeline=$true)]$epochdate)
    
    if (!$psboundparameters.count) {help -ex convertFrom-epoch | Out-String | Remove-EmptyLines; return}
    if (("$epochdate").length -gt 10 ) {(Get-Date -Date "01/01/1970").AddMilliseconds($epochdate)}
    else { $Result = (Get-Date -Date "01/01/1970").AddSeconds($epochdate) }

    $cstzone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Central Standard Time")
    [System.TimeZoneInfo]::ConvertTimeFromUtc($Result.ToUniversalTime(), $cstzone)
}

convertFrom-epoch 1599838249
我从这个问题中得到了灵感:

“中央标准时间”对我不起作用。我的系统有“美国/芝加哥”:


因此,我假设白天节省时间将自动调整,即它将显示芝加哥的时间。

CDT是机器上配置的本地时区吗?
Function convertFrom-epoch {
    [CmdletBinding()]
    param ([Parameter(ValueFromPipeline=$true)]$epochdate)
    
    if (!$psboundparameters.count) {help -ex convertFrom-epoch | Out-String | Remove-EmptyLines; return}
    if (("$epochdate").length -gt 10 ) {(Get-Date -Date "01/01/1970").AddMilliseconds($epochdate)}
    else {[System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId((Get-Date -Date "01/01/1970").AddSeconds($epochdate), 'America/Chicago')}
}

convertFrom-epoch 1597044600
[System.TimeZoneInfo]::GetSystemTimeZones() | % {if ($_.Id -eq "America/Chicago") {$_}}

Id                         : America/Chicago
DisplayName                : (UTC-06:00) Central Standard Time
StandardName               : Central Standard Time
DaylightName               : Central Daylight Time
BaseUtcOffset              : -06:00:00
SupportsDaylightSavingTime : True