Skip to content

Commit 6c00403

Browse files
cushercrisbeto
authored andcommitted
fix(material-luxon-adapter): zone on DateTime ignored (#26887)
Fixes a bug where the timezone on the Luxon DateTime is thrown away during conversion to string, resulting in using the Luxon defaultZone regardless of how the particular DateTime is configured. Fixes #26869 (cherry picked from commit 00ff979)
1 parent a95a904 commit 6c00403

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import {LOCALE_ID} from '@angular/core';
1010
import {TestBed, waitForAsync} from '@angular/core/testing';
1111
import {DateAdapter, MAT_DATE_LOCALE} from '@angular/material/core';
12-
import {DateTime} from 'luxon';
12+
import {DateTime, FixedOffsetZone, Settings} from 'luxon';
1313
import {LuxonDateModule} from './index';
1414
import {MAT_LUXON_DATE_ADAPTER_OPTIONS} from './luxon-date-adapter';
1515

@@ -351,6 +351,16 @@ describe('LuxonDateAdapter', () => {
351351
expect(date).toEqual('2. jan. 2017');
352352
});
353353

354+
it('should format with a different timezone', () => {
355+
Settings.defaultZone = FixedOffsetZone.parseSpecifier('UTC-12');
356+
357+
let date = adapter.format(DateTime.local(2017, JAN, 2, {zone: 'UTC-12'}), 'DD');
358+
expect(date).toEqual('Jan 2, 2017');
359+
360+
date = adapter.format(DateTime.local(2017, JAN, 2, {zone: 'UTC+12'}), 'DD');
361+
expect(date).toEqual('Jan 2, 2017');
362+
});
363+
354364
it('should throw when attempting to format invalid date', () => {
355365
expect(() => adapter.format(DateTime.fromMillis(NaN), 'LL/dd/yyyy')).toThrowError(
356366
/LuxonDateAdapter: Cannot format invalid date\./,

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,11 @@ export class LuxonDateAdapter extends DateAdapter<LuxonDateTime> {
193193
if (!this.isValid(date)) {
194194
throw Error('LuxonDateAdapter: Cannot format invalid date.');
195195
}
196-
return date
197-
.setLocale(this.locale)
198-
.setZone(this._useUTC ? 'utc' : undefined)
199-
.toFormat(displayFormat);
196+
if (this._useUTC) {
197+
return date.setLocale(this.locale).setZone('utc').toFormat(displayFormat);
198+
} else {
199+
return date.setLocale(this.locale).toFormat(displayFormat);
200+
}
200201
}
201202

202203
addCalendarYears(date: LuxonDateTime, years: number): LuxonDateTime {

0 commit comments

Comments
 (0)