2018-02-05 05:25:19 +00:00
|
|
|
<template>
|
2020-10-27 09:11:41 +00:00
|
|
|
<time :title="absolute">
|
2022-01-15 22:47:28 +00: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 04:49:48 +00:00
|
|
|
</time>
|
2018-02-05 05:25:19 +00:00
|
|
|
</template>
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2022-01-15 22:47:28 +00:00
|
|
|
<script lang="ts" setup>
|
|
|
|
import { onUnmounted } from 'vue';
|
|
|
|
import { i18n } from '@/i18n';
|
2018-02-09 09:28:06 +00:00
|
|
|
|
2022-01-15 22:47:28 +00:00
|
|
|
const props = withDefaults(defineProps<{
|
|
|
|
time: Date | string;
|
|
|
|
mode?: 'relative' | 'absolute' | 'detail';
|
|
|
|
}>(), {
|
|
|
|
mode: 'relative',
|
|
|
|
});
|
|
|
|
|
2022-05-26 13:53:09 +00:00
|
|
|
const _time = typeof props.time === 'string' ? new Date(props.time) : props.time;
|
2022-01-15 22:47:28 +00:00
|
|
|
const absolute = _time.toLocaleString();
|
2018-06-09 23:03:02 +00:00
|
|
|
|
2022-01-15 22:47:28 +00:00
|
|
|
let now = $ref(new Date());
|
|
|
|
const relative = $computed(() => {
|
|
|
|
const ago = (now.getTime() - _time.getTime()) / 1000/*ms*/;
|
|
|
|
return (
|
2022-01-28 06:29:24 +00: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() }) :
|
2022-01-15 22:47:28 +00:00
|
|
|
ago >= 60 ? i18n.t('_ago.minutesAgo', { n: (~~(ago / 60)).toString() }) :
|
|
|
|
ago >= 10 ? i18n.t('_ago.secondsAgo', { n: (~~(ago % 60)).toString() }) :
|
2022-01-28 02:39:49 +00:00
|
|
|
ago >= -1 ? i18n.ts._ago.justNow :
|
2022-05-29 06:15:52 +00:00
|
|
|
i18n.ts._ago.future);
|
2018-02-13 04:49:48 +00:00
|
|
|
});
|
2022-01-15 22:47:28 +00:00
|
|
|
|
|
|
|
function tick() {
|
|
|
|
// TODO: パフォーマンス向上のため、このコンポーネントが画面内に表示されている場合のみ更新する
|
|
|
|
now = new Date();
|
|
|
|
|
|
|
|
tickId = window.setTimeout(() => {
|
|
|
|
window.requestAnimationFrame(tick);
|
|
|
|
}, 10000);
|
|
|
|
}
|
|
|
|
|
|
|
|
let tickId: number;
|
|
|
|
|
|
|
|
if (props.mode === 'relative' || props.mode === 'detail') {
|
|
|
|
tickId = window.requestAnimationFrame(tick);
|
|
|
|
|
|
|
|
onUnmounted(() => {
|
|
|
|
window.clearTimeout(tickId);
|
|
|
|
});
|
|
|
|
}
|
2018-02-05 05:25:19 +00:00
|
|
|
</script>
|