简体   繁体   English

一个Powershell函数将unix时间转换为字符串?

[英]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. 我正在尝试使用ADO.NET和PowerShell读取Firefox places.sqlite的一些日期字段。

Obviously these fields are in Unix time. 显然这些字段是在Unix时间。

I'm looking for a conversion to .Net datetime or string 我正在寻找转换为.Net日期时间或字符串

Edit: I want to bind the datarow to a WPF GridView. 编辑:我想将数据行绑定到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. 我需要一种方法在SQL查询中进行转换,或者在将数据行绑定到网格时使用某种方式。

这样的事情会让你走上正确的道路:

[TimeZone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($unix))
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. 这将返回一个对象,其中“kind”设置为Utc。 Then, if you want local time, call the ToLocalTime method on the resulting object. 然后,如果您想要本地时间,请在结果对象上调用ToLocalTime方法。

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

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. 另一部分是您必须找出,您必须将整数除以1000000以获得秒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM