-
Notifications
You must be signed in to change notification settings - Fork 9.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
Add support for model binding DateTime as UTC #24893
Conversation
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.
/fyi tests still don't work in other time zones
src/Mvc/Mvc.Core/src/ModelBinding/Binders/DateTimeModelBinder.cs
Outdated
Show resolved
Hide resolved
src/Mvc/Mvc.Core/src/ModelBinding/Binders/DateTimeModelBinder.cs
Outdated
Show resolved
Hide resolved
src/Mvc/Mvc.Core/src/ModelBinding/Binders/DateTimeModelBinder.cs
Outdated
Show resolved
Hide resolved
src/Mvc/Mvc.Core/src/ModelBinding/Binders/DateTimeModelBinderProvider.cs
Outdated
Show resolved
Hide resolved
src/Mvc/Mvc.Core/test/ModelBinding/Binders/DateTimeModelBinderTest.cs
Outdated
Show resolved
Hide resolved
src/Mvc/Mvc.Core/test/ModelBinding/Binders/DateTimeModelBinderTest.cs
Outdated
Show resolved
Hide resolved
src/Mvc/Mvc.Core/test/ModelBinding/Binders/DateTimeModelBinderTest.cs
Outdated
Show resolved
Hide resolved
Missed your latest commit but my other comments stand |
* Cleanup unused exception code path, fix doc comments * Clean up usage of variables * Adjust logging to be consistent
2e97cb8
to
efe27b0
Compare
🆙 📅 |
1 similar comment
🆙 📅 |
Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:
|
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.
LGTM
var type = metadata.UnderlyingOrModelType; | ||
try | ||
{ | ||
|
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.
else | ||
{ | ||
// unreachable | ||
throw new NotSupportedException(); |
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 type is constructable right? So it isn't really unreachable.
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.
Maybe because this uncommon? IDK, removed it because it didn't really add much value.
@@ -46,6 +46,8 @@ public Task BindModelAsync(ModelBindingContext bindingContext) | |||
throw new ArgumentNullException(nameof(bindingContext)); | |||
} | |||
|
|||
_logger.AttemptingToBindModel(bindingContext); |
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.
Do we need move the logging above the work in other binders?
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.
Of the ones that relied on the value provider, Simple type was the only outlier.
@@ -73,7 +72,7 @@ public Task BindModelAsync(ModelBindingContext bindingContext) | |||
} | |||
else if (type == typeof(float)) | |||
{ | |||
model = float.Parse(value, _supportedStyles, culture); | |||
model = float.Parse(value, _supportedStyles, valueProviderResult.Culture); |
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.
Any functional reason for inlining this or just code clarity?
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.
See #24893 (comment)
src/Mvc/Mvc.Core/src/ModelBinding/Binders/DateTimeModelBinder.cs
Outdated
Show resolved
Hide resolved
src/Mvc/Mvc.Core/src/ModelBinding/Binders/DateTimeModelBinder.cs
Outdated
Show resolved
Hide resolved
else | ||
{ | ||
// unreachable | ||
throw new NotSupportedException(); |
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.
Maybe because this uncommon? IDK, removed it because it didn't really add much value.
Hello @pranavkm! Because this pull request has the p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (
|
/// </summary> | ||
public class DateTimeModelBinderProvider : IModelBinderProvider | ||
{ | ||
internal static readonly DateTimeStyles SupportedStyles = DateTimeStyles.AdjustToUniversal | DateTimeStyles.AllowWhiteSpaces; |
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.
Should this be configurable?
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 can create a binder provider that configures the model binder (it's constructable). It's the pattern we use for other types.
I was bitten by this bug today on .NET Core 3.1.403. Is this going to be back-ported to a future 3.1.x release or .NET 5 only? |
@augustoproiete we generally do not backport new features or non-critical bug fixes to earlier releases. You could consider using this PR as a template to copy to your application if you require this to work in your application. On a side note, it's incredibly easy for us to miss comments on closed issues or PR. For the next time, please consider filing a new issue so there's clear visibility. |
Fixes #11584
This change adds a model binder that produces UTC DateTime values by default. Prior to this change, model bound DateTime values used local times. However, this was inconsistent with JSON formatted values which produces DateTime of UTC kinds.
I'll add a migration note as a follow up with a code sample to revert to the previous behavior.