diff --git a/src/utilities.js b/src/utilities.js index 01b5d25..10e5a2a 100644 --- a/src/utilities.js +++ b/src/utilities.js @@ -119,19 +119,15 @@ export function getDurationAsSeconds(duration: String, durationRegex: RegExp) { const [, years, months, , days, hours, minutes, seconds] = new RegExp( durationRegex).exec(duration) || []; - const now = new Date(); - const anchor = new Date(now); - anchor.setFullYear(anchor.getFullYear() + Number(years || 0)); - anchor.setMonth(anchor.getMonth() + Number(months || 0)); - anchor.setDate(anchor.getDate() + Number(days || 0)); - anchor.setHours(anchor.getHours() + Number(hours || 0)); - anchor.setMinutes(anchor.getMinutes() + Number(minutes || 0)); - anchor.setSeconds(anchor.getSeconds() + Number(seconds || 0)); - if (seconds && countDecimals(String(seconds)) > 0) { - const milliseconds = Number(Number(seconds) % 1).toFixed(6) * 1000.0; - anchor.setMilliseconds(anchor.getMilliseconds() + milliseconds); - } - return ((anchor * 1.0) - now) / 1000.0; + let result = 0.0; + + result += (Number(seconds) * 1.0 || 0.0); + result += (Number(minutes) * 60.0 || 0.0); + result += (Number(hours) * 3600.0 || 0.0); + result += (Number(days) * (60 * 60 * 24.0) || 0.0); + result += (Number(years) * (60 * 60 * 24 * 365.0) || 0.0); + + return result; } /** diff --git a/test/utilities.spec.js b/test/utilities.spec.js index 440a508..8f34538 100644 --- a/test/utilities.spec.js +++ b/test/utilities.spec.js @@ -204,16 +204,6 @@ describe('Utility Tests', () => { ).to.equal(86400); }); - it('P1M returns number of seconds for one month from now', () => { - const now = new Date(); - const oneMonthFromNow = new Date(now); - oneMonthFromNow.setMonth(oneMonthFromNow.getMonth() + 1); - - expect( - Utilities.getDurationAsSeconds('P1M', scorm2004_regex.CMITimespan), - ).to.equal((oneMonthFromNow - now) / 1000.0); - }); - it('P1Y returns number of seconds for one year from now', () => { const now = new Date(); const oneYearFromNow = new Date(now); @@ -244,11 +234,11 @@ describe('Utility Tests', () => { scorm2004_regex.CMITimespan), ).to.equal('PT1H21M0.5S'); }); - it('P1Y364D plus P2DT1H45M52S equals P732DT1H45M52S', () => { + it('P1Y364D plus P2DT1H45M52S equals P731DT1H45M52S', () => { expect( Utilities.addTwoDurations('P1Y364D', 'P2DT1H45M52S', scorm2004_regex.CMITimespan), - ).to.equal('P732DT1H45M52S'); + ).to.equal('P731DT1H45M52S'); }); it('Invalid plus valid equals valid', () => { expect(