Retrieve date from Evernote date_created timestamp field - sqlite

Does anyone know on what basetime Evernote calculates datetime?
I need to directly deal with the notes table in the Evernote SQLite DB and the documentation refers people to the SQLite manual https://www.sqlite.org/lang_datefunc.html
This stored time 736012.8334375 should yield 2016/02/18 21:00
I've tried multiple variants such as
select datetime(((((736012.8334375)*1000/60)/60)/24), 'unixepoch'), datetime(((736012.8334375)), 'unixepoch'), datetime(736012.8334375, 'unixepoch'), strftime('%s', 'now'), strftime('7736012.8334375', 'unixepoch'), datetime((736012.8334375 *1000), 'unixepoch')
giving
"1970-01-01 23:39:46","1970-01-09 12:26:52","1970-01-09 12:26:52","1464341058",,"1993-04-28 16:00:33"
This Excel formula
=((((736012.8334375)*1000/60)/60)/24)+DATE(1970,1,1)+(1/24)
gets closer with
4/28/93 5:00 PM
but still a bit out.
What am I doing wrong?

Here's the formula I arrived at for determining the real date from Evernote's dates:
unixTime = (EvernoteTime * 86400) - 62135683200
I've tested this with a few time zones by exporting data from the Evernote app to html and hand-verifying the times match.
I'm not sure where the 62135683200 comes from. It's not quite the difference between unix epoch and year zero, but after arriving at the correct value I stopped trying to figure it out.

Related

Ms-Access convert datetime to seconds since epoch [duplicate]

I'm trying to write a query for an MS Access 2007 connection to a MySQL database through ODBC. Everything's working fine, and the query does what I want it to do. The part that I'm hung up on is that I'm stuck asking the user for unix epoch time, instead of a regular date.
I looked up a bunch of references for MS Access, and while there are a number of date conversion functions I can use in the SQL call, I can't find any that I can use to convert from a normal date -> unix epoch date.
What I would like, and I assume this works, is to ask the user for the date in a much kinder fashion (a human readable date), and then convert it into unix epoch date. Now that I think about it, I guess my other option is to convert the unix epoch dates in the database after drawing them out with the SQL query, but I'd rather convert the user's input if at all possible as there is less of that input so I wouldn't have to do as much work.
SELECT TOP 5 Count( * ) AS [Number of visits by language], login.lang AS [Language]
FROM login, reservations, reservation_users
WHERE (reservations.start_date Between [Starting unix epoch time] And [Ending unix epoch time]) And reservations.is_blackout=0 And reservation_users.memberid=login.memberid And reservation_users.resid=reservations.resid And reservation_users.invited=0
GROUP BY login.lang
ORDER BY Count( * ) DESC;
Don't know if this is going to work (don't have Access to confirm), but try the suggestion from here:
http://weblogs.sqlteam.com/jeffs/archive/2007/04/13/format-date-sql-server.aspx
which is to use:
DATEDIFF(second, '1 Jan 1970', tbl.LastChangeDate)
This is from some of the comments.
EDIT: See the comment from Remou.
Using MS 2010 I found this works: DateAdd("s",([epoch timestamp]-21600),#1/1/1970#)
http://www.pcreview.co.uk/forums/convert-epoch-date-t2324318.html

SQLite: How to convert a bigint field to date or timestamp?

I've received a SQLite file with .db extension.
Opening it, I have a column with serial numbers: i.e. 1600414704594 (that should correspond to 2020-10-09 and whatsover time)
The db comes from outside and I don't know how that date field has been built.
Could you suggest me a query to get a valid date/time from that db column ?
I've tried reading this post but none of the given solution returned me a valid (and actual) date, please help me.
It looks like an Unix time in milliseconds. SQLite's unixepoch modifier expects it in seconds. The conversion is fairly easy :
SELECT DATETIME(1600414704594 / 1000, 'unixepoch')
2020-09-18 07:38:24

where clause with time columns on sql server

In the project there a functionality that lets you book the place during the day, they have to enter the day, the start time and final time from 7:00 am to 12:00 AM (midnight) or 1:00 AM, for example if someone enter date=21/oct/2011 start time=8:00 pm and end time 12:00 am (he book the place from 8:00 pm until 12:00 am) the webform send to the store procedure 20:00 and 00:00 to check the table to see if is available, if someone already book in the same day until midnight it is store like this
startime=23:00 endtime=00:00
so when i check the new client it has to return that there a reservation already in the range of time,
my query is not efficient but it working from the 7:00 to 23:00 range, it fails when from the webfrom enters a endtime 12 am (00:00 on sql) because the starttime es greater than the end time
this is my query
select COUNT(*)
from table1
where id_place=#id_place
and date=#date
and (
(#start_time=res_start_time and #end_time=res_end_time)
or (#start_time > res_start_time and #start_time < res_end_time)
or (#end_time > res_start_time and res_end_time < res_end_time)
or (res_start_time > #start_time and res_start_time < #end_time)
or (res_end_time > #start_time and res_end_time < #end_time)
or (res_start_time < #start_time and res_end_time >#end_time)
)
-- #start_time = start time of the reservations (from webform)
-- #end_time = end time of the reservations (from webform)
-- res_start_time= represents the start time column
-- res_ebd_time= represents the end time column
i need help on two things, how i solved the issue when i have to check times like 12:00 am or 1:00 am that are already in the table like the example at the begins of the question and to check my query because is think there has to be a better solution to implement this kind of functionality
You obviously have a date somewhere in the table, as well, or you would not be able to book resources. This leads to a couple of possibilities.
Query both time and date (can get complex, but you can make this a udf for reuse)
Use DateTime instead of date and time columns
if in SQL Server, consider creating a CLR function to handle this, as the CLR (.NET code) will more efficiently determine "is in time range"
I am sure there are other possibilities. The key point here is it sounds like your algorithm is failing largely due to events that spill over to another day. If that is correct, bring the day into the equation is your best bet. This can end up as a rather complex SQL statement, a change of data types (datetime instead of date and time) or creating a .NET function (CLR) to help more efficiently determine "is in range".

How can I store the current timestamp in SQLite as ticks?

I have a SQLite database where I store the dates as ticks. I am not using the default ISO8601 format. Let's say I have a table defined as follows:
CREATE TABLE TestDate (LastModifiedTime DATETIME)
Using SQL, I wish to insert the current date and time. If I execute any of the below statements, I end up getting the date and time stored as a string and not in ticks.
INSERT INTO TestDate (LastModifiedTime) VALUES(CURRENT_TIMESTAMP)
INSERT INTO TestDate (LastModifiedTime) VALUES(DateTime('now'))
I have looked at the SQLite documenation, but I do not seem to find any option to obtain the current timestamp in ticks.
I can of course define a parameter in C# and store the value as a System.DateTime. This does result in the datetime getting stored to the database in ticks.
What I would like to do is be able to insert and update the current timestamp directly from within the SQL statement. How would I do this?
Edit:
The reason I want the data stored as ticks in the database, is that the dates are stored in the same format as stored by the ADO.Net data provider, and so that when the data is also queried using the ADO.Net provider it is correctly retrieved as a System.DataTime .Net type.
This particular oddity of SQLite caused me much anguish.
Easy way - store and retrieve as regular timestamp
create table TestDate (
LastModifiedTime datetime
);
insert into TestDate (LastModifiedTime) values (datetime('now'));
select datetime(LastModifiedTime), strftime('%s.%f', LastModifiedTime) from TestDate;
Output: 2011-05-10 21:34:46|1305063286.46.000
Painful way - store and retrieve as a UNIX timestamp
You can use strftime to retrieve the value in ticks. Additionally, to store a UNIX timestamp (roughly equivalent to ticks), you can can surround the number of seconds in single-quotes.
insert into TestDate (LastModifiedTime) values ('1305061354');
SQLite will store this internally as some other value that is not a UNIX timestamp. On retrieval, you need to explicitly tell SQLite to retrieve it as a UNIX timestamp.
select datetime(LastModifiedTime, 'unixepoch') FROM TestDate;
To store the current date and time, use strftime('%s', 'now').
insert into TestDate (LastModifiedTime) VALUES (strftime('%s', 'now'));
Full example:
create table TestDate (
LastModifiedTime datetime
);
insert into TestDate (LastModifiedTime) values (strftime('%s', 'now'));
select datetime(LastModifiedTime, 'unixepoch') from TestDate;
When executed by sqlite3, this script with print:
2011-05-10 21:02:34 (or your current time)
After further study of the SQLite documentation and other information found on date number conversions, I have come up with the following formula, which appears to produce correct results:
INSERT INTO TestDate(LastModifiedTime)
VALUES(CAST((((JulianDay('now', 'localtime') - 2440587.5)*86400.0) + 62135596800) * 10000000 AS BIGINT))
Seems like a painful way to produce something that I would expect to be available as a built-in datetime format, especially that the database supports the storing of datetime values in ticks. Hopefully, this becomes useful for others too.
Update:
The above formula is not perfect when it comes to daylight savings. See section Caveats And Bugs in SQLite docs regarding local time calculation.
The following will return the number of milliseconds since the UNIX Epoch:
SELECT (strftime('%s', 'now') - strftime('%S', 'now') + strftime('%f', 'now')) * 1000 AS ticks
It works by grabbing the number of seconds since the Unix Epoch (%s), subtracting the number of seconds in the current time (%S), adding the number of seconds with decimal places (%f), and multiplying the result by 1000 to convert from seconds to milliseconds.
The subtraction and addition are to add precision to the value without skewing the result. As stated in the SQLite Documentation, all uses of 'now' within the same step will return the same value.

SQLite - Ordering

I have a strange issue where upon selecting information from a SQLite database, ans ordering based upon date, the results returned are invalid.
My SQL statement is as such:
Select pk from usersDates order by datetime(usersDate, 'localtime') ASC
I have dates stored in the database which range as far as 2111. However the order the data is returned in indicates that dates from 2036 happen after the ones from 2111.
The column 'usersDate is actually a double (time interval since 1970 / unix time) - hence the reason for the cast.
Does anyone know what would cause this?
You should re-read the date and time syntax. The 'localtime' modifier expects an UTC time on its left.
Use SELECT pk FROM usersDates ORDER BY datetime(usersDate, 'unixepoch', 'localtime') ASC.

Resources