Change the timer data structure to fix network bugs and refactored shared timer functions

This commit is contained in:
Mitchell McCaffrey
2020-08-05 12:01:54 +10:00
parent 0c1ec22234
commit 4199d7ab6a
5 changed files with 83 additions and 51 deletions
+33
View File
@@ -0,0 +1,33 @@
const MILLISECONDS_IN_HOUR = 3600000;
const MILLISECONDS_IN_MINUTE = 60000;
const MILLISECONDS_IN_SECOND = 1000;
/**
* Returns a timers duration in milliseconds
* @param {Object} t The object with an hour, minute and second property
*/
export function getHMSDuration(t) {
if (!t) {
return 0;
}
return (
t.hour * MILLISECONDS_IN_HOUR +
t.minute * MILLISECONDS_IN_MINUTE +
t.second * MILLISECONDS_IN_SECOND
);
}
/**
* Returns an object with an hour, minute and second property
* @param {number} duration The duration in milliseconds
*/
export function getDurationHMS(duration) {
let workingDuration = duration;
const hour = Math.floor(workingDuration / MILLISECONDS_IN_HOUR);
workingDuration -= hour * MILLISECONDS_IN_HOUR;
const minute = Math.floor(workingDuration / MILLISECONDS_IN_MINUTE);
workingDuration -= minute * MILLISECONDS_IN_MINUTE;
const second = Math.floor(workingDuration / MILLISECONDS_IN_SECOND);
return { hour, minute, second };
}