forked from gbtami/pychess-variants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatetime.ts
44 lines (41 loc) · 2.11 KB
/
datetime.ts
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
import { _, ngettext } from './i18n';
export function timeago(date: string) {
const TZdate = (new Date(date)).getTime();
const maxLength: {[key:string]: number} = { second: 60, minute: 60, hour: 24, day: 7, week: 4.35, month: 12, year: 10000 };
let val, inTheFuture;
if (Date.now() >= TZdate) {
val = (Date.now() - TZdate) / 1000;
inTheFuture = false;
} else {
val = (TZdate - Date.now()) / 1000;
inTheFuture = true;
}
for (const unit in maxLength) {
if (Math.floor(val / maxLength[unit]) === 0) {
const result = Math.floor(val);
switch (unit) {
case "year":
return inTheFuture ? ngettext("in %1 year", "in %1 years", result) : ngettext("%1 year ago", "%1 years ago", result);
case "month":
return inTheFuture ? ngettext("in %1 month", "in %1 months", result) : ngettext("%1 month ago", "%1 months ago", result);
case "week":
return inTheFuture ? ngettext("in %1 week", "in %1 weeks", result) : ngettext("%1 week ago", "%1 weeks ago", result);
case "day":
return inTheFuture ? ngettext("in %1 day", "in %1 days", result) : ngettext("%1 day ago", "%1 days ago", result);
case "hour":
return inTheFuture ? ngettext("in %1 hour", "in %1 hours", result) : ngettext("%1 hour ago", "%1 hours ago", result);
case "minute":
return inTheFuture ? ngettext("in %1 minute", "in %1 minutes", result) : ngettext("%1 minute ago", "%1 minutes ago", result);
case "second":
return inTheFuture ? ngettext("in %1 second", "in %1 seconds", result) : ngettext("%1 second ago", "%1 seconds ago", result);
}
}
val = val / maxLength[unit];
}
return '';
}
export function renderTimeago() {
const els = document.getElementsByTagName("info-date");
Array.from(els).forEach((el) => {el.innerHTML = timeago(el.getAttribute('timestamp') ?? "unknown when");});
setTimeout(renderTimeago, 1200);
}