A Powershell function to convert unix time to string?

ε祈祈猫儿з 提交于 2021-02-07 19:51:00

问题


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.


回答1:


Something like this should put you on the right path:

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



回答2:


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



回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/5779244/a-powershell-function-to-convert-unix-time-to-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!