From bcf7332675adf445aa74c1da4d3fed012edfe3f8 Mon Sep 17 00:00:00 2001 From: bgildson Date: Thu, 29 Oct 2020 08:46:54 -0300 Subject: [PATCH] changed duration format to also represent days --- webroot/js/app.js | 4 ++-- webroot/js/utils/helpers.js | 19 +++++++++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/webroot/js/app.js b/webroot/js/app.js index b238f088b..681a56f5e 100644 --- a/webroot/js/app.js +++ b/webroot/js/app.js @@ -8,7 +8,7 @@ import UsernameForm from './components/chat/username.js'; import VideoPoster from './components/video-poster.js'; import Chat from './components/chat/chat.js'; import Websocket from './utils/websocket.js'; -import { secondsToHMMSS, hasTouchScreen, getOrientation } from './utils/helpers.js'; +import { parseSecondsToDurationString, hasTouchScreen, getOrientation } from './utils/helpers.js'; import { addNewlines, @@ -269,7 +269,7 @@ export default class App extends Component { let streamDurationString = ''; if (this.state.lastConnectTime) { const diff = (Date.now() - Date.parse(this.state.lastConnectTime)) / 1000; - streamDurationString = secondsToHMMSS(diff); + streamDurationString = parseSecondsToDurationString(diff); } this.setState({ streamStatusMessage: `${MESSAGE_ONLINE} ${streamDurationString}`, diff --git a/webroot/js/utils/helpers.js b/webroot/js/utils/helpers.js index d055284aa..aab4f856c 100644 --- a/webroot/js/utils/helpers.js +++ b/webroot/js/utils/helpers.js @@ -96,19 +96,26 @@ export function generateUsername() { return `User ${(Math.floor(Math.random() * 42) + 1)}`; } -export function secondsToHMMSS(seconds = 0) { +export function padLeft(text, pad, size) { + return String(pad.repeat(size) + text).slice(-size); +} + +export function parseSecondsToDurationString(seconds = 0) { const finiteSeconds = Number.isFinite(+seconds) ? Math.abs(seconds) : 0; - const hours = Math.floor(finiteSeconds / 3600); - const hoursString = hours ? `${hours}:` : ''; + const days = Math.floor(finiteSeconds / 86400); + const daysString = days > 0 ? `${days} day${days > 1 ? 's' : ''} ` : ''; + + const hours = Math.floor((finiteSeconds / 3600) % 24); + const hoursString = hours || days ? padLeft(`${hours}:`, '0', 3) : ''; const mins = Math.floor((finiteSeconds / 60) % 60); - const minString = mins < 10 ? `0${mins}:` : `${mins}:`; + const minString = padLeft(`${mins}:`, '0', 3); const secs = Math.floor(finiteSeconds % 60); - const secsString = secs < 10 ? `0${secs}` : `${secs}`; + const secsString = padLeft(`${secs}`, '0', 2); - return hoursString + minString + secsString; + return daysString + hoursString + minString + secsString; } export function setVHvar() {