Skip to content

Commit aee9801

Browse files
authored
Fix toAbsoluteLocaleDate and add more tests (go-gitea#32387)
1 parent 58eb16e commit aee9801

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

web_src/js/webcomponents/absolute-date.test.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@ test('toAbsoluteLocaleDate', () => {
1313
day: 'numeric',
1414
})).toEqual('15. März 2024');
1515

16-
expect(toAbsoluteLocaleDate('12345-03-15 01:02:03', '', {
17-
year: 'numeric',
18-
month: 'short',
19-
day: 'numeric',
20-
})).toEqual('Mar 15, 12345');
16+
// these cases shouldn't happen
17+
expect(toAbsoluteLocaleDate('2024-03-15 01:02:03', '', {})).toEqual('Invalid Date');
18+
expect(toAbsoluteLocaleDate('10000-01-01', '', {})).toEqual('Invalid Date');
19+
20+
// test different timezone
21+
const oldTZ = process.env.TZ;
22+
process.env.TZ = 'America/New_York';
23+
expect(new Date('2024-03-15').toLocaleString()).toEqual('3/14/2024, 8:00:00 PM');
24+
expect(toAbsoluteLocaleDate('2024-03-15')).toEqual('3/15/2024, 12:00:00 AM');
25+
process.env.TZ = oldTZ;
2126
});

web_src/js/webcomponents/absolute-date.ts

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
export function toAbsoluteLocaleDate(date: string, lang: string, opts: Intl.DateTimeFormatOptions) {
2-
return new Date(date).toLocaleString(lang || [], opts);
1+
export function toAbsoluteLocaleDate(date: string, lang?: string, opts?: Intl.DateTimeFormatOptions) {
2+
// only use the date part, it is guaranteed to be in ISO format (YYYY-MM-DDTHH:mm:ss.sssZ) or (YYYY-MM-DD)
3+
// if there is an "Invalid Date" error, there must be something wrong in code and should be fixed.
4+
// TODO: there is a root problem in backend code: the date "YYYY-MM-DD" is passed to backend without timezone (eg: deadline),
5+
// then backend parses it in server's timezone and stores the parsed timestamp into database.
6+
// If the user's timezone is different from the server's, the date might be displayed in the wrong day.
7+
const dateSep = date.indexOf('T');
8+
date = dateSep === -1 ? date : date.substring(0, dateSep);
9+
return new Date(`${date}T00:00:00`).toLocaleString(lang || [], opts);
310
}
411

512
window.customElements.define('absolute-date', class extends HTMLElement {
@@ -15,14 +22,8 @@ window.customElements.define('absolute-date', class extends HTMLElement {
1522
const lang = this.closest('[lang]')?.getAttribute('lang') ||
1623
this.ownerDocument.documentElement.getAttribute('lang') || '';
1724

18-
// only use the date part, it is guaranteed to be in ISO format (YYYY-MM-DDTHH:mm:ss.sssZ)
19-
let date = this.getAttribute('date');
20-
let dateSep = date.indexOf('T');
21-
dateSep = dateSep === -1 ? date.indexOf(' ') : dateSep;
22-
date = dateSep === -1 ? date : date.substring(0, dateSep);
23-
2425
if (!this.shadowRoot) this.attachShadow({mode: 'open'});
25-
this.shadowRoot.textContent = toAbsoluteLocaleDate(date, lang, opt);
26+
this.shadowRoot.textContent = toAbsoluteLocaleDate(this.getAttribute('date'), lang, opt);
2627
};
2728

2829
attributeChangedCallback(_name, oldValue, newValue) {

0 commit comments

Comments
 (0)