18 digit timestamp to Local Time

青春壹個敷衍的年華 提交于 2019-12-12 06:37:27

问题


I came across Sq-lite database.I found our date time is stored in 18 digit time-stamp.

Please help me how to convert it to Local Time. (I tried to convert it(No.of milliseconds from 1900). But i did not get it.It shows 29 days difference. I added 29 days of milliseconds to time. But time output is wrong.My assumption is, it will be NTP time.)

Here i give 18-digit time-stamp and its equivalent date time.

Time-Stamp Date-Time

362087070028927400 27-10-2014 15:06:57
362087302762879170 27-10-2014 15:22:06
362087302763135170 27-10-2014 15:22:06
362087302851460030 27-10-2014 15:22:06
362087302851716030 27-10-2014 15:22:06
362087302851716030 27-10-2014 15:22:06
362087305419799000 27-10-2014 15:22:16
362087307972777150 27-10-2014 15:22:26
362087310530875300 27-10-2014 15:22:36
362087313092045760 27-10-2014 15:22:46
362087315652448200 27-10-2014 15:22:56
362087318210802600 27-10-2014 15:23:06
362087320772741060 27-10-2014 15:23:16
362087323333655740 27-10-2014 15:23:26

Thanks


回答1:


It looks to me like it's based at 1970 (the Unix epoch) instead of 1900, with 256000000 "ticks" per second.

I haven't seen such a format before, but it seems to check out - for example, using Noda Time:

using System;
using NodaTime;

class Test
{
    static void Main()
    {
        Console.WriteLine(ToInstant(362087323333655740));
    }

    static Instant ToInstant(long value)
    {
        // We don't have to truncate to milliseconds, but it's simple for now...
        long millis = value / 256000;
        return Instant.FromMillisecondsSinceUnixEpoch(millis);
    }
}

Output:

2014-10-27T09:53:26Z

... which would correspond to 2014-10-27T15:23:26 as the local time in India.




回答2:


Thanks Jon Skeet.

I tried without NodaTime dll.

My code as In Vb.net;

Dim theDate As Date = New Date(1970, 1, 1, 0, 0, 0).AddMilliseconds(CLng(lstUnixTime.Items(k) / 256000L))
Dim offsetAmount As TimeSpan = TimeZone.CurrentTimeZone.GetUtcOffset(New Date(1900, 1, 1, 0, 0, 0))
theDate += offsetAmount

Now I am getting Correct date.

Thanks for your valuable reply and answer.




回答3:


for php

$dateLargeInt= "131424999530821857";
$secsAfterADEpoch = $dateLargeInt / (10000000);

$ADToUnixConvertor=((1970-1601) * 365.242190) * 86400; 
// unix epoch - AD epoch * number of tropical days * seconds in a day 
$unixTsLastLogon=intval($secsAfterADEpoch-$ADToUnixConvertor); 

// unix Timestamp version of AD timestamp
$lastlogon=date("d-m-Y", $unixTsLastLogon); // formatted date

echo $lastlogon;



回答4:


Here's a solution I did in Python that worked well for me (this is for 18-digit LDAP / Active Directory timestamps):

import datetime

def ad_timestamp(timestamp):
    if timestamp != 0:
        return datetime.datetime(1601, 1, 1) + datetime.timedelta(seconds=timestamp/10000000)
    return np.nan

So then if you need to convert a Pandas column:

df.lastLogonTimestamp = df.lastLogonTimestamp.fillna(0).apply(ad_timestamp)

Note: I needed to use fillna before using apply. Also, since I filled with 0's, I checked for that in the conversion function about, if timestamp != 0. Hope that makes sense. It's extra stuff but you may need it to convert the column in question.



来源:https://stackoverflow.com/questions/26729302/18-digit-timestamp-to-local-time

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