From 53f3e2317ec6dcb5f6ca6af1236ecc28ee417278 Mon Sep 17 00:00:00 2001 From: Sarah Chintomby Date: Mon, 17 Jun 2019 10:03:10 -0400 Subject: [PATCH 1/5] [DEV-5546] Fix calc relativedate for day and also month concerning edge cases --- src/calc/Calc.js | 10 +++++- test/calc/CalcTest.js | 72 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 test/calc/CalcTest.js diff --git a/src/calc/Calc.js b/src/calc/Calc.js index 4e637da..65d228b 100644 --- a/src/calc/Calc.js +++ b/src/calc/Calc.js @@ -274,7 +274,7 @@ exports.relativeDate = function( data, value ) // days case 'd': - date_new.setDate( date_new.getUTCDate() + +tval ); + date_new.setDate( date_new.getDate() + +tval ); break; // seconds @@ -286,6 +286,14 @@ exports.relativeDate = function( data, value ) return ''; } + // when adding a month to the last day of the month + // make sure the new date is also the last day of the month + // and not the first day of the next + if( type === 'm' && date_new.getDate() !== now_day ) + { + date_new.setDate(0); + } + // return in the YYYY-MM-DD format, since that's what our fields are // formatted as return date_new.getFullYear() + '-' diff --git a/test/calc/CalcTest.js b/test/calc/CalcTest.js new file mode 100644 index 0000000..c7338d4 --- /dev/null +++ b/test/calc/CalcTest.js @@ -0,0 +1,72 @@ +/** + * Tests Calc + * + * Copyright (C) 2017 R-T Specialty, LLC. + * + * This file is part of liza. + * + * liza is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +var liza = require( '../..' ), + relativeDate = liza.calc.Calc.relativeDate, + expect = require( 'chai' ).expect; + + +describe( 'relativeDate', function() +{ + //returns a date relative to another date by adding a given value + + it( 'assert 10 days is added, the date\'s month changes', function() + { + expect( relativeDate( ['2019-11-25'], ['10d'] ) ) + .to.have.all.members( ['2019-12-05'] ); + }); + + it( 'assert 10 days is added, the date\'s month and year changes', function() + { + expect( relativeDate( ['2019-12-28'], ['10d'] ) ) + .to.have.all.members( ['2020-01-07'] ); + }); + + it( 'assert edge case 6 months is added, the date does not go into the 7th month', function() + { + expect( relativeDate( ['2019-12-31'], ['6m'] ) ) + .to.have.all.members( ['2020-06-30'] ); + }); + + it( 'assert edge case 3 months is added, the date does not go into the 4th month', function() + { + expect( relativeDate( ['2019-08-31'], ['3m'] ) ) + .to.have.all.members( ['2019-11-30'] ); + }); + + it( 'assert 2 years is added', function() + { + expect( relativeDate( ['2019-12-31'], ['2y'] ) ) + .to.have.all.members( ['2021-12-31'] ); + }); + + it( 'assert edge case february', function() + { + expect( relativeDate( ['2018-12-31'], ['2m'] ) ) + .to.have.all.members( ['2019-02-28'] ); + }); + + it( 'assert ege case february leap year', function() + { + expect( relativeDate( ['2019-12-31'], ['2m'] ) ) + .to.have.all.members( ['2020-02-29'] ); + }); +}); From 8f0b12bd3a0e17a5d3613cd79b1b4c649ac07cb2 Mon Sep 17 00:00:00 2001 From: Sarah Chintomby Date: Mon, 17 Jun 2019 11:05:52 -0400 Subject: [PATCH 2/5] [DEV-5546] Change getdate to getUTCDate --- src/calc/Calc.js | 2 +- test/calc/CalcTest.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/calc/Calc.js b/src/calc/Calc.js index 65d228b..5dee95e 100644 --- a/src/calc/Calc.js +++ b/src/calc/Calc.js @@ -274,7 +274,7 @@ exports.relativeDate = function( data, value ) // days case 'd': - date_new.setDate( date_new.getDate() + +tval ); + date_new.setDate( date_new.getUTCDate() + +tval ); break; // seconds diff --git a/test/calc/CalcTest.js b/test/calc/CalcTest.js index c7338d4..dbec665 100644 --- a/test/calc/CalcTest.js +++ b/test/calc/CalcTest.js @@ -64,7 +64,7 @@ describe( 'relativeDate', function() .to.have.all.members( ['2019-02-28'] ); }); - it( 'assert ege case february leap year', function() + it( 'assert edge case february leap year', function() { expect( relativeDate( ['2019-12-31'], ['2m'] ) ) .to.have.all.members( ['2020-02-29'] ); From 3372e433d70e8c0e1e3ff297762a0554660bcbd6 Mon Sep 17 00:00:00 2001 From: Sarah Chintomby Date: Mon, 17 Jun 2019 13:13:58 -0400 Subject: [PATCH 3/5] [DEV-5546] Update getdate to universal time --- src/calc/Calc.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/calc/Calc.js b/src/calc/Calc.js index 5dee95e..654ea70 100644 --- a/src/calc/Calc.js +++ b/src/calc/Calc.js @@ -289,7 +289,7 @@ exports.relativeDate = function( data, value ) // when adding a month to the last day of the month // make sure the new date is also the last day of the month // and not the first day of the next - if( type === 'm' && date_new.getDate() !== now_day ) + if( type === 'm' && date_new.getUTCDate() !== now_day ) { date_new.setDate(0); } From 716f2b1a32172076a273f00d360221418263ee4d Mon Sep 17 00:00:00 2001 From: Sarah Chintomby Date: Tue, 18 Jun 2019 10:09:18 -0400 Subject: [PATCH 4/5] [DEV-5546] Update to use setUTCDate --- src/calc/Calc.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/calc/Calc.js b/src/calc/Calc.js index 654ea70..a067a51 100644 --- a/src/calc/Calc.js +++ b/src/calc/Calc.js @@ -274,7 +274,7 @@ exports.relativeDate = function( data, value ) // days case 'd': - date_new.setDate( date_new.getUTCDate() + +tval ); + date_new.setUTCDate( date_new.getUTCDate() + +tval ); break; // seconds @@ -291,7 +291,7 @@ exports.relativeDate = function( data, value ) // and not the first day of the next if( type === 'm' && date_new.getUTCDate() !== now_day ) { - date_new.setDate(0); + date_new.setUTCDate(0); } // return in the YYYY-MM-DD format, since that's what our fields are From c55235d7d4bed1068ab3d6f2bad7338ccd4445b6 Mon Sep 17 00:00:00 2001 From: Sarah Chintomby Date: Tue, 18 Jun 2019 10:21:10 -0400 Subject: [PATCH 5/5] [DEV-5546] Move edge case code for month --- src/calc/Calc.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/calc/Calc.js b/src/calc/Calc.js index a067a51..da611ec 100644 --- a/src/calc/Calc.js +++ b/src/calc/Calc.js @@ -270,6 +270,13 @@ exports.relativeDate = function( data, value ) // months case 'm': date_new.setMonth( date_new.getMonth() + +tval ); + // when adding a month to the last day of the month + // make sure the new date is also the last day of the month + // and not the first day of the next + if( date_new.getUTCDate() !== now_day ) + { + date_new.setUTCDate(0); + } break; // days @@ -286,14 +293,6 @@ exports.relativeDate = function( data, value ) return ''; } - // when adding a month to the last day of the month - // make sure the new date is also the last day of the month - // and not the first day of the next - if( type === 'm' && date_new.getUTCDate() !== now_day ) - { - date_new.setUTCDate(0); - } - // return in the YYYY-MM-DD format, since that's what our fields are // formatted as return date_new.getFullYear() + '-'