DocSpace-buildtools/packages/asc-web-common/components/MediaViewer/sub-components/duration.js

25 lines
529 B
JavaScript
Raw Normal View History

import React from "react";
2020-04-19 20:06:17 +00:00
2020-04-30 07:35:02 +00:00
export default function Duration({ className, seconds }) {
2020-04-19 20:06:17 +00:00
return (
<time dateTime={`P${Math.round(seconds)}S`} className={className}>
{format(seconds)}
</time>
);
2020-04-19 20:06:17 +00:00
}
2020-04-30 07:35:02 +00:00
function format(seconds) {
const date = new Date(seconds * 1000);
const hh = date.getUTCHours();
const mm = date.getUTCMinutes();
const ss = pad(date.getUTCSeconds());
2020-04-19 20:06:17 +00:00
if (hh) {
return `${hh}:${pad(mm)}:${ss}`;
2020-04-19 20:06:17 +00:00
}
return `${mm}:${ss}`;
2020-04-19 20:06:17 +00:00
}
2020-04-30 07:35:02 +00:00
function pad(string) {
return ("0" + string).slice(-2);
}