You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Thank you for the issue! I will fix this problem with dates in the next version. The reason of problem is that Dates in JS are actually not Dates, but objects, and we can not compare them directly with "<" or ">", but need to convert them before to milliseconds or write special compare function.
In case when there is no column definition, I will check Date-type on the fly and compile the different function code, like:
if(dt instanceof Date) {
... generate right comparisin
}
I think this Date issue can be fixed in the following functions:
This issue will require to require generation of two different types of code: when you predefine columns (SQL style) and when - not (JavaSqript style). The first approach is much faster at execution time, because second will require check datatypes on the fly.
Another idea is to keep all SQL dates in string format, like: "YYYY-MM-DD", but to have separate Date type for JavaScript dates, like JSDATE. It much faster and better from localization point of view, e.g., I am tired to fight with dates like: "Mon Oct 20 2014 03:00:00 GMT+0300 (MSK)" in JavaScript instead simple "2014-10-20"
@agershun does the SQL ISO Date include timezone offset? What about summer/winter time? Or is it just date - not time? Does it account for validating leap years?
I am thinking about just simple date without time zones. For exmple, for our main project (planning and budgeting software - timezones are not important. Even more: I always have to fix dates like this:
Sat Nov 22 2014 23:37:45 GMT+0300 (MSK)
Because, sometimes JS adds 3 hours of difference between Greenwich and local time (in also depends on the computer setup), because "Midnight in Moscow is lunchtime in L.A.", and we have to check all this issues.
So, I think we will simply as an option use simple string in "Japanese" format: YYYY-MM-DD. It easy to sort and compare strings rather than JS Date objects. I think, it is not so hard to rewrite all necessary DATE functions to operate with strings (like DAY(), MONTH(), YEAR()).
Of course, for complex software with time functionality, we will keep "classic" JS Date objects.
@agershun I see what you are saying about SQLite and strings as date type. I was not thinking clearly - but you are completely right - it is a very reasonable approach to store dates as string, and leave it up to the user to manage the data type in what format they like, and use date manipulation functions. It is also easy for functions to be added by the user, so it is easy for dates to be handled in any which way the user pleases. Having seen the light - I would say that converting to JS dates should not be the default at all, but it is a nice feature if enabled.
I had a look at the latest library, and was trying to disable the jsdate (which I expect is alasql.options.jsdate - not alasql.jsdate as you mentioned in this thread) option, but unfortunately can't get it to work...
Seems that the dates are somehow converted to js date format even when I set the option to false, and more strangely, seem to pick up random timezone offsets, and display the wrong date/time.
Yes, sorry, it was not implemented fully yet. I think in couple of days I finish with this issue.
What do you think, which approach is the better?
a) to have two separate datatypes (e.g. ISODATE and JSDATE)
b) or to use general option (like alasql.options.jsdate) to switch between these approaches by default (but with one datatype DATE)
Hi, I finished with it! I simply implement Date (and other JavaScript objects) into SQL.
alasql.fn.Date=Date;alasql('CREATE TABLE orders (orderdate Date)');alasql('INSERT INTO orders VALUES ("2014-01-01")');varres=alasql('SELECT * FROM orders");
// It give a JavaScript datesvarres=alasql('SELECT orderdare->getFullYear() FROM orders');// This is gives an array of years// Here - getFullYear() - is a function of object Date.prototype
Activity
agershun commentedon Nov 8, 2014
Thank you for the issue! I will fix this problem with dates in the next version. The reason of problem is that Dates in JS are actually not Dates, but objects, and we can not compare them directly with "<" or ">", but need to convert them before to milliseconds or write special compare function.
billymoon commentedon Nov 9, 2014
@agershun thanks for feedback. I don't think there is a problem with comparing javascript date objects with
<and>. See example code here...... which outputs ...
If you point me in the right direction, I wouldn't mind having a go at making the fix myself for you to pull.
agershun commentedon Nov 10, 2014
Hi. Thank you for you post. I will check it during next two days.
Now I use the following approach:
for compare function - conversion to integer with "+" like
var dt = new Date();
console.log(+dt);
I already uses this approach in compileOrder() function (https://github.com/agershun/alasql/blob/master/src/42compile.js).
I think, I will use function like from the posts at stackoverflow about dates in JS
if(dt instanceof Date) {
... generate right comparisin
}
agershun commentedon Nov 10, 2014
I think I will use .getTime() instead "+", or better .valueof() method to compare any objects except numbers, booleans and strings.
Here are performance tests for dates and for primitive types
agershun commentedon Nov 10, 2014
Another idea is to keep all SQL dates in string format, like: "YYYY-MM-DD", but to have separate Date type for JavaScript dates, like JSDATE. It much faster and better from localization point of view, e.g., I am tired to fight with dates like: "Mon Oct 20 2014 03:00:00 GMT+0300 (MSK)" in JavaScript instead simple "2014-10-20"
agershun commentedon Nov 10, 2014
I fixed a little bit, and now the example works fine for ORDER BY clause. But, unfortunately, the problem is more wider...
agershun commentedon Nov 22, 2014
I want tol use two different approaches:
We added special option: alasql.jsdate to choose proper approach.
billymoon commentedon Nov 22, 2014
@agershun does the SQL ISO Date include timezone offset? What about summer/winter time? Or is it just date - not time? Does it account for validating leap years?
agershun commentedon Nov 22, 2014
I am thinking about just simple date without time zones. For exmple, for our main project (planning and budgeting software - timezones are not important. Even more: I always have to fix dates like this:
Sat Nov 22 2014 23:37:45 GMT+0300 (MSK)
Because, sometimes JS adds 3 hours of difference between Greenwich and local time (in also depends on the computer setup), because "Midnight in Moscow is lunchtime in L.A.", and we have to check all this issues.
So, I think we will simply as an option use simple string in "Japanese" format: YYYY-MM-DD. It easy to sort and compare strings rather than JS Date objects. I think, it is not so hard to rewrite all necessary DATE functions to operate with strings (like DAY(), MONTH(), YEAR()).
Of course, for complex software with time functionality, we will keep "classic" JS Date objects.
I think this is acceptable, because SQL.js does exactly the same thing:
http://kripken.github.io/sql.js/GUI/
billymoon commentedon Nov 22, 2014
@agershun I see what you are saying about SQLite and strings as date type. I was not thinking clearly - but you are completely right - it is a very reasonable approach to store dates as string, and leave it up to the user to manage the data type in what format they like, and use date manipulation functions. It is also easy for functions to be added by the user, so it is easy for dates to be handled in any which way the user pleases. Having seen the light - I would say that converting to JS dates should not be the default at all, but it is a nice feature if enabled.
I had a look at the latest library, and was trying to disable the jsdate (which I expect is
alasql.options.jsdate- notalasql.jsdateas you mentioned in this thread) option, but unfortunately can't get it to work...http://jsfiddle.net/Lvemnqb3/
Seems that the dates are somehow converted to js date format even when I set the option to false, and more strangely, seem to pick up random timezone offsets, and display the wrong date/time.
agershun commentedon Nov 23, 2014
Yes, sorry, it was not implemented fully yet. I think in couple of days I finish with this issue.
What do you think, which approach is the better?
a) to have two separate datatypes (e.g. ISODATE and JSDATE)
b) or to use general option (like alasql.options.jsdate) to switch between these approaches by default (but with one datatype DATE)
agershun commentedon Nov 24, 2014
@billymoon HI,
I have made a test in version 0.0.25 to try the approach with alasql.options.datetimeformat:
https://github.com/agershun/alasql/blob/version-0.0.25/test/test131.js
All other datetime functionality will be chnged in the similar way.
agershun commentedon Nov 26, 2014
Hi, I finished with it! I simply implement Date (and other JavaScript objects) into SQL.
Check for more examples:
https://github.com/agershun/alasql/blob/version-0.0.26/test/test140.js
If you want ot use good old SQL date just use DATE, instead Date:
agershun commentedon Nov 27, 2014
I updated the first sample in Fiddle - now it works!