UNPKG

@sports-alliance/sports-lib

Version:

A Library to for importing / exporting and processing GPX, TCX, FIT and JSON files from services such as Strava, Movescount, Garmin, Polar etc

68 lines (67 loc) 2.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DataDuration = void 0; const data_number_1 = require("./data.number"); class DataDuration extends data_number_1.DataNumber { /** * Converts to hhh:mmm:ss:ms * @todo should adopt and round depending if needed to show seconds or not * @param showDays * @param showSeconds * @param showMilliseconds * @param useColonFormat when true, returns compact colon-separated format e.g. `1:04:32` */ getDisplayValue(showDays = false, showSeconds = true, showMilliseconds = false, useColonFormat = false) { const seconds = this.getValue(); const h = Math.floor(seconds / 3600); const d = Math.floor(h / 24); const m = Math.floor((seconds % 3600) / 60); const s = Math.floor((seconds % 3600) % 60); const ms = (Math.round((seconds % 1) * 10) / 10).toString().substring(1); if (useColonFormat) { const msStr = showMilliseconds ? ms : ''; const ss = ('0' + s).slice(-2) + msStr; const mm = ('0' + m).slice(-2); return h > 0 ? `${h}:${mm}:${ss}` : `${mm}:${ss}`; } if (!m && !h) { return showSeconds ? ('0' + s).slice(-2) + (showMilliseconds ? ms : '') + 's' : s + String(showMilliseconds ? ms : '') + `s`; } else if (!h) { return (('0' + m).slice(-2) + 'm' + (showSeconds ? ' ' + ('0' + s).slice(-2) + (showMilliseconds ? ms : '') + 's' : ``)); } else { if (d) { if (showDays) { return (d + 'd ' + ('0' + (h - d * 24)).slice(-2) + 'h ' + ('0' + m).slice(-2) + 'm ' + (showSeconds ? ('0' + s).slice(-2) + (showMilliseconds ? ms : '') + 's' : ``)); } return (h + 'h ' + ('0' + m).slice(-2) + 'm ' + (showSeconds ? ('0' + s).slice(-2) + (showMilliseconds ? ms : '') + 's' : ``)); } else { return (('0' + h).slice(-2) + 'h ' + ('0' + m).slice(-2) + 'm ' + (showSeconds ? ('0' + s).slice(-2) + (showMilliseconds ? ms : '') + 's' : ``)); } } } getDisplayUnit() { return ''; } } exports.DataDuration = DataDuration; DataDuration.type = 'Duration'; DataDuration.unit = 's';