Skip to content

Commit

Permalink
fix(material-luxon-adapter): zone on DateTime ignored (angular#26887)
Browse files Browse the repository at this point in the history
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 angular#26869
  • Loading branch information
cusher authored Apr 6, 2023
1 parent bb15f1b commit 00ff979
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
12 changes: 11 additions & 1 deletion src/material-luxon-adapter/adapter/luxon-date-adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import {LOCALE_ID} from '@angular/core';
import {TestBed, waitForAsync} from '@angular/core/testing';
import {DateAdapter, MAT_DATE_LOCALE} from '@angular/material/core';
import {DateTime} from 'luxon';
import {DateTime, FixedOffsetZone, Settings} from 'luxon';
import {LuxonDateModule} from './index';
import {MAT_LUXON_DATE_ADAPTER_OPTIONS} from './luxon-date-adapter';

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

it('should format with a different timezone', () => {
Settings.defaultZone = FixedOffsetZone.parseSpecifier('UTC-12');

let date = adapter.format(DateTime.local(2017, JAN, 2, {zone: 'UTC-12'}), 'DD');
expect(date).toEqual('Jan 2, 2017');

date = adapter.format(DateTime.local(2017, JAN, 2, {zone: 'UTC+12'}), 'DD');
expect(date).toEqual('Jan 2, 2017');
});

it('should throw when attempting to format invalid date', () => {
expect(() => adapter.format(DateTime.fromMillis(NaN), 'LL/dd/yyyy')).toThrowError(
/LuxonDateAdapter: Cannot format invalid date\./,
Expand Down
9 changes: 5 additions & 4 deletions src/material-luxon-adapter/adapter/luxon-date-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,11 @@ export class LuxonDateAdapter extends DateAdapter<LuxonDateTime> {
if (!this.isValid(date)) {
throw Error('LuxonDateAdapter: Cannot format invalid date.');
}
return date
.setLocale(this.locale)
.setZone(this._useUTC ? 'utc' : undefined)
.toFormat(displayFormat);
if (this._useUTC) {
return date.setLocale(this.locale).setZone('utc').toFormat(displayFormat);
} else {
return date.setLocale(this.locale).toFormat(displayFormat);
}
}

addCalendarYears(date: LuxonDateTime, years: number): LuxonDateTime {
Expand Down

0 comments on commit 00ff979

Please sign in to comment.