import { isString } from 'lodash';
import moment from 'moment';
import { SECOND_IN_TIME_UNIT } from 'utils/constants';
export const timeFormatStr = {
YMDTHms: 'YYYY-MM-DDTHH:mm:ss',
YMDHms: 'YYYY-MM-DD HH:mm:ss',
YMDHm: 'YYYY-MM-DD HH:mm',
YMD: 'YYYY-MM-DD',
MDHm: 'MM-DD HH:mm',
};
export const getLocalTime = (time) => moment.utc(time).local();
export const getLocalTimeStr = (time, formatter = timeFormatStr.YMDHms) => {
const realFormatter = isString(formatter) ? formatter : timeFormatStr.YMDHms;
return getLocalTime(time).format(realFormatter);
};
export const getTimestamp = (time) => getLocalTime(time).unix();
export const getStrFromTimestamp = (stamp, formatter = timeFormatStr.YMDHms) =>
moment.unix(Number(stamp)).format(formatter);
export const checkTimeIn = (inputTime, start, end) => {
if (!inputTime) {
return false;
}
const inputT = getLocalTime(inputTime);
const startT = getLocalTime(start);
const endT = getLocalTime(end);
if (!start) {
return inputT <= endT;
}
if (!end) {
return inputT >= startT;
}
return inputT.isAfter(startT) && inputT.isBefore(endT);
};
export const getSinceTime = (input) => {
if (!input) {
return '-';
}
return getLocalTime(input).fromNow();
};
export const getKeepTime = (input) => {
const { m, h, d, w } = SECOND_IN_TIME_UNIT;
if (!input) {
return '-';
}
if (input < 0) {
return t('Permanent');
}
if (input < m) {
return t('to delete');
}
let interval = 0;
if (input < h) {
interval = parseInt(input / m, 10);
return t(
'{interval, plural, =1 {one minute} other {# minutes} } later delete',
{ interval }
);
}
if (input < d) {
interval = parseInt(input / h, 10);
return t(
'{interval, plural, =1 {one hour} other {# hours} } later delete',
{ interval }
);
}
if (input < w) {
interval = parseInt(input / d, 10);
return t('{interval, plural, =1 {one day} other {# days} } later delete', {
interval,
});
}
interval = parseInt(input / w, 10);
return t('{interval, plural, =1 {one week} other {# weeks} } later delete', {
interval,
});
};