Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Unix time with PowerShell

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)
like image 704
mrwh1t3 Avatar asked May 28 '12 08:05

mrwh1t3


People also ask

How do I convert epoch time in PowerShell?

Using the Get-Date cmdlet with -UFormat %s in PowerShell, it converts the date time to epoch time. To convert the epoch time to date time, use Microsoft . Net class library System. TimeSpan to add the seconds to the epoch time date of January 01, 1970.

How do I convert a string to a date in PowerShell?

Use ParseExact to Convert String to DateTime in PowerShell We have a variable $date , which contains the date in string format. You can check the data type using the GetType() method. Let's convert the string to DateTime. When converting, the date and time format in a string must match the specified format in DateTime.

How do I get the current date in PowerShell?

The Get-Date cmdlet gets a DateTime object that represents the current date or a date that you specify. Get-Date can format the date and time in several . NET and UNIX formats. You can use Get-Date to generate a date or time character string, and then send the string to other cmdlets or programs.

What is epoch format?

In a computing context, an epoch is the date and time relative to which a computer's clock and timestamp values are determined. The epoch traditionally corresponds to 0 hours, 0 minutes, and 0 seconds (00:00:00) Coordinated Universal Time (UTC) on a specific date, which varies from system to system.


4 Answers

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)
like image 123
JohnB Avatar answered Oct 31 '22 10:10

JohnB


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.
like image 23
uknzguy Avatar answered Oct 31 '22 08:10

uknzguy


Use:

(([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

like image 39
Fredrick Avatar answered Oct 31 '22 08:10

Fredrick


$date = get-date "1/1/1970"
$date.AddSeconds($unixTime).ToLocalTime()
like image 20
VMwareWolf Avatar answered Oct 31 '22 10:10

VMwareWolf