Making sure seconds are padded when they add up to zero

This commit is contained in:
Jonathan Putney
2020-01-02 13:17:31 -05:00
parent 4e660322bc
commit 9823220be7
2 changed files with 15 additions and 8 deletions

View File

@@ -28,14 +28,21 @@ export function getSecondsAsHHMMSS(totalSeconds: Number) {
const dateObj = new Date(totalSeconds * 1000); const dateObj = new Date(totalSeconds * 1000);
const minutes = dateObj.getUTCMinutes(); const minutes = dateObj.getUTCMinutes();
// make sure we add any possible decimal value // make sure we add any possible decimal value
let seconds = (dateObj.getSeconds() + (totalSeconds % 1.0)); const seconds = dateObj.getSeconds();
if (countDecimals(seconds) > 2) { const ms = totalSeconds % 1.0;
seconds = seconds.toFixed(2); let msStr = '';
if (countDecimals(ms) > 0) {
if (countDecimals(ms) > 2) {
msStr = ms.toFixed(2);
} else {
msStr = String(ms);
}
msStr = '.' + msStr.split('.')[1];
} }
return hours.toString().padStart(2, '0') + ':' + return hours.toString().padStart(2, '0') + ':' +
minutes.toString().padStart(2, '0') + ':' + minutes.toString().padStart(2, '0') + ':' +
seconds.toString().padStart(2, '0'); seconds.toString().padStart(2, '0') + msStr;
} }
/** /**

View File

@@ -224,9 +224,9 @@ describe('Utility Tests', () => {
describe('addTwoDurations()', () => { describe('addTwoDurations()', () => {
it('P1H5M30.5S plus PT15M10S equals P1H20M40.5S', () => { it('P1H5M30.5S plus PT15M10S equals P1H20M40.5S', () => {
expect( expect(
Utilities.addTwoDurations('PT1H5M30.5S', 'PT15M10S', Utilities.addTwoDurations('PT1H5M30.5S', 'PT15M30S',
scorm2004_regex.CMITimespan), scorm2004_regex.CMITimespan),
).to.equal('PT1H20M40.5S'); ).to.equal('PT1H21M0.5S');
}); });
it('P1Y364D plus P2DT1H45M52S equals P732DT1H45M52S', () => { it('P1Y364D plus P2DT1H45M52S equals P732DT1H45M52S', () => {
expect( expect(
@@ -251,9 +251,9 @@ describe('Utility Tests', () => {
describe('addHHMMSSTimeStrings()', () => { describe('addHHMMSSTimeStrings()', () => {
it('01:05:30.5 plus 00:15:10 equals 01:20:40.5', () => { it('01:05:30.5 plus 00:15:10 equals 01:20:40.5', () => {
expect( expect(
Utilities.addHHMMSSTimeStrings('01:05:30.5', '00:15:10', Utilities.addHHMMSSTimeStrings('01:05:30.5', '00:15:30',
scorm12_regex.CMITimespan), scorm12_regex.CMITimespan),
).to.equal('01:20:40.5'); ).to.equal('01:21:00.5');
}); });
it('17496:00:00 plus 49:35:52 equals 17545:35:52', () => { it('17496:00:00 plus 49:35:52 equals 17545:35:52', () => {
expect( expect(