开发者

A Powershell function to convert unix time to string?

I'm trying to read the date fields of Firefox places.sqlite datedase using ADO.NET and PowerShell.

Obviously these fields are in Unix time.

I'm looking for a conversion to .Net datetime or string

Edit: I want to bind the datarow to a WPF GridView.

开发者_运维知识库

I need either a way to do the conversion within the SQL query or some way while binding the datarows to the grid.


Something like this should put you on the right path:

[TimeZone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($unix))


[timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds(1303743289))


function Convert-UnixTimeToDateTime([int]$UnixTime)
{
    (New-Object DateTime(1970, 1, 1, 0, 0, 0, 0, [DateTimeKind]::Utc)).AddSeconds($UnixTime)
}

This returns an object with the "kind" set to Utc. Then, if you want local time, call the ToLocalTime method on the resulting object.


The working solution to my problem is

SELECT datetime (b.dateAdded / 1000000, 'unixepoch', 'localtime') dateAdded  from moz_bookmarks

BTW it is partly hidden in the the original documentation.

The other part is that you have to find out, that you must divide the integer by 1000000 to get the seconds.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜