Skip to content

Commit

Permalink
Add date function to get current date without time component (#1937)
Browse files Browse the repository at this point in the history
* #1936 Add date function to get current date without time component
- Add CURDATE method to AlaSQL
- Add test

* #1936 Add date function to get current date without time component
- Support dateAsString option
- Extend tests

* #1936 Add date function to get current date without time component
- Add CURRENT_DATE alias
- Extend test

* - Fix running the parser in package.json
- Add rebuild parser documentation
- Add CURDATE/CURRENT_DATE to parser definition
- Allow both with and without parenthesis
- Adjust test

* - Use tabs

* - Use alias to prevent duplicate code for CURRENT_DATE

* - Update dependencies
  • Loading branch information
paulrutter authored and mathiasrw committed Jul 8, 2024
1 parent c470d97 commit 53ad87c
Show file tree
Hide file tree
Showing 6 changed files with 668 additions and 574 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,15 @@ AlaSQL is an [OPEN Open Source Project](http://openopensource.org/). This means
We appreciate any and all contributions we can get. If you feel like contributing, have a look at [CONTRIBUTING.md](https://github.com/alasql/alasql/blob/develop/CONTRIBUTING.md)
## Rebuilding the parser
To rebuild the parser, follow these steps:
* Make changes to alasqlparser.jison
* `npm install -g jison`
* `npm run jison`
* `npm test` to validate the changes made
* Commit changes to alasqlparser.jison and alasqlparser.js
## Credits
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
"scripts": {
"test": "sh build.sh && yarn test-only",
"test-ci": "(yarn test-format || 1) && yarn test && yarn install-g && alasql 'select 1 as Succes'",
"test-only": "mocha ./test --reporter dot # npx bun node_modules/.bin/mocha ./test --reporter dot",
"test-only": "node node_modules/mocha/bin/mocha.js ./test --reporter dot",
"#test-only": "(command -v bun && bun node_modules/.bin/mocha ./test --reporter dot) || npx bun node_modules/.bin/mocha ./test --reporter dot",
"test-browser": "node test/browserTestRunner.js 7387",
"test-cover": "# istanbul cover -x 'lib/zt/zt.js' --dir test/coverage _mocha",
"build": "yarn format && yarn build-only",
"build-only": "sh build.sh",
"install-g": "yarn build && npm uninstall alasql -g && npm install -g .",
"release": "yarn version",
"jison": "npx jison-gho ./src/alasqlparser.jison -o ./src/alasqlparser.js",
"jison": "jison ./src/alasqlparser.jison -o ./src/alasqlparser.js",
"fmt": "yarn pretty-commit --write",
"format": "yarn pretty-since-dev --write",
"format-all": "yarn pretty-all --write",
Expand Down Expand Up @@ -62,7 +62,7 @@
"eslint-plugin-promise": "6.1.1",
"git-branch-is": "4.0.0",
"husky": "8.0.3",
"jison-gho": "^0.6.1-216",
"jison": "^0.4.18",
"lint-staged": "15.2.2",
"mocha": "10.4.0",
"mocha.parallel": "0.15.6",
Expand All @@ -72,7 +72,7 @@
"react-native-fs": "^2.20.0",
"rexreplace": "7.1.3",
"strftime": "0.10.2",
"tabletop": "1.6.3",
"tabletop": "^1.6.2",
"uglify-js": "3.17.4"
},
"resolutions": {
Expand Down
19 changes: 19 additions & 0 deletions src/61date.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,25 @@ stdfn.NOW = function () {
stdfn.GETDATE = stdfn.NOW;
stdfn.CURRENT_TIMESTAMP = stdfn.NOW;

/**
* Returns the current date, without time component.
* @returns date object without time component
*/
stdfn.CURDATE = stdfn.CURRENT_DATE = function () {
var date = new Date();
date.setHours(0, 0, 0, 0);
if (alasql.options.dateAsString) {
var s =
date.getFullYear() +
'-' +
('0' + (date.getMonth() + 1)).substr(-2) +
'-' +
('0' + date.getDate()).substr(-2);
return s;
}
return date;
};

// stdfn.GETDATE = function(){
// var d = new Date();
// var s = d.getFullYear()+"."+("0"+(d.getMonth()+1)).substr(-2)+"."+("0"+d.getDate()).substr(-2);
Expand Down
8 changes: 8 additions & 0 deletions src/alasqlparser.jison
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ COLUMNS return 'COLUMN'
"CROSS" return 'CROSS'
'CUBE' return 'CUBE'
"CURRENT_TIMESTAMP" return 'CURRENT_TIMESTAMP'
"CURRENT_DATE" return 'CURRENT_DATE'
"CURDATE" return 'CURRENT_DATE'
"CURSOR" return 'CURSOR'
DATABASE(S)? return 'DATABASE'
'DATEADD' return 'DATEADD'
Expand Down Expand Up @@ -1259,6 +1261,8 @@ Expression
{$$ = $1}
| CURRENT_TIMESTAMP
{ $$ = new yy.FuncValue({funcid:'CURRENT_TIMESTAMP'});}
| CURRENT_DATE
{ $$ = new yy.FuncValue({funcid:'CURRENT_DATE'});}
/* | USER
{ $$ = new yy.FuncValue({funcid:'USER'});}
*/ ;
Expand Down Expand Up @@ -1313,6 +1317,8 @@ PrimitiveValue
{ $$ = $1; }
| CURRENT_TIMESTAMP
{ $$ = new yy.FuncValue({funcid:'CURRENT_TIMESTAMP'}); }
| CURRENT_DATE
{ $$ = new yy.FuncValue({funcid:'CURRENT_DATE'}); }
/* | USER
{ $$ = new yy.FuncValue({funcid:'USER'}); }
*/ ;
Expand Down Expand Up @@ -1387,6 +1393,8 @@ FuncValue
{ $$ = new yy.FuncValue({ funcid: 'IIF', args:$3 }) }
| REPLACE LPAR ExprList RPAR
{ $$ = new yy.FuncValue({ funcid: 'REPLACE', args:$3 }) }
| CURRENT_DATE LPAR RPAR
{ $$ = new yy.FuncValue({ funcid: $1 }) }
| DATEADD LPAR Literal COMMA Expression COMMA Expression RPAR
{ $$ = new yy.FuncValue({ funcid: 'DATEADD', args:[new yy.StringValue({value:$3}),$5,$7]}) }
| DATEADD LPAR STRING COMMA Expression COMMA Expression RPAR
Expand Down
Loading

0 comments on commit 53ad87c

Please sign in to comment.