tangled
alpha
login
or
join now
dunkirk.sh
/
zera
5
fork
atom
the home site for me: also iteration 3 or 4 of my site
5
fork
atom
overview
issues
pulls
pipelines
feat: update relative time to use 2 week period
dunkirk.sh
4 days ago
17bd0c11
198814a7
verified
This commit was signed with the committer's
known signature
.
dunkirk.sh
SSH Key Fingerprint:
SHA256:DqcG0RXYExE26KiWo3VxJnsxswN1QNfTBvB+bdSpk80=
+25
-12
1 changed file
expand all
collapse all
unified
split
static
js
relative-time.js
+25
-12
static/js/relative-time.js
···
32
32
}
33
33
34
34
get threshold() {
35
35
-
return this.getAttribute('threshold') || 'P30D';
35
35
+
return this.getAttribute('threshold') || 'P14D';
36
36
}
37
37
38
38
get prefix() {
···
70
70
this.scheduleUpdate(3600000);
71
71
} else {
72
72
this.textContent = this.formatRelative(diff);
73
73
-
this.scheduleUpdate(this.getNextUpdateDelay(absDiff));
73
73
+
const delay = this.getNextUpdateDelay(absDiff);
74
74
+
if (delay !== null) this.scheduleUpdate(delay);
74
75
}
75
76
}
76
77
···
88
89
return 60000 * 5;
89
90
} else if (days < 7) {
90
91
return 3600000;
92
92
+
} else if (days < 30) {
93
93
+
return 3600000 * 6;
91
94
} else {
92
92
-
return 3600000 * 6;
95
95
+
return null;
93
96
}
94
97
}
95
98
99
99
+
getRtf() {
100
100
+
if (!this._rtf || this._rtfLang !== navigator.language) {
101
101
+
this._rtfLang = navigator.language;
102
102
+
this._rtf = new Intl.RelativeTimeFormat(navigator.language, {
103
103
+
numeric: 'auto',
104
104
+
style: 'long'
105
105
+
});
106
106
+
}
107
107
+
return this._rtf;
108
108
+
}
109
109
+
96
110
formatRelative(diff) {
97
97
-
const rtf = new Intl.RelativeTimeFormat(navigator.language, {
98
98
-
numeric: 'auto',
99
99
-
style: 'long'
100
100
-
});
111
111
+
const rtf = this.getRtf();
101
112
102
113
const absDiff = Math.abs(diff);
103
114
const sign = diff > 0 ? -1 : 1;
···
105
116
const minutes = Math.floor(seconds / 60);
106
117
const hours = Math.floor(minutes / 60);
107
118
const days = Math.floor(hours / 24);
108
108
-
const months = Math.floor(days / 30);
109
109
-
const years = Math.floor(days / 365);
119
119
+
const date = new Date(Date.now() - diff);
120
120
+
const now = new Date();
121
121
+
const months = (now.getFullYear() - date.getFullYear()) * 12 + (now.getMonth() - date.getMonth());
122
122
+
const years = Math.floor(Math.abs(months) / 12) * sign;
110
123
111
124
if (seconds < 60) {
112
125
return rtf.format(sign * seconds, 'second');
···
116
129
return rtf.format(sign * hours, 'hour');
117
130
} else if (days < 30) {
118
131
return rtf.format(sign * days, 'day');
119
119
-
} else if (months < 12) {
120
120
-
return rtf.format(sign * months, 'month');
132
132
+
} else if (Math.abs(months) < 12) {
133
133
+
return rtf.format(months, 'month');
121
134
} else {
122
122
-
return rtf.format(sign * years, 'year');
135
135
+
return rtf.format(years, 'year');
123
136
}
124
137
}
125
138