Skip to content

Commit 452d3cc

Browse files
fix(material-luxon-adapter): preserve timezone on .clone() (#33564)
Fixes a bug in the Angular Material LuxonDateAdapter where the .clone() method didn't clone the zone of the original DateTime object and caused issues with the TimePicker component.
1 parent 0e34c64 commit 452d3cc

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎src/material-luxon-adapter/adapter/luxon-date-adapter.spec.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,22 @@ describe('LuxonDateAdapter', () => {
418418
expect(clone.toISO()).toEqual(date.toISO());
419419
});
420420

421+
it('should respect timezone on clone', () => {
422+
const dateLocal = DateTime.local(2017, JAN, 1);
423+
const dateInCet = dateLocal.setZone('Europe/Budapest');
424+
const cloneInCet = adapter.clone(dateInCet);
425+
const dateInPst = dateLocal.setZone('America/Los_Angeles');
426+
const cloneInPst = adapter.clone(dateInPst);
427+
428+
expect(cloneInCet).not.toBe(dateInCet);
429+
expect(cloneInCet.toISO()).toEqual(dateInCet.toISO());
430+
expect(cloneInCet.zone).toEqual(dateInCet.zone);
431+
432+
expect(cloneInPst).not.toBe(dateInPst);
433+
expect(cloneInPst.toISO()).toEqual(dateInPst.toISO());
434+
expect(cloneInPst.zone).toEqual(dateInPst.zone);
435+
});
436+
421437
it('should compare dates', () => {
422438
expect(
423439
adapter.compareDate(DateTime.local(2017, JAN, 1), DateTime.local(2017, JAN, 2)),

‎src/material-luxon-adapter/adapter/luxon-date-adapter.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,10 @@ export class LuxonDateAdapter extends DateAdapter<LuxonDateTime> {
134134
}
135135

136136
clone(date: LuxonDateTime): LuxonDateTime {
137-
return LuxonDateTime.fromObject(date.toObject(), this._getOptions());
137+
return LuxonDateTime.fromObject(date.toObject(), {
138+
...this._getOptions(),
139+
zone: date.zone,
140+
});
138141
}
139142

140143
createDate(year: number, month: number, date: number): LuxonDateTime {

0 commit comments

Comments
 (0)