简体   繁体   English

将纪元时间戳转换为DateTime

[英]Converting Epoch timestamp to DateTime

Hello i am experiencing a strange problem, my scenario is that i am reading the history of google chrome browser stored in sqlite database using c# language. 您好,我遇到一个奇怪的问题,我的情况是,我正在使用c#语言读取存储在sqlite数据库中的Google Chrome浏览器的历史记录。 every thing goes fine except the timestamp that chrome stores in epoch format i have to upload the data to server using php and store it in the MYSQL database. 除了chrome以时代格式存储的时间戳之外,一切都正常,我必须使用php将数据上传到服务器,并将其存储在MYSQL数据库中。 Now my problem is that i can't manage to convert that epoch timestamp to MYSQL based datetime. 现在我的问题是我无法将那个时代的时间戳转换为基于MYSQL的日期时间。 for C# i tried following code 对于C#我尝试了以下代码

public static DateTime FromUnixTime(long unixTime)
{
    return epoch.AddSeconds(unixTime);
}

taken from here i tried all available solutions on that link but they did not worked in my case. 这里获取,我尝试了该链接上的所有可用解决方案,但在我的情况下它们没有起作用。

for PHP i tried the following code taken from here 对于PHP,我尝试了下面的代码从这里获取

echo date("Y-m-d H:i:s", substr($epoch, 0, 10));

but it converts correct if the timestamp is same as mentioned in example but it returns wrong year when executed with my epoch timestamp. 但是如果时间戳与示例中提到的时间戳相同,则转换正确,但使用我的纪元时间戳执行时返回错误的年份。

i even tried to solve this problem at MYSQL query level so i searched and tried the following solution taken from here 我甚至试过在MySQL查询级别来解决这个问题,所以我搜查,试图从采取了以下解决方案在这里

select from_unixtime(floor(1389422614485/1000));

it does work when i don't replace the example epoch timestamp but when i put my own it did not work 当我不替换示例时代时间戳时,它确实起作用,但是当我放自己的时间戳时,它不起作用

kindly help me to get rid of this strange irritating problem does not matter at which layer you provide solution all are acceptable following languages are preferred 请帮助我摆脱这个奇怪的烦人的问题,无论您在哪一层提供解决方案都可以,以下语言是首选

  1. C# C#
  2. PHP PHP
  3. MYSQL Query MYSQL查询

example epoch timestamp is foloowing 示例时代时间戳记正在消失

13209562668824233

i do know that with respect to length its not same as in examples but note that chrome does convert this efficiently. 我确实知道,就长度而言,它与示例中的不一样,但是请注意,chrome确实可以有效地进行转换。

A solution for PHP (64 Bit, without Milliseconds) is PHP(64位,无毫秒)的解决方案是

$ts = 13209562668824233;

$date = date_create("1601-1-1 UTC")
  ->modify((int)($ts/1000000)." Seconds")
  ->format('Y-m-d H:i:s')
;  //"2019-08-06 10:57:48"

For more details see: What is the format of Chrome's timestamps? 有关更多详细信息,请参阅: Chrome时间戳的格式是什么?

As explained by this answer , the Chrome timestamp is not equal to Unix epoch time. 如此答案所解释,Chrome时间戳记不等于Unix纪元时间。 This is why you're not getting the results you expect from such methods. 这就是为什么您无法从此类方法获得预期结果的原因。 It's actually microseconds since the 1st Jan, 1601 (as opposed to Unix epoch time's seconds since 1st Jan, 1970). 实际上,这是自1601年1月1日以来的微秒(与1970年1月1日以来的Unix纪元的秒数​​相反)。

You can test your WebKit timestamp here , where you'll see it returns Tuesday, 6 August 2019 10:57:48 (UTC). 您可以在此处测试WebKit时间戳,在那里您将看到它返回2019年8月6日星期二10:57:48(UTC)。

So to convert this in code, we should first subtract the difference between 1970 and 1601 (in microseconds) and then divde the value by 1 million to get seconds (C# solution): 因此,要在代码中进行转换,我们应该首先减去1970与1601之间的差(以微秒为单位),然后将该值除以100万以得到秒(C#解决方案):

public static DateTime ConvertWebKitTime(long webkitEpoch)
{
    const long epochDifferenceMicroseconds = 11644473600000000; // difference in microseconds between 1601 and 1970
    var epoch = (webkitEpoch - epochDifferenceMicroseconds) / 1000000; // adjust to seconds since 1st Jan 1970
    return DateTimeOffset.FromUnixTimeSeconds(epoch).UtcDateTime; // convert to datetime
}

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

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