lbry-desktop/ui/util/time.js

35 lines
773 B
JavaScript
Raw Permalink Normal View History

// @flow
export function secondsToHms(seconds: number) {
seconds = Math.floor(seconds);
var hours = Math.floor(seconds / 3600);
var minutes = Math.floor(seconds / 60) % 60;
var seconds = seconds % 60;
return [hours, minutes, seconds]
.map(v => (v < 10 ? '0' + v : v))
.filter((v, i) => v !== '00' || i > 0)
.join(':');
}
export function hmsToSeconds(str: string) {
let timeParts = str.split(':'),
seconds = 0,
multiplier = 1;
if (timeParts.length > 0) {
while (timeParts.length > 0) {
let nextPart = parseInt(timeParts.pop(), 10);
if (!Number.isInteger(nextPart)) {
nextPart = 0;
}
seconds += multiplier * nextPart;
multiplier *= 60;
}
} else {
seconds = 0;
}
return seconds;
}