Skip to content

Commit

Permalink
Fix newDate() and Now() reverted back the date format
Browse files Browse the repository at this point in the history
newDate() for Safari (et. al.)
Supported formats:
"2022.01.10 04:10",
"2022.01.10 04:10:11",
"2022.01.10 04:10:11.123",
"2022-01-10 04:10",
"2022-01-10 04:10:11",
"2022-01-10 04:10:11.123",
"2022.1.10",
"2022-1-1"
...

Fix for NOW() in Firefox: reverted back the date format: 2022-11-01 -> 2022.11.01
  • Loading branch information
alsundukov committed Dec 12, 2022
1 parent f54a551 commit 82f66ed
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/17alasql.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ alasql.options.logtarget = 'output'; // target for log. Values: 'console', 'outp
alasql.options.logprompt = true; // Print SQL at log

alasql.options.progress = false; // Callback for async queries progress
alasql.options.nowdateseparator = '-'; // Separator for Now() function

// Default modifier
// values: RECORDSET, VALUE, ROW, COLUMN, MATRIX, TEXTSTRING, INDEX
Expand Down
30 changes: 22 additions & 8 deletions src/61date.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ stdfn.ASCII = function (a) {
return a.charCodeAt(0);
};

/**
/**
Return first non-null argument
See https://msdn.microsoft.com/en-us/library/ms190349.aspx
*/
Expand Down Expand Up @@ -49,11 +49,12 @@ stdfn.DATE = function (d) {

stdfn.NOW = function () {
var d = new Date();
var separator = alasql.options.nowdateseparator;
var s =
d.getFullYear() +
'-' +
separator +
('0' + (d.getMonth() + 1)).substr(-2) +
'-' +
separator +
('0' + d.getDate()).substr(-2);
s +=
' ' +
Expand Down Expand Up @@ -166,12 +167,25 @@ alasql.stdfn.DATE_SUB = alasql.stdfn.SUBDATE = function (d, interval) {
return new Date(nd);
};

var dateRegexp = /^\d{4}\.\d{2}\.\d{2} \d{2}:\d{2}:\d{2}/;
var dateRegexp =
/^(?<year>\d{4})[-\.](?<month>\d{1,2})[-\.](?<day>\d{1,2})( (?<hours>\d{2}):(?<minutes>\d{2})(:(?<seconds>\d{2})(\.(?<milliseconds>)\d{3})?)?)?/;
function newDate(d) {
if (typeof d === 'string') {
if (dateRegexp.test(d)) {
d = d.replace('.', '-').replace('.', '-');
let date = new Date(d);

if (isNaN(date)) {
if (typeof d === 'string') {
const match = d.match(dateRegexp);
if (match) {
const {year, month, day, hours, minutes, seconds, milliseconds} = match.groups;
const dateArrguments = [year, month - 1, day];
if (hours) {
dateArrguments.push(hours, minutes, seconds || 0, milliseconds || 0);
}

date = new Date(...dateArrguments);
}
}
}
return new Date(d);

return date;
}
18 changes: 18 additions & 0 deletions test/test845.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,22 @@ describe('Test ' + test + ' - use NOW() function', function () {
//02/25/22
assert(/\d{2}\/\d{2}\/\d{2}/.test(res[0].conv));
});

it('3. NOW() with point', function () {
var currentSeparator = alasql.options.nowdateseparator;
alasql.options.nowdateseparator = '.';
var res = alasql('SELECT NOW() AS now');
//2022.02.25 19:21:27.839
assert(/\d{4}.\d{2}.\d{2} \d{2}:\d{2}:\d{2}.\d{3}/.test(res[0].now));
alasql.options.nowdateseparator = currentSeparator;
});

it('4. CONVERT with NOW() with point as an argument', function () {
var currentSeparator = alasql.options.nowdateseparator;
alasql.options.nowdateseparator = '.';
var res = alasql('SELECT CONVERT(STRING,NOW(),1) AS conv');
//02/25/22
assert(/\d{2}\/\d{2}\/\d{2}/.test(res[0].conv));
alasql.options.nowdateseparator = currentSeparator;
});
});

0 comments on commit 82f66ed

Please sign in to comment.