Skip to content

feat(material-luxon-adapter) add option to set zone - #33579

Merged
crisbeto merged 1 commit into
angular:mainfrom
laliconfigcat:luxon-data-adapter-zone-handling
Sep 14, 2026
Merged

feat(material-luxon-adapter) add option to set zone#33579
crisbeto merged 1 commit into
angular:mainfrom
laliconfigcat:luxon-data-adapter-zone-handling

Conversation

@laliconfigcat

@laliconfigcat laliconfigcat commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Add zone option to be able to change the zone through MAT_LUXON_DATE_ADAPTER_OPTIONS.

In a web app where the user can set their preferred timezone apart from local and UTC, the current implementation is not sufficient. The current UTC/local works fine, but there are use cases when it is not enough.
I added a new zone option to the MatLuxonDateAdapterOptions and it is passed everywhere in the adapter.

The main issue was with the DateRangePicker. However I gave it DateTimes that were in the desired timezone, after changing the range, the new DateTime objects were in local timezone.
Also, the today highlight was wrong with the today date that was using local timezone.

Basically the change is this in the _getOptions and some minor refactorings:

zone: this._useUTC ? 'utc' : undefined,

->

zone: this._useUTC ? 'utc' : this._zone,

I also added the ability to configure the zone programmatically with a public .setZone(zone: string)

The DateTimeOptions was passed to every method. I refactored it a bit so the methods that are only accepting the LocaleOptions only receive the LocaleOptions, and pass only the DateTimeOptions where the luxon methods are accepting it. This makes it clearer which methods are working with the zone and which are not. So I created a _getLocaleOptions and a _getDateTimeOptions instead of the current _getOptions. I hope you don't mind, it seems clearer to me this way.

While writing the tests, I copied the UTC tests and tried to make it work with the new zone setting. One of my tests was failing and it turned out that the original test was not working correctly. I fixed that too ("should parse dates to UTC"). The date format was wrong and the parse gave back null and the test basically tested if null equals null.

I also added some remarks to the documentation where it talked about the useUTC parameter.

@pullapprove
pullapprove Bot requested review from crisbeto and tjshiu July 23, 2026 16:37
@laliconfigcat
laliconfigcat force-pushed the luxon-data-adapter-zone-handling branch from d3acb55 to 905f8f2 Compare July 23, 2026 16:46
@angular-robot angular-robot Bot added the detected: feature PR contains a feature commit label Jul 23, 2026
@laliconfigcat

Copy link
Copy Markdown
Contributor Author

Hello @crisbeto @tjshiu ,

Is everything alright with my PR? Should I open an issue first, or is it alright to create the PR right away?

* Changing this will change how Angular Material components like DatePicker output dates.
* The zone parameter will be ignored if the useUtc parameter is set to true.
*/
zone?: string;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should call this timeZone.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went with luxon's terminology here, but i'm totally fine with modifying it to timeZone. Should we stay with luxon's terminology or should i update it to timeZone?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use timeZone.

const result = this._useUTC
? LuxonDateTime.utc(year, month + 1, date, options)
: LuxonDateTime.local(year, month + 1, date, options);
? LuxonDateTime.utc(year, month + 1, date, this._getLocaleOptions())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't setting the timezone conflict with the setting for enabling UTC?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to introduce any breaking change with this PR, so I kept the useUTC parameter and it has precedence over the new zone setting.
I added a comment to the parameter too: "The zone parameter will be ignored if the useUtc parameter is set to true.".

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should throw an error if both UTC and timeZone are set. It's not a breaking change since there shouldn't be any apps setting the time zone at the moment.

* Changing this will change how Angular Material components like DatePicker output dates.
* The zone parameter will be ignored if the useUtc parameter is set to true.
*/
setZone(zone?: string) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be setTimezone

@laliconfigcat laliconfigcat Sep 14, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went with luxon's terminology here, but i'm totally fine with modifying it to timeZone. Should we stay with luxon's terminology or should i update it to setTimeZone (or setTimezone as you suggested)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@crisbeto should i go with setTimeZone or setTimezone as you suggested? If the parameter is timeZone, I guess it would be better to go with setTimeZone.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think setTimeZone

@laliconfigcat
laliconfigcat force-pushed the luxon-data-adapter-zone-handling branch 2 times, most recently from 429e390 to 1560c63 Compare September 14, 2026 10:36
@laliconfigcat

Copy link
Copy Markdown
Contributor Author

@crisbeto I modifed the PR based on your suggestions. I also created some extra tests to test the error thrown when useUtc and timeZone is also provided.

Add zone option to be able to change the zone through MAT_LUXON_DATE_ADAPTER_OPTIONS.
@laliconfigcat
laliconfigcat force-pushed the luxon-data-adapter-zone-handling branch from 1560c63 to 1c2ff17 Compare September 14, 2026 10:50
@crisbeto crisbeto added action: merge The PR is ready for merge by the caretaker target: minor This PR is targeted for the next minor release labels Sep 14, 2026
@crisbeto
crisbeto merged commit d3acf18 into angular:main Sep 14, 2026
33 of 35 checks passed
@crisbeto

Copy link
Copy Markdown
Member

This PR was merged into the repository. The changes were merged into the following branches:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action: merge The PR is ready for merge by the caretaker detected: feature PR contains a feature commit target: minor This PR is targeted for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants