UNPKG

@nextcloud/vue

Version:
45 lines (44 loc) 2.37 kB
import { getCanonicalLocale as s } from "@nextcloud/l10n"; import { t as n } from "../chunks/l10n-9a5a6afc.mjs"; import { n as m } from "../chunks/_plugin-vue2_normalizer-5b4c43a4.mjs"; const l = { long: n("a few seconds ago"), short: n("seconds ago"), narrow: n("sec. ago") }, d = { name: "NcDatetime", props: { timestamp: { type: [Date, Number], required: !0 }, format: { type: Object, default: () => ({ timeStyle: "medium", dateStyle: "short" }) }, relativeTime: { type: [Boolean, String], default: "long", validator: (t) => t === !1 || ["long", "short", "narrow"].includes(t) }, ignoreSeconds: { type: Boolean, default: !1 } }, data() { return { currentTime: Date.now(), intervalId: void 0 }; }, computed: { dateObject() { return new Date(this.timestamp); }, formattedTime() { if (this.relativeTime !== !1) { const t = new Intl.RelativeTimeFormat(s(), { numeric: "auto", style: this.relativeTime }), e = (this.dateObject - new Date(this.currentTime)) / 1e3; if (Math.abs(e) <= 90) return this.ignoreSeconds ? l[this.relativeTime] : t.format(Math.round(e), "second"); const r = e / 60; if (Math.abs(r) <= 90) return t.format(Math.round(r), "minute"); const i = r / 60; if (Math.abs(i) <= 72) return t.format(Math.round(i), "hour"); const a = i / 24; if (Math.abs(a) <= 6) return t.format(Math.round(a), "day"); const o = a / 7; return Math.abs(o) <= 52 ? t.format(Math.round(o), "week") : t.format(Math.round(a / 365), "year"); } return this.formattedFullTime; }, formattedFullTime() { return new Intl.DateTimeFormat(s(), this.format).format(this.dateObject); } }, watch: { relativeTime(t, e) { window.clearInterval(this.intervalId), this.intervalId = void 0, t && (this.intervalId = window.setInterval(this.setCurrentTime, 1e3)); } }, mounted() { this.relativeTime !== !1 && (this.intervalId = window.setInterval(this.setCurrentTime, 1e3)); }, destroyed() { window.clearInterval(this.intervalId); }, methods: { setCurrentTime() { this.currentTime = Date.now(); } } }; var u = function() { var t = this, e = t._self._c; return e("span", { staticClass: "nc-datetime", attrs: { "data-timestamp": t.timestamp, title: t.formattedFullTime } }, [t._v(t._s(t.formattedTime))]); }, h = [], c = m(d, u, h, !1, null, null, null, null); const T = c.exports; export { T as default };