2020-10-16 12:38:04 +00:00
|
|
|
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-10-16 12:38:04 +00:00
|
|
|
);
|
2020-04-19 20:06:17 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 07:35:02 +00:00
|
|
|
function format(seconds) {
|
2020-10-16 12:38:04 +00:00
|
|
|
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) {
|
2020-10-16 12:38:04 +00:00
|
|
|
return `${hh}:${pad(mm)}:${ss}`;
|
2020-04-19 20:06:17 +00:00
|
|
|
}
|
2020-10-16 12:38:04 +00:00
|
|
|
return `${mm}:${ss}`;
|
2020-04-19 20:06:17 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 07:35:02 +00:00
|
|
|
function pad(string) {
|
2020-10-16 12:38:04 +00:00
|
|
|
return ("0" + string).slice(-2);
|
|
|
|
}
|