summaryrefslogtreecommitdiff
path: root/pse-dashboard/src/components/LastUpdate.vue
blob: b30b254cd11d98cc43a639b52132ab08edf074d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<script setup>
import { computed } from 'vue'
import { useI18n } from 'vue-i18n';
import dayjs from 'dayjs/esm'
import relativeTime from 'dayjs/esm/plugin/relativeTime'
import utc from 'dayjs/esm/plugin/utc'
import 'dayjs/esm/locale/de.js'
dayjs.extend(relativeTime)
dayjs.extend(utc)

const props = defineProps({
    iso: {
        type: String,
        default: ""
    },
    unix: {
        type: Number,
        default: 0
    }
});

const { locale } = useI18n();

const tzOffset = (new Date()).getTimezoneOffset();

// compute difference from given iso string and now whenever data changes
const lastUpdate = computed(() => {
    let lastUpdateTime;

    if (props.iso) {
        lastUpdateTime = dayjs(props.iso).utc(true);
    } else {
        // lastUpdateTime = dayjs(props.unix * 1000);
        lastUpdateTime = dayjs(props.unix * 1000).utcOffset(tzOffset).utc(true);
    }

    return lastUpdateTime.locale(locale.value).fromNow();
});

</script>
<template>
    {{ lastUpdate }}
</template>
<style scoped>
</style>