From 441a67e09b0c222aeb9d5e911215de23a5abc749 Mon Sep 17 00:00:00 2001 From: cv <34649812+cpvalente@users.noreply.github.com> Date: Thu, 10 Jun 2021 22:46:26 +0200 Subject: [PATCH] timer has formatted timetag --- server/src/classes/Timer.js | 9 ++++++++- server/src/utils/time.js | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 server/src/utils/time.js diff --git a/server/src/classes/Timer.js b/server/src/classes/Timer.js index 29a5ca793..5bae78b0d 100644 --- a/server/src/classes/Timer.js +++ b/server/src/classes/Timer.js @@ -4,10 +4,13 @@ * */ +import { stringFromMillis } from '../utils/time.js'; + export class Timer { clock = null; duration = null; current = null; + timeTag = null; secondaryTimer = null; _finishAt = null; _finishedAt = null; @@ -133,8 +136,12 @@ export class Timer { // getObject getObject() { + // update timer this.update(); + // update timetag + this.timeTag = stringFromMillis(this.current); + return { clock: this.clock, running: Timer.toSeconds(this.current), @@ -216,4 +223,4 @@ export class Timer { this._finishedAt = null; } } -} \ No newline at end of file +} diff --git a/server/src/utils/time.js b/server/src/utils/time.js new file mode 100644 index 000000000..106423a3e --- /dev/null +++ b/server/src/utils/time.js @@ -0,0 +1,18 @@ +export const stringFromMillis = ( + ms, + showSeconds = true, + delim = ':', + ifNull = '...' +) => { + if (ms === null || isNaN(ms)) return ifNull; + const showWith0 = (value) => (value < 10 ? `0${value}` : value); + const hours = showWith0(Math.floor(((ms / (1000 * 60 * 60)) % 60) % 24)); + const minutes = showWith0(Math.floor((ms / (1000 * 60)) % 60)); + const seconds = showWith0(Math.floor((ms / 1000) % 60)); + + return showSeconds + ? `${ + parseInt(hours) ? `${hours}${delim}` : `00${delim}` + }${minutes}${delim}${seconds}` + : `${parseInt(hours) ? `${hours}` : '00'}${delim}${minutes}`; +};