2020-05-06 05:05:59 +02:00
|
|
|
// @flow
|
2022-01-03 13:41:00 +01:00
|
|
|
import moment from 'moment';
|
2020-05-06 05:05:59 +02:00
|
|
|
|
|
|
|
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]
|
2022-01-03 13:41:00 +01:00
|
|
|
.map((v) => (v < 10 ? '0' + v : v))
|
2020-05-06 05:05:59 +02:00
|
|
|
.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;
|
|
|
|
}
|
2022-01-03 13:41:00 +01:00
|
|
|
|
|
|
|
// Only intended use of future dates is for claims, in case of scheduled
|
|
|
|
// publishes or livestreams, used in util/formatAriaLabel
|
2022-03-31 09:21:28 +02:00
|
|
|
export function getTimeAgoStr(
|
|
|
|
date: any,
|
|
|
|
showFutureDate?: boolean,
|
|
|
|
genericSecondsString?: boolean,
|
|
|
|
zeroDurationStr: string = 'Just now'
|
|
|
|
) {
|
2022-01-03 13:41:00 +01:00
|
|
|
const suffixList = ['years', 'months', 'days', 'hours', 'minutes', 'seconds'];
|
|
|
|
let duration = 0;
|
|
|
|
let suffix = '';
|
2022-01-04 21:33:55 +01:00
|
|
|
let str = '';
|
2022-01-03 13:41:00 +01:00
|
|
|
|
|
|
|
suffixList.some((s) => {
|
|
|
|
// moment() is very liberal with it's rounding.
|
|
|
|
// Always round down dates for better youtube parity.
|
|
|
|
duration = Math.floor(moment().diff(date, s));
|
|
|
|
suffix = s;
|
|
|
|
|
2022-01-03 21:05:19 +01:00
|
|
|
return duration > 0 || (showFutureDate && duration * -1 > 0);
|
2022-01-03 13:41:00 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// Strip off the ending 's' for the singular suffix
|
2022-01-06 20:09:17 +01:00
|
|
|
if (duration === 1 || (duration === -1 && showFutureDate)) suffix = suffix.replace(/s$/g, '');
|
2022-01-03 13:41:00 +01:00
|
|
|
|
2022-01-03 21:34:00 +01:00
|
|
|
// negative duration === it's a future date from now
|
|
|
|
if (duration < 0 && showFutureDate) {
|
|
|
|
str = suffix === 'seconds' ? 'in a few seconds' : 'in %duration% ' + suffix;
|
2022-01-05 13:03:40 +01:00
|
|
|
duration = duration * -1;
|
2022-01-03 21:34:00 +01:00
|
|
|
} else if (duration <= 0) {
|
2022-03-31 09:21:28 +02:00
|
|
|
str = zeroDurationStr;
|
2022-01-04 21:33:55 +01:00
|
|
|
} else {
|
|
|
|
str = suffix === 'seconds' && genericSecondsString ? 'A few seconds ago' : '%duration% ' + suffix + ' ago';
|
|
|
|
}
|
2022-01-03 13:41:00 +01:00
|
|
|
|
|
|
|
return __(str, { duration });
|
|
|
|
}
|