简体   繁体   中英

Sqlite epoch datetime conversion

So I'm trying to convert epoch time from a sqlite database. I am using the time column the type is Timeswap.

The value stored is: 1383928265 (11/8/2013 11:31:05 AM) http://www.epochconverter.com/

The SQL:

var sql = "SELECT session_id, xml, strftime('%s',stopped), strftime('%s',time), orig_title FROM processed";

The result: 119360752804800 (10/13/1973 7:45:52 AM)

My converter:

    public static DateTime FromUnixTime(long unixTime)
    {
        var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        return epoch.AddSeconds(unixTime).ToLocalTime();
    }

I made some adjustments to the converter:

    public static DateTime FromUnixTime(long unixTime)
    {
        var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        var s = unixTime / 86400;
        return epoch.AddSeconds(s).ToLocalTime();
    }

But it's yielding: 10/11/2013 6:34:37 AM

I cannot get the correct date from the sqlite database.

EDIT: schema

SQLite模式

You are using strftime wrong. A number is interpreted as a Julian date unless you use the unixepoch modifier:

> SELECT strftime('%s', 1383928265), datetime(1383928265);
119360535336000  3784354-44914542-14 12:00:00
> SELECT strftime('%s', 1383928265, 'unixepoch'), datetime(1383928265, 'unixepoch');
1383928265       2013-11-08 16:31:05

However, the value in the database already has the exact format that you want, so you do not need to use strftime in the first place:

SELECT session_id, xml, stopped, time, orig_title FROM processed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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