-
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
feat(moment-dateadapter): add option to create utc dates #11336
Conversation
Moment DateAdapter should create dates in utc format to avoid time zone collisions when passing date to servers Closes angular#7167
We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for all the commit author(s) or Co-authors. If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google. |
I added the missing email addresses to my google and github account. CLA should be fine now. |
CLAs look good, thanks! |
Very welcome addition! This will break existing apps which depend on the local date output. |
I initially thought this was doing something similar to what we do in |
@mmalerba, thanks for looking into this, I think however the issue is more complicated. My, and I think a number of others problem is that it parses wrong. you say:
here is what moment js docs says:
So when I click on the date 2018-05-19, the moment object arrives as "2018-01-18T23:00:00Z", i.e. it points to the wrong timestamp.
Very probably yes, but on the other hand I've also seen at least one other workaround similar to the one @Silthus proposes, and I'm going to have to implement something similar as well. Could we have a UTCMomentDateAdapter in addition to the current MomentDateAdapter? Or maybe we could add a flag somehow? |
I see, we could do it as an option probably. We could create injectable a token like |
I still have this on my radar and will try to work on it this week 👍🏻 |
Added MAT_MOMENT_DATE_ADAPTER_OPTIONS with the useUtc: boolean option to parse dates as utc The new option defaults to false and will not cause any breaking changes. Closes angular#7167
Describe how to set the MomentDateAdapter to parse dates as utc. Describe the default behaviour of the MomentDateAdapter. Add an example on how to configure MAT_MOMENT_DATA_ADAPTER_OPTIONS Closes angular#7167
@mmalerba is it ok now like this? |
deps: [MAT_DATE_LOCALE, MAT_MOMENT_DATE_ADAPTER_OPTIONS] | ||
}, | ||
{ | ||
provide: MAT_MOMENT_DATE_ADAPTER_OPTIONS, |
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.
@jelbourn do you think we should use a root or module provider for this?
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.
Probably root, but my tests failed with just the 'root' keyword on the injection token. Not sure how to do that with the new angular 6 system.
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.
Generally anything that doesn't depend on Overlay
should be providedIn: 'root'
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.
You should be able to use MAT_DATE_LOCALE
as an example: https://github.com/angular/material2/blob/master/src/lib/core/datetime/date-adapter.ts#L13
And tests for it here: https://github.com/angular/material2/blob/master/src/lib/core/datetime/native-date-adapter.spec.ts
It looks like you set it up correctly... what error were you getting in the tests?
Only provide MAT_MOMENT_DATE_ADAPTER_OPTIONS in root
@@ -29,10 +29,6 @@ export * from './moment-date-formats'; | |||
provide: DateAdapter, | |||
useClass: MomentDateAdapter, | |||
deps: [MAT_DATE_LOCALE, MAT_MOMENT_DATE_ADAPTER_OPTIONS] |
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 the deps
all tests pass.
Only provide MAT_MOMENT_DATE_ADAPTER_OPTIONS in root
…us/material2 into fix-moment-dateadapter-utc
@mmalerba should all be good now. The travis check failed because of a timeout to the edge browser. |
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.
Looks good, we just need to make the new constructor param optional so its not a breaking change
@@ -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 comment
The 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 MomentDateAdapter
lets make this:
/** @deletion-target 8.0.0 make this param required */
@Optional() @Inject(MAT_MOMENT_DATE_ADAPTER_OPTIONS) private options?: MatMomentDateAdapterOptions
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
this.options && this.options.useUtc
after making the change requested above
@mmalerba done |
@mmalerba care to look into it? This should solve an a real problem for many of us. |
Is it coming in the next release? |
@kuncevic It's ready to submit, but we need to check it against Google's codebase to make sure nothing breaks first |
@mmalerba hi, |
Will this also work with manually typed in dates? If not, what should be used for the parse.dateInput of MAT_DATE_FORMATS? |
I'm pretty sure that the parse function will need replacing with something like:
There could be other places where moment will need replacing with moment.utc and maybe a neater way of replacing them all. Sorry for not submitting a pull request right now. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Moment DateAdapter should create dates in utc format to avoid time zone
collisions when passing date to servers.
To avoid a breaking change a new injection token
MAT_MOMENT_DATE_ADAPTER_OPTIONS
has been created with the optionuseUtc: boolean
. The option defaults tofalse
.See #7167 (comment) for more details on the issue.
Closes #7167