-
Notifications
You must be signed in to change notification settings - Fork 6.8k
feat(material-luxon-adapter) add option to set zone #33579
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
laliconfigcat
wants to merge
1
commit into
angular:main
Choose a base branch
from
laliconfigcat:luxon-data-adapter-zone-handling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ import { | |
| DateTime as LuxonDateTime, | ||
| Info as LuxonInfo, | ||
| DateTimeOptions as LuxonDateTimeOptions, | ||
| LocaleOptions as LuxonLocaleOptions, | ||
| CalendarSystem as LuxonCalendarSystem, | ||
| } from 'luxon'; | ||
|
|
||
|
|
@@ -23,6 +24,13 @@ export interface MatLuxonDateAdapterOptions { | |
| */ | ||
| useUtc: boolean; | ||
|
|
||
| /** | ||
| * Sets the zone of DateTime objects. | ||
| * 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; | ||
|
|
||
| /** | ||
| * Sets the first day of week. | ||
| * Changing this will change how Angular Material components like DatePicker shows start of week. | ||
|
|
@@ -63,6 +71,7 @@ export class LuxonDateAdapter extends DateAdapter<LuxonDateTime> { | |
| private _useUTC: boolean; | ||
| private _firstDayOfWeek: number | undefined; | ||
| private _defaultOutputCalendar: LuxonCalendarSystem; | ||
| private _zone?: string; | ||
|
|
||
| constructor() { | ||
| super(); | ||
|
|
@@ -75,6 +84,7 @@ export class LuxonDateAdapter extends DateAdapter<LuxonDateTime> { | |
| this._useUTC = !!options?.useUtc; | ||
| this._firstDayOfWeek = options?.firstDayOfWeek; | ||
| this._defaultOutputCalendar = options?.defaultOutputCalendar || 'gregory'; | ||
| this.setZone(options?.zone); | ||
| this.setLocale(dateLocale || LuxonDateTime.local().locale); | ||
| } | ||
|
|
||
|
|
@@ -122,7 +132,7 @@ export class LuxonDateAdapter extends DateAdapter<LuxonDateTime> { | |
| } | ||
|
|
||
| getYearName(date: LuxonDateTime): string { | ||
| return date.toFormat('yyyy', this._getOptions()); | ||
| return date.toFormat('yyyy', this._getLocaleOptions()); | ||
| } | ||
|
|
||
| getFirstDayOfWeek(): number { | ||
|
|
@@ -135,14 +145,12 @@ export class LuxonDateAdapter extends DateAdapter<LuxonDateTime> { | |
|
|
||
| clone(date: LuxonDateTime): LuxonDateTime { | ||
| return LuxonDateTime.fromObject(date.toObject(), { | ||
| ...this._getOptions(), | ||
| ...this._getLocaleOptions(), | ||
| zone: date.zone, | ||
| }); | ||
| } | ||
|
|
||
| createDate(year: number, month: number, date: number): LuxonDateTime { | ||
| const options = this._getOptions(); | ||
|
|
||
| if (month < 0 || month > 11) { | ||
| throw Error(`Invalid month index "${month}". Month index has to be between 0 and 11.`); | ||
| } | ||
|
|
@@ -153,8 +161,8 @@ export class LuxonDateAdapter extends DateAdapter<LuxonDateTime> { | |
|
|
||
| // Luxon uses 1-indexed months so we need to add one to the month. | ||
| 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()) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't setting the timezone conflict with the setting for enabling UTC? |
||
| : LuxonDateTime.local(year, month + 1, date, this._getDateTimeOptions()); | ||
|
|
||
| if (!this.isValid(result)) { | ||
| throw Error(`Invalid date "${date}". Reason: "${result.invalidReason}".`); | ||
|
|
@@ -164,13 +172,13 @@ export class LuxonDateAdapter extends DateAdapter<LuxonDateTime> { | |
| } | ||
|
|
||
| today(): LuxonDateTime { | ||
| const options = this._getOptions(); | ||
|
|
||
| return this._useUTC ? LuxonDateTime.utc(options) : LuxonDateTime.local(options); | ||
| return this._useUTC | ||
| ? LuxonDateTime.utc(this._getLocaleOptions()) | ||
| : LuxonDateTime.local(this._getDateTimeOptions()); | ||
| } | ||
|
|
||
| parse(value: unknown, parseFormat: string | string[]): LuxonDateTime | null { | ||
| const options: LuxonDateTimeOptions = this._getOptions(); | ||
| const options: LuxonDateTimeOptions = this._getDateTimeOptions(); | ||
|
|
||
| if (typeof value == 'string' && value.length > 0) { | ||
| const iso8601Date = LuxonDateTime.fromISO(value, options); | ||
|
|
@@ -217,15 +225,15 @@ export class LuxonDateAdapter extends DateAdapter<LuxonDateTime> { | |
| } | ||
|
|
||
| addCalendarYears(date: LuxonDateTime, years: number): LuxonDateTime { | ||
| return date.reconfigure(this._getOptions()).plus({years}); | ||
| return date.reconfigure(this._getLocaleOptions()).plus({years}); | ||
| } | ||
|
|
||
| addCalendarMonths(date: LuxonDateTime, months: number): LuxonDateTime { | ||
| return date.reconfigure(this._getOptions()).plus({months}); | ||
| return date.reconfigure(this._getLocaleOptions()).plus({months}); | ||
| } | ||
|
|
||
| addCalendarDays(date: LuxonDateTime, days: number): LuxonDateTime { | ||
| return date.reconfigure(this._getOptions()).plus({days}); | ||
| return date.reconfigure(this._getLocaleOptions()).plus({days}); | ||
| } | ||
|
|
||
| toIso8601(date: LuxonDateTime): string { | ||
|
|
@@ -238,7 +246,7 @@ export class LuxonDateAdapter extends DateAdapter<LuxonDateTime> { | |
| * string into null. Returns an invalid date for all other values. | ||
| */ | ||
| override deserialize(value: unknown): LuxonDateTime | null { | ||
| const options = this._getOptions(); | ||
| const options = this._getDateTimeOptions(); | ||
| let date: LuxonDateTime | undefined; | ||
| if (value instanceof Date) { | ||
| date = LuxonDateTime.fromJSDate(value, options); | ||
|
|
@@ -320,15 +328,31 @@ export class LuxonDateAdapter extends DateAdapter<LuxonDateTime> { | |
| } | ||
|
|
||
| override addSeconds(date: LuxonDateTime, amount: number): LuxonDateTime { | ||
| return date.reconfigure(this._getOptions()).plus({seconds: amount}); | ||
| return date.reconfigure(this._getLocaleOptions()).plus({seconds: amount}); | ||
| } | ||
|
|
||
| /** Gets the options that should be used when constructing a new `DateTime` object. */ | ||
| private _getOptions(): LuxonDateTimeOptions { | ||
| /** | ||
| * Sets the zone of DateTime objects. | ||
| * 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) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be |
||
| this._zone = zone; | ||
| } | ||
|
|
||
| /** Gets the Locale options that should be used when Luxon expects a LocaleOptions parameter. */ | ||
| private _getLocaleOptions(): LuxonLocaleOptions { | ||
| return { | ||
| zone: this._useUTC ? 'utc' : undefined, | ||
| locale: this.locale, | ||
| outputCalendar: this._defaultOutputCalendar, | ||
| }; | ||
| } | ||
|
|
||
| /** Gets the DateTime options that should be used when Luxon expects a DateTimeOptions, e.g. when constructing/parsing a `DateTime` object. */ | ||
| private _getDateTimeOptions(): LuxonDateTimeOptions { | ||
| return { | ||
| ...this._getLocaleOptions(), | ||
| zone: this._useUTC ? 'utc' : this._zone, | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.