Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix newDate() and option for reverted back Now() date format #1534

Merged
merged 6 commits into from
Jun 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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})?)?)?/;
mathiasrw marked this conversation as resolved.
Show resolved Hide resolved
mathiasrw marked this conversation as resolved.
Show resolved Hide resolved
function newDate(d) {
if (typeof d === 'string') {
if (dateRegexp.test(d)) {
d = d.replace('.', '-').replace('.', '-');
let date = new Date(d);

if (isNaN(date)) {
mathiasrw marked this conversation as resolved.
Show resolved Hide resolved
if (typeof d === 'string') {
mathiasrw marked this conversation as resolved.
Show resolved Hide resolved
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;
});
});
mathiasrw marked this conversation as resolved.
Show resolved Hide resolved