2018-02-05 07:25:19 +02:00
|
|
|
<template>
|
2020-10-27 11:11:41 +02:00
|
|
|
<time :title="absolute">
|
2022-01-16 00:47:28 +02:00
|
|
|
<template v-if="mode === 'relative'">{{ relative }}</template>
|
|
|
|
<template v-else-if="mode === 'absolute'">{{ absolute }}</template>
|
|
|
|
<template v-else-if="mode === 'detail'">{{ absolute }} ({{ relative }})</template>
|
2018-02-13 06:49:48 +02:00
|
|
|
</time>
|
2018-02-05 07:25:19 +02:00
|
|
|
</template>
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2022-01-16 00:47:28 +02:00
|
|
|
<script lang="ts" setup>
|
|
|
|
import { onUnmounted } from 'vue';
|
|
|
|
import { i18n } from '@/i18n';
|
2023-01-01 10:11:33 +02:00
|
|
|
import { dateTimeFormat } from '@/scripts/intl-const';
|
2018-02-09 11:28:06 +02:00
|
|
|
|
2022-01-16 00:47:28 +02:00
|
|
|
const props = withDefaults(defineProps<{
|
|
|
|
time: Date | string;
|
|
|
|
mode?: 'relative' | 'absolute' | 'detail';
|
|
|
|
}>(), {
|
|
|
|
mode: 'relative',
|
|
|
|
});
|
|
|
|
|
2022-05-26 16:53:09 +03:00
|
|
|
const _time = typeof props.time === 'string' ? new Date(props.time) : props.time;
|
2023-01-01 10:11:33 +02:00
|
|
|
const absolute = dateTimeFormat.format(_time);
|
2018-06-10 02:03:02 +03:00
|
|
|
|
2022-08-06 13:20:53 +03:00
|
|
|
let now = $shallowRef(new Date());
|
2022-01-16 00:47:28 +02:00
|
|
|
const relative = $computed(() => {
|
|
|
|
const ago = (now.getTime() - _time.getTime()) / 1000/*ms*/;
|
|
|
|
return (
|
2022-06-25 21:12:58 +03:00
|
|
|
ago >= 31536000 ? i18n.t('_ago.yearsAgo', { n: Math.round(ago / 31536000).toString() }) :
|
|
|
|
ago >= 2592000 ? i18n.t('_ago.monthsAgo', { n: Math.round(ago / 2592000).toString() }) :
|
|
|
|
ago >= 604800 ? i18n.t('_ago.weeksAgo', { n: Math.round(ago / 604800).toString() }) :
|
|
|
|
ago >= 86400 ? i18n.t('_ago.daysAgo', { n: Math.round(ago / 86400).toString() }) :
|
|
|
|
ago >= 3600 ? i18n.t('_ago.hoursAgo', { n: Math.round(ago / 3600).toString() }) :
|
|
|
|
ago >= 60 ? i18n.t('_ago.minutesAgo', { n: (~~(ago / 60)).toString() }) :
|
|
|
|
ago >= 10 ? i18n.t('_ago.secondsAgo', { n: (~~(ago % 60)).toString() }) :
|
|
|
|
ago >= -1 ? i18n.ts._ago.justNow :
|
2022-05-29 09:15:52 +03:00
|
|
|
i18n.ts._ago.future);
|
2018-02-13 06:49:48 +02:00
|
|
|
});
|
2022-01-16 00:47:28 +02:00
|
|
|
|
2023-01-06 09:58:43 +02:00
|
|
|
let tickId: number;
|
|
|
|
|
2022-01-16 00:47:28 +02:00
|
|
|
function tick() {
|
|
|
|
now = new Date();
|
2023-01-04 01:54:54 +02:00
|
|
|
const ago = (now.getTime() - _time.getTime()) / 1000/*ms*/;
|
|
|
|
const next = ago < 60 ? 10000 : ago < 3600 ? 60000 : 180000;
|
2022-01-16 00:47:28 +02:00
|
|
|
|
2023-01-06 09:58:43 +02:00
|
|
|
tickId = window.setTimeout(tick, next);
|
2022-01-16 00:47:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (props.mode === 'relative' || props.mode === 'detail') {
|
2023-01-06 09:58:43 +02:00
|
|
|
tick();
|
2022-01-16 00:47:28 +02:00
|
|
|
|
|
|
|
onUnmounted(() => {
|
2023-01-06 09:58:43 +02:00
|
|
|
window.clearTimeout(tickId);
|
2022-01-16 00:47:28 +02:00
|
|
|
});
|
|
|
|
}
|
2018-02-05 07:25:19 +02:00
|
|
|
</script>
|