-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
feat(moment-dateadapter): add option to create utc dates #11336
Changes from 6 commits
1db44c3
e2fe907
1577506
d0abfc9
83ba2a0
cda28a1
abfb4e0
fa8ad76
c7c14fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {Inject, Injectable, Optional} from '@angular/core'; | ||
import {Inject, Injectable, Optional, InjectionToken} from '@angular/core'; | ||
import {DateAdapter, MAT_DATE_LOCALE} from '@angular/material'; | ||
// Depending on whether rollup is used, moment needs to be imported differently. | ||
// Since Moment.js doesn't have a default export, we normally need to import using the `* as` | ||
|
@@ -19,6 +19,31 @@ import {default as _rollupMoment, Moment} from 'moment'; | |
|
||
const moment = _rollupMoment || _moment; | ||
|
||
/** Configurable options for {@see MomentDateAdapter}. */ | ||
export interface MatMomentDateAdapterOptions { | ||
/** | ||
* Turns the use of utc dates on or off. | ||
* Changing this will change how Angular Material components like DatePicker output dates. | ||
* {@default false} | ||
*/ | ||
useUtc: boolean; | ||
} | ||
|
||
/** InjectionToken for moment date adapter to configure options. */ | ||
export const MAT_MOMENT_DATE_ADAPTER_OPTIONS = new InjectionToken<MatMomentDateAdapterOptions>( | ||
'MAT_MOMENT_DATE_ADAPTER_OPTIONS', { | ||
providedIn: 'root', | ||
factory: MAT_MOMENT_DATE_ADAPTER_OPTIONS_FACTORY | ||
}); | ||
|
||
|
||
/** @docs-private */ | ||
export function MAT_MOMENT_DATE_ADAPTER_OPTIONS_FACTORY(): MatMomentDateAdapterOptions { | ||
return { | ||
useUtc: false | ||
}; | ||
} | ||
|
||
|
||
/** Creates an array and fills it with values. */ | ||
function range<T>(length: number, valueFunction: (index: number) => T): T[] { | ||
|
@@ -48,7 +73,8 @@ export class MomentDateAdapter extends DateAdapter<Moment> { | |
narrowDaysOfWeek: string[] | ||
}; | ||
|
||
constructor(@Optional() @Inject(MAT_DATE_LOCALE) dateLocale: string) { | ||
constructor(@Optional() @Inject(MAT_DATE_LOCALE) dateLocale: string, | ||
@Inject(MAT_MOMENT_DATE_ADAPTER_OPTIONS) private options: MatMomentDateAdapterOptions) { | ||
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. In order to make this not a breaking change for anyone who might be extending the /** @deletion-target 8.0.0 make this param required */
@Optional() @Inject(MAT_MOMENT_DATE_ADAPTER_OPTIONS) private options?: MatMomentDateAdapterOptions |
||
super(); | ||
this.setLocale(dateLocale || moment.locale()); | ||
} | ||
|
@@ -130,7 +156,12 @@ export class MomentDateAdapter extends DateAdapter<Moment> { | |
throw Error(`Invalid date "${date}". Date has to be greater than 0.`); | ||
} | ||
|
||
let result = moment({year, month, date}).locale(this.locale); | ||
let result; | ||
if (this.options.useUtc) { | ||
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.
|
||
result = moment.utc({ year, month, date }).locale(this.locale); | ||
} else { | ||
result = moment({ year, month, date }).locale(this.locale); | ||
} | ||
|
||
// If the result isn't valid, the date must have been out of bounds for this month. | ||
if (!result.isValid()) { | ||
|
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.
I think I found why the tests did not work. Now with the
MAT_MOMENT_DATE_ADAPTER_OPTIONS
in thedeps
all tests pass.