Rick G
Rick G

Reputation: 1

How to convert from epoch time to a human readable format in PowerShell?

I'm trying to convert epoch time to a readable format. I have either a text file or a CSV file that contains 5 columns of information which includes the Unixtime in one of the columns. I would like to take the imported file and replace the Unixtime with a readable date and output all information to another file.

What I have so far is given in the below example.

Data:

id       created        state     description
--       --------       ------    -----------
12345    1581171592232  pending   changes for....

Code:

$taskarray = @()
Import-Csv c:\tasks.csv | ForEach-Object {$taskarray += $_.created}
foreach($tdate in $taskarray){
    $time = [datetimeoffset]::FromUnixTimeMilliseconds($tdate).ToString('yyyy-MM-dd-HH:mm:ss')
    $time
}

Upvotes: 0

Views: 3867

Answers (2)

Theo
Theo

Reputation: 61253

First remark: the example you give has an invalid value for the Unix timestamp, which should be between -62135596800 and 253402300799 inclusive.

Second remark: A Unix timestamp is in UTC. I'm guessing you want the returned date in LocalTime.

I would suggest using a small helper function for this:

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 }
}

Supposing your input file looks like

id,created,state,description
12345,1598093799,pending,changes for....
67890,1598093800,pending,changes for....
74185,1598093801,pending,changes for....

You can use the function like tis:

(Import-Csv -Path 'D:\Test\TheInputFile.csv') | 
Select-Object id, 
              @{Name = 'created'; Expression = {'{0:yyyy-MM-dd-HH:mm:ss}' -f (ConvertFrom-UnixTimeStamp -UnixTimeStamp $_.created)}}, 
              state, description |
Export-Csv -Path 'D:\Test\TheConvertedFile.csv' -NoTypeInformation

Upvotes: 1

vvvv4d
vvvv4d

Reputation: 4095

(([System.DateTimeOffset]::FromUnixTimeSeconds($tdate)).DateTime).ToString("s")

Or

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

Upvotes: 1

Related Questions