如何在PowerShell中将历元时间转换为人类可读的格式?

如何在PowerShell中将历元时间转换为人类可读的格式?,powershell,Powershell,我正在尝试将历元时间转换为可读格式。我有一个文本文件或CSV文件,其中包含5列信息,其中一列包含Unixtime。我想获取导入的文件,用可读的日期替换Unixtime,并将所有信息输出到另一个文件 到目前为止,我已经在下面的例子中给出了 数据: id创建状态描述 -- -------- ------ ----------- 12345 1581171592232…的挂起更改。。。。 代码: $taskarray=@() 导入Csv c:\tasks.Csv | F

我正在尝试将历元时间转换为可读格式。我有一个文本文件或CSV文件,其中包含5列信息,其中一列包含Unixtime。我想获取导入的文件,用可读的日期替换Unixtime,并将所有信息输出到另一个文件

到目前为止,我已经在下面的例子中给出了

数据:

id创建状态描述
--       --------       ------    -----------
12345 1581171592232…的挂起更改。。。。
代码:

$taskarray=@()
导入Csv c:\tasks.Csv | ForEach对象{$taskarray+=$\.created}
foreach($tdate in$taskarray){
$time=[datetimeoffset]::fromUnixtimeMissions($tdate).ToString('yyyy-MM-dd-HH:MM:ss'))
$time
}
(([System.DateTimeOffset]::FromUnixTimeSeconds($tdate)).DateTime)到字符串(/code>

(获取日期“1970-01-01 00:00:00.000Z”)+([TimeSpan]::FromSeconds($tdate)
([System.DateTimeOffset]::FromUnixTimeSeconds($tdate)).DateTime)。ToString(“s”)


(Get Date“1970-01-01 00:00:00.000Z”)+([TimeSpan]::FromSeconds($tdate)

第一句话:您给出的示例具有无效的Unix时间戳值,该值应介于-62135596800和253402300799之间(含)

第二句话:Unix时间戳是UTC。我猜您希望返回的日期是LocalTime

我建议使用一个小的辅助函数:

function ConvertFrom-UnixTimeStamp([Int64]$UnixTimeStamp, [switch]$AsUTC) {
    while ($UnixTimeStamp -lt -62135596800 -or $UnixTimeStamp -gt 253402300799) {
        # Assume $UnixTimeStamp to include milliseconds
        $UnixTimeStamp = [int64][math]::Truncate([double]$UnixTimeStamp / 1000)
    }
    if ($UnixTimeStamp -gt [Int32]::MaxValue) {
        # see: https://en.wikipedia.org/wiki/Year_2038_problem
        Write-Warning "The given value exceeds the [Int32]::MaxValue of 2147483647 and therefore enters the Year2038 Unix bug.."
    }
    # the Unix Epoch is January 1, 1970 midnight in UTC
    # older PowerShell versions use:
    # [DateTime]$epoch = New-Object System.DateTime 1970, 1, 1, 0, 0, 0, 0, Utc
    [DateTime]$epoch = [DateTime]::new(1970, 1, 1, 0, 0, 0, 0, 'Utc')

    $date = $epoch.AddSeconds($UnixTimeStamp)
    if ($AsUTC) { $date } else { $date.ToLocalTime() }

    # or use:
    # if ($AsUTC) { [DateTimeOffset]::FromUnixTimeSeconds($UnixTimeStamp).UtcDateTime }
    # else { [DateTimeOffset]::FromUnixTimeSeconds($UnixTimeStamp).LocalDateTime }
}
假设您的输入文件如下所示:

id,created,state,description 12345,1598093799,pending,changes for.... 67890,1598093800,pending,changes for.... 74185,1598093801,pending,changes for....
第一句话:您给出的示例中Unix时间戳的值无效,该值应介于-62135596800和253402300799之间(包括-62135596800和253402300799)

第二句话:Unix时间戳是UTC。我猜您希望返回的日期是LocalTime

我建议使用一个小的辅助函数:

function ConvertFrom-UnixTimeStamp([Int64]$UnixTimeStamp, [switch]$AsUTC) {
    while ($UnixTimeStamp -lt -62135596800 -or $UnixTimeStamp -gt 253402300799) {
        # Assume $UnixTimeStamp to include milliseconds
        $UnixTimeStamp = [int64][math]::Truncate([double]$UnixTimeStamp / 1000)
    }
    if ($UnixTimeStamp -gt [Int32]::MaxValue) {
        # see: https://en.wikipedia.org/wiki/Year_2038_problem
        Write-Warning "The given value exceeds the [Int32]::MaxValue of 2147483647 and therefore enters the Year2038 Unix bug.."
    }
    # the Unix Epoch is January 1, 1970 midnight in UTC
    # older PowerShell versions use:
    # [DateTime]$epoch = New-Object System.DateTime 1970, 1, 1, 0, 0, 0, 0, Utc
    [DateTime]$epoch = [DateTime]::new(1970, 1, 1, 0, 0, 0, 0, 'Utc')

    $date = $epoch.AddSeconds($UnixTimeStamp)
    if ($AsUTC) { $date } else { $date.ToLocalTime() }

    # or use:
    # if ($AsUTC) { [DateTimeOffset]::FromUnixTimeSeconds($UnixTimeStamp).UtcDateTime }
    # else { [DateTimeOffset]::FromUnixTimeSeconds($UnixTimeStamp).LocalDateTime }
}
假设您的输入文件如下所示:

id,created,state,description 12345,1598093799,pending,changes for.... 67890,1598093800,pending,changes for.... 74185,1598093801,pending,changes for....
好的,但是我如何将具有新可读日期的所有现有信息传输到新文件好的,但是如何将具有新可读日期的所有现有信息传输到新文件file@RickG请尝试编辑后的答案。该函数现在还可以处理包含毫秒的unix时间戳,如示例中所示:
158171592232
转换为<代码> 2020—02-08:15:19:52/代码> Rigg我们没有收到你的消息…我的答案解决了你的问题吗?如果是的话,请点击一下。✓ 左边的图标。这将帮助其他有类似问题的人更容易找到它。@RickG请尝试编辑后的答案。该函数现在还可以处理包含毫秒的unix时间戳,如您的示例中所示:
158171592232
转换为
2020-02-08-15:19:52
@RickG我们没有收到您的消息。我的答案解决了吗你的问题?如果是,请点击✓ 左边的图标。这将帮助其他有类似问题的人更容易找到它。