Jump to content

[Solved] SQLite convert TimeStamps to date and time


Kyan
 Share

Recommended Posts

Hi here

I been messing around with timestamps in SQL databases, my problem is how do I convert this 40660.9454658044 timestamp to human readable date & time?

I found out this excellent website http://www.epochconverter.com/#tools but this timestamp is in another format, in SQL I tried DATEADD(s, '40660.9454658044', '1970-01-01 00:00:00') but no success .

Here i found a TIMESTAMP() function http://www.w3schools.com/sql/sql_datatypes.asp but doesn't work :(

Anyway to convert this?

EDIT: 40757.6542476852 = 01-08-2011 16:42 = dd-mm-yyyy hh:mm but I don't know how to convert it :(

Edited by DiOgO

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Link to comment
Share on other sites

I don't use SQLLite, but I would say that everything after the decimal point is the percentage of the day that the hours and minutes represented in decimal, and everything prior to the decimal is the number of days since a certain date in time. Like a Julian date.

Fairly easy to work out and write your own conversion function.

Edited by Clark
Link to comment
Share on other sites

This example will convert 40757.6542476852 to 01-08-2011 16:42:00.

It uses a base date of 1899/12/29 00:59:53 in yyy/mm/dd hh:mm:ss format.

You may be able to tweak this example to have it work correctly on other timestamp conversions.

#include <Date.au3>

Local $iTimeStamp = 40757.6542476852 ;= 01-08-2011 16:42  = dd-mm-yyyy hh:mm
Local $iDec = $iTimeStamp - Int($iTimeStamp)
Local $Date = _DateAdd("D", Int($iTimeStamp), "1899/12/29 00:59:53")
Local $DateTime = _DateAdd("s", Int($iDec * 24 * 3600), $Date)
MsgBox(0, "Results", "Timestamp: " & $iTimeStamp & " = " & _
        StringRegExpReplace($DateTime, "(\d{4})/(\d{2})/(\d{2}) (.*)", "\3-\2-\1 \4") & " in dd/mm/yyyy hh:mm:ss")
Link to comment
Share on other sites

Where did you get the 40660.9454658044 timestamp?

I ask because it isn't an exact Julian format difference of any two dates expressed down to thousands of seconds.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

I don't use SQLLite, but I would say that everything after the decimal point is the percentage of the day that the hours and minutes represented in decimal, and everything prior to the decimal is the number of days since a certain date in time. Like a Julian date.

Fairly easy to work out and write your own conversion function.

seems logic and works right, don't know how that percetage part is calculated

This example will convert 40757.6542476852 to 01-08-2011 16:42:00.

It uses a base date of 1899/12/29 00:59:53 in yyy/mm/dd hh:mm:ss format.

You may be able to tweak this example to have it work correctly on other timestamp conversions.

#include <Date.au3>

Local $iTimeStamp = 40757.6542476852 ;= 01-08-2011 16:42 = dd-mm-yyyy hh:mm
Local $iDec = $iTimeStamp - Int($iTimeStamp)
Local $Date = _DateAdd("D", Int($iTimeStamp), "1899/12/29 00:59:53")
Local $DateTime = _DateAdd("s", Int($iDec * 24 * 3600), $Date)
MsgBox(0, "Results", "Timestamp: " & $iTimeStamp & " = " & _
StringRegExpReplace($DateTime, "(\d{4})/(\d{2})/(\d{2}) (.*)", "\3-\2-\1 \4") & " in dd/mm/yyyy hh:mm:ss")

works fine, but the base date seems to be 1899/12/30 :)

this ones passed

40806.7112615741 = 20-09-2011 18:04

40805.9523958333 = 19-09-2011 23:51

Where did you get the 40660.9454658044 timestamp?

I ask because it isn't an exact Julian format difference of any two dates expressed down to thousands of seconds.

my SMS db :), the new db uses the same timestamp as epochconverter.com uses

EDIT: found something http://stackoverflow.com/questions/3963617/why-is-1899-12-30-the-zero-date-in-access-sql-server-instead-of-12-31

Edited by DiOgO

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Link to comment
Share on other sites

Wouldn't you want to use the built-in SQLLite Date() Time() or DateTime() functions and have your query return the values already in the desired format?

but how do I use it, in this way no output from sqlite expert personal....

timestamp: 40806.7213194444

date&time: 20-09-2011 18:18 (relatively to timestamp)

command: SELECT DateTime(Timestamp) FROM table where Text='texttexttext';

output: -4601-08-16 05:18:42

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Link to comment
Share on other sites

What does this get you?

sqlite> SELECT DATETIME(TimeStamp, 'unixepoch');

1970-01-01 11:19:20 ...till...1970-01-01 11:20:06 and dezens of them are equal like 30times 1970-01-01 11:19:20....

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Link to comment
Share on other sites

Unix epoch is a number of seconds since 1970-01-01. Nothing close to a Julian format.

Let me come up with something

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

this date 1899/12/30 00:59:53 wouldn't be 1899/12/30 00:00:00 ? but since I'm in a GMT region that's why 1899/12/30 00:59:53 ~ 1899/12/29 01:00:00

1899/12/29 01:00:00 ->GMT

1899/12/29 00:00:00 ->UTC-13 (GMT-1)? is the julian format in UTC or something? finland is GMT+2

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Link to comment
Share on other sites

Here you are: try those queries to see why and how.

select julianday('1899-12-30 00:00:00'); -- that gives 2415018.5 (remember Julian dates start at noon)

select datetime('40660.9454658044', '+2415018 days', '+12 hours', 'localtime'); -- gets you 2011-04-28 00:41:28 (depending on your local time)

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Here you are: try those queries to see why and how.

select julianday('1899-12-30 00:00:00'); -- that gives 2415018.5 (remember Julian dates start at noon)

select datetime('40660.9454658044', '+2415018 days', '+12 hours', 'localtime'); -- gets you 2011-04-28 00:41:28 (depending on your local time)

awesome :D I test it with 40806.7213194444 and gives me exactly the date and time, thank you

in w3schools there isn't a julianday() and datetime() function :(

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Link to comment
Share on other sites

These are SQLite goodies. For more detail see http://www.sqlite.org/lang_datefunc.html

OTOH Microsoft awfully wrong timestamp convention is ... well ... pure microsoftism.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

These are SQLite goodies. For more detail see http://www.sqlite.org/lang_datefunc.html

OTOH Microsoft awfully wrong timestamp convention is ... well ... pure microsoftism.

allways something hidden :s

at introdution, datetime is described like: datetime(timestring, modifier, modifier, ...), a modifier could be a sum or subtraction to the first parameter 'timestring'? and localtime (in your code) is equivalent to my computer time?

Edited by DiOgO

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Link to comment
Share on other sites

Correct on all points. If 'localtime' is not mentionned, times default to UTC as the doc says. My assumption is that the timestring value you supply is already local time and your machine's settings are correct for your location. Double check by yourself that the statement works the way you need and adjust parameters accordingly if not. Use strftime() function instead if you need more precise control over the format of the output date. Again, the doc is your best friend as always.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Correct on all points. If 'localtime' is not mentionned, times default to UTC as the doc says. My assumption is that the timestring value you supply is already local time and your machine's settings are correct for your location. Double check by yourself that the statement works the way you need and adjust parameters accordingly if not. Use strftime() function instead if you need more precise control over the format of the output date. Again, the doc is your best friend as always.

okey :)

works fine with timelocal setting, seems when they made nokia suite thought in that, with strftime I could get rid of seconds in the output like this: strftime('%d-%m-%Y %H:%M', '40660.9454658044', '+2415018 days', '+12 hours', 'localtime') ?

EDIT: nope

output: 27-04-2011 23:41 (from strftime() command)

espected: 2011-09-20 18:18:42 (from your code and the correct one)

EDIT2: even with the equivalent to datatime function i got the same result :s, 5 months behind of supposed ;S

Edited by DiOgO

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Link to comment
Share on other sites

You must mix your input timestamps.

This query works as intended:

select datetime('40660.9454658044', '+2415018 days', '+12 hours', 'localtime') as "DateTime() default",
strftime('%d-%m-%Y %H:%M', '40660.9454658044', '+2415018 days', '+12 hours', 'localtime') as "with strftime()"
returns
RecNo DateTime() default with strftime()
----- ------------------- -----------------
1 2011-04-28 00:41:28 28-04-2011 00:41

EDIT: note that since I live in France my local time is +1 from UTC

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

You must mix your input timestamps.

This query works as intended:

select datetime('40660.9454658044', '+2415018 days', '+12 hours', 'localtime') as "DateTime() default",
strftime('%d-%m-%Y %H:%M', '40660.9454658044', '+2415018 days', '+12 hours', 'localtime') as "with strftime()"
returns
RecNo DateTime() default with strftime()
----- ------------------- -----------------
1 2011-04-28 00:41:28 28-04-2011 00:41

EDIT: note that since I live in France my local time is +1 from UTC

why do I need to do that?, is the same parameter inputted at strftime() :think: who made up this function what had in mind? :bonk:

btw, the output still wrong the correct should be 2011-09-20 18:18:42 (forget about it, I was using this timestamp for comparison 40806.7213194444)

Edited by DiOgO

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...