Reputation: 419
I am parsing an SQLite database using the PowerShell SQLite module, and a couple of the return values are created and modified, both of which are in Unix time.
What I would like to do is somehow convert that into "human time". I have removed some of the other SQL queries for ease of reading.
Import-Module SQLite
mount-sqlite -name GoogleDrive -dataSource E:\Programming\new.db
$cloud_entry = Get-ChildItem GoogleDrive:\cloud_entry
foreach ($entry in $cloud_entry)
{
$entry.created
}
The output looks like a large column of Unix timestamps:
1337329458
Update: I ultimately went with the following:
$ctime = $entry.created
[datetime]$origin = '1970-01-01 00:00:00'
$origin.AddSeconds($ctime)
Upvotes: 22
Views: 83420
Reputation: 357
Everything I tried returned an error that the unix date was 'out of range'. Here is what finally worked. After edits. Thanks to Kumpf. :)
PS> get-date ((Get-Date "1970-01-01 00:00:00.000Z") + ([TimeSpan]::FromSeconds($unixTime))) -uformat "%m/%d/%Y %T"
11/05/2024 06:10:56
Borrowed from miljbee and Peter Mo
Upvotes: 0
Reputation: 628
As of PowerShell 7.1, you can use -UnixTimeSeconds
follows...
PS> Get-Date -UnixTimeSeconds 0 -AsUTC
Thursday, January 1, 1970 12:00:00 AM
PS> Get-Date -UnixTimeSeconds 1 -AsUTC
Thursday, January 1, 1970 12:00:01 AM
PS> Get-Date -UnixTimeSeconds 2 -AsUTC
Thursday, January 1, 1970 12:00:02 AM
Upvotes: 0
Reputation: 1310
Use:
# Creates a timestamp representation in UTC. Use .LocalDateTime for local time.
(([System.DateTimeOffset]::FromUnixTimeSeconds($unixTime)).DateTime).ToString("s")
FromUnixTimeMilliseconds
is also available.
ToString("s")
: Sortable: "The pattern reflects a defined standard (ISO 8601)"
Ref.: Standard Date and Time Format Strings, The Sortable ("s") Format Specifier
Upvotes: 31
Reputation: 179
I know this is a super old question, but just wanted to share that Powershell 7's Get-Date
has native support for converting Unix time now (which I discovered thanks to this question). By default, it will convert it to local time.
PS> Get-Date -UnixTimeSeconds 1338194440
Monday, May 28, 2012 4:40:40 AM
PS> (Get-Date -UnixTimeSeconds 1338194440).Kind
Local
If you want it in UTC, just add -AsUTC
to the command.
PS> Get-Date -UnixTimeSeconds 1338194440 -AsUTC
Monday, May 28, 2012 8:40:40 AM
Upvotes: 3
Reputation: 1259
The DateTime type in PowerShell 7 has a static UnixEpoch value defined, so this can be done even more tersely now:
[DateTime]::UnixEpoch.AddSeconds($unixTime)
Note: This was introduced in .NET Core 2.1, so I believe it is applicable to PowerShell versions since 6.1.
Upvotes: 1
Reputation: 842
I found that neither of the suggested solutions solved my needs. So I adjusted and propose the following:
function ConvertFrom-UnixTime {
[CmdletBinding(DefaultParameterSetName = "Seconds")]
param (
[Parameter(Position = 0,
ValueFromPipeline = $true,
Mandatory = $true,
ParameterSetName = "Seconds")]
[int]
$Seconds,
[Parameter(Position = 0,
ValueFromPipeline = $true,
Mandatory = $true, ParameterSetName = "Miliseconds")]
[bigint]
$Miliseconds
)
Begin {
$date = (Get-Date "1970-01-01 00:00:00.000Z")
}
Process {
switch ($PSCmdlet.ParameterSetName) {
"Miliseconds" {
$date = $date.AddMilliseconds($Miliseconds)
}
Default {
$date = $date.AddSeconds($Seconds);
}
}
}
End {
$date
}
}
Set-Alias -Name epoch -Value ConvertFrom-UnixTime
Usage:
1633694244| epoch
1633694244565 | epoch
Both outputs:
8. oktober 2021 12:57:24
Upvotes: 1
Reputation: 9684
Not bringing anything new to the table, but a pipable version of Fredrics answer:
function epoch() {
Param(
[Parameter(ValueFromPipeline)]$epochTime,
[Switch]$Ms
)
Process {
if ($Ms) {
[System.DateTimeOffset]::FromUnixTimeMilliseconds($epochTime)
} else {
[System.DateTimeOffset]::FromUnixTimeSeconds($epochTime)
}
}
}
"1628043561861" | epoch -Ms
DateTime : 04.08.2021 02:19:21
UtcDateTime : 04.08.2021 02:19:21
LocalDateTime : 04.08.2021 04:19:21
Date : 04.08.2021 00:00:00
Day : 4
DayOfWeek : Wednesday
DayOfYear : 216
Hour : 2
Millisecond : 861
Minute : 19
Month : 8
Offset : 00:00:00
Second : 21
Ticks : 637636403618610000
UtcTicks : 637636403618610000
TimeOfDay : 02:19:21.8610000
Year : 2021
Upvotes: 4
Reputation: 763
I wanted to be sure that I was calculating from UTC, so I added the SpecifyKind
:
$epochStart = New-Object -Type DateTime -ArgumentList 1970, 1, 1, 0, 0, 0, 0
$epochStart = [DateTime]::SpecifyKind($epochStart,[DateTimeKind]::Utc)
$DateTimeUTC = $epochStart.AddSeconds($UnixTimestamp)
Upvotes: 1
Reputation: 13743
See Convert a Unix timestamp to a .NET DateTime.
You can easily reproduce this in PowerShell.
$origin = New-Object -Type DateTime -ArgumentList 1970, 1, 1, 0, 0, 0, 0
$whatIWant = $origin.AddSeconds($unixTime)
Upvotes: 31
Reputation: 350
A simple one-liner:
(Get-Date "1970-01-01 00:00:00.000Z") + ([TimeSpan]::FromSeconds($unixTime))
Upvotes: 4
Reputation: 409
Function Convert-FromUnixDate ($UnixDate) {
[timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($UnixDate))
}
$niceTime = Convert-FromUnixDate $ctime
PS C:\> $niceTime
Friday, 18 May 2012 8:24:18 p.m.
Upvotes: 19
Reputation: 91
$date = get-date "1/1/1970"
$date.AddSeconds($unixTime).ToLocalTime()
Upvotes: 8
Reputation: 419
$ctime = $entry.created
[datetime]$origin = '1970-01-01 00:00:00'
$origin.AddSeconds($ctime)
Upvotes: 1