Cleanup locate time handling

This commit is contained in:
the-djmaze 2022-12-23 12:22:57 +01:00
parent fca7e44aa0
commit b0ed74575f
9 changed files with 58 additions and 71 deletions

39
dev/prototype.js vendored
View file

@ -5,23 +5,13 @@
return this.filter((v, i, a) => (fn ? fn(v) : v) && a.indexOf(v) === i);
};
Date.defineRelativeTimeFormat = config => relativeTime = config;
// full = Monday, December 12, 2022 at 12:16:21 PM Central European Standard Time
// long = December 12, 2022 at 12:16:21 PM GMT+1
// medium = Dec 12, 2022, 12:16:21 PM
// short = 12/12/22, 12:16 PM
let formats = {
LT : {timeStyle: 'short'},
// LT : {hour: 'numeric', minute: 'numeric'},
L : {},
LL : {dateStyle: 'medium'},
// LL : {year: 'numeric', month: 'short', day: 'numeric'},
LLL : {dateStyle: 'long', timeStyle: 'short'},
// LLL : {year: 'numeric', month: 'long', day: 'numeric', hour: 'numeric', minute: 'numeric'},
LLLL : {dateStyle: 'full', timeStyle: 'short'},
// LLLL : {weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', hour: 'numeric', minute: 'numeric'},
'd M': {day: '2-digit', month: 'short'}
LLL : {dateStyle: 'long', timeStyle: 'short'}
},
phpFormats = {
// Day
@ -50,9 +40,6 @@
v: {fractionalSecondDigits: 3},
Z: {timeZone: 'UTC'}
},
relativeTime = {
// see /snappymail/v/0.0.0/app/localization/relativetimeformat/
},
pad2 = v => 10 > v ? '0' + v : v;
// Format momentjs/PHP date formats to Intl.DateTimeFormat
@ -82,30 +69,6 @@
return new Intl.DateTimeFormat(doc.documentElement.lang, options).format(this);
};
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat
Date.prototype.fromNow = function() {
let unit = 'second',
value = Math.round((this.getTime() - Date.now()) / 1000),
t = [[60,'minute'],[3600,'hour'],[86400,'day'],[2628000,'month'],[31536000,'year']],
i = 5,
abs = Math.abs(value);
while (i--) {
if (t[i][0] <= abs) {
value = Math.round(value / t[i][0]);
unit = t[i][1];
break;
}
}
if (Intl.RelativeTimeFormat) {
let rtf = new Intl.RelativeTimeFormat(doc.documentElement.lang);
return rtf.format(value, unit);
}
abs = Math.abs(value);
let rtf = relativeTime.long[unit][0 > value ? 'past' : 'future'],
plural = relativeTime.plural(abs);
return (rtf[plural] || rtf).replace('{0}', abs);
};
Element.prototype.closestWithin = function(selector, parent) {
const el = this.closest(selector);
return (el && el !== parent && parent.contains(el)) ? el : null;