You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
Repository navigation
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
Input fields don't emit (and thus round-trip) offset value when binding against DateTimeOffset fields #6648
Was working on the ASP.NET Core App-building Workshop and discovered an interesting issue when model binding to properties of type DateTimeOffset, especially when the application is running on a machine with a different system locale (and thus default time zone) than the DateTimeOffset values being edited.
Consider a Tag Helper use like <input asp-for="StartTime" />, where StartTime is defined as public DateTimeOffset StartTime { get; set; } on the model. The rendered HTML for the input field will not include the offset value: <input class="form-control" type="datetime-local" id="StartTime" name="StartTime" value="2017-08-14T09:00:00.000" /> . When this is posted back, the model bound value will now have an offset of the machine's system locale, even if the field wasn't changed, resulting in a different value.
We need to consider what the correct behavior of the helpers should be here, and how it interacts with the HTML5 datetime-local field type. Or perhaps we need specific guidance (and maybe helpers/binders) for handling DateTimeOffset values.
type="datetime-local" is incomplete for scenarios involving systems that are in potentially-different time zones. Browsers and the W3C have deprecated the complete choice (type="datetime").
Options include:
The W3C School's recommendation is to include a separate field allowing users to specify the timezone of their choice. This solution should probably be about scaffolding and not MVC itself.
MVC could however expand <input asp-for="{some DateTimeOffset expression}"/> into two elements. One would be the requested <input/> (of type datetime-local) and the other a <select> containing well-known timezones. This would be a breaking change and it would be difficult to style the generated elements. For example, should all attributes in the source <input/> be applied to the <select>? Also, how would less-frequently-used timezones be chosen?
Another choice would be to determine the browser's timezone from request headers. Then, model bind using the correct offset and generate RFC3339-compliant strings for that timezone. I'm not sure this information is consistently available…
Finally, could mostly punt the issue and use type="text" with one of the round-tripping formats e.g. 0:yyyy-MM-ddTHH:mm:ss.fffK instead of auto-selecting datetime-local. type="datetime-local" would still be supported when chosen in the .cshtml. One downside here is end users must be cognizant of their timezone as an offset from UTC. Another is correctly-formatted values aren't particularly usable. (That said, many desktop browsers treat type="datetime-local" as if it were text anyhow.)
Thanks Doug, but given that 'DateTimeOffset' is the generally recommended way to store dates in .NET now, I think we need to think more about a first-class way to support this better. It isn't always about the time-zone the user is in, but just the general proper handling of the data being bound. In this case, you suffer data loss which is terrible. We need to to mitigate that somehow, e.g. render a hidden field with original offset and have the model binder use it in connection with the value in text box when binding. Ideally we need a solution for user selection of the offset too though.
@DamianEdwards your "data loss" is my "incomplete" 😺 (I didn't mention the user's timezone except in 3️⃣)
To clarify, data loss occurs today in two ways: Users can't see what timezone a value is in and model binding can't infer what timezone a user meant. About all that works semi-cleanly is round-tripping when the user does not make a change. I agree a separate field (option 2️⃣ perhaps with an added hidden field or tag helper attributes to control the generated field) avoids the data loss in both directions.
Is there another generally-accepted approach I've missed?
Separately, should MVC generate something different when the user explicitly chooses type="datetime-local" versus when that type is defaulted based on the expression?
Data is lost when the original value has an offset that is different to the server's system locale. That should be fixed as it's clearly a bug in our handling of this end-to-end.
First class support for offset selection is a more advanced matter that we should investigate.
Data is lost when the original value has an offset that is different to the server's system locale.
Yes, the chosen RFC3339 format (yyyy-MM-ddTHH:mm:ss.fff) simply truncates the offset. That breaks even the round-tripping case because the implicit assumption is the offset corresponds to the server's current timezone. Good news is this happens less often because .NET parses even values like 2017/10/01T12:35+7:00 into a DateTimeOffset with the current offset e.g. 2017-09-30T22:35:00.000-07:00 where I'm at.
I'm unsure about offering a partial solution e.g. just providing the hidden field only helps with round-tripping data the server provided.
We can talk in person then. It's simply broken for cross-timezone scenarios, and we have no guidance on allowing selection of time-zone on top of that. We need to do something here.
Looks like this is actually a regression in ASP.NET Core 2.0.0. We currently run in RFC 3339-compliant mode and render DataTimeOffset properties using type="datetime-local" by default. It should take both changes to lose the offset and neither behaviour was the default in MVC 5.x.
This appears to also be a regression from ASP.NET Core 1.1.0 behaviour. We switched to be RFC 3339-compliant by default in in 1.0.0-rc2 (see issue #4102, PR #4250 and 6b369ef). But, the change to use type="datetime-local" didn't happen until 2.0.0-preview1 (see issue #4871, PR #5664 and f95d49c).
I'll test ASP.NET Core 1.1 to confirm the defaults round-trip the offset later today or tomorrow…
I do think it's super important that datetime-local be preserved
Agreed. I'm not going to make any changes undoing a developer's explicit selection of this type. This issue is limited to MVC's auto-selection based on the datatypes.
Activity
Eilon commentedon Aug 14, 2017
@dougbu can you take a look and share thoughts on this?
dougbu commentedon Aug 14, 2017
type="datetime-local"is incomplete for scenarios involving systems that are in potentially-different time zones. Browsers and the W3C have deprecated the complete choice (type="datetime").Options include:
<input asp-for="{some DateTimeOffset expression}"/>into two elements. One would be the requested<input/>(of typedatetime-local) and the other a<select>containing well-known timezones. This would be a breaking change and it would be difficult to style the generated elements. For example, should all attributes in the source<input/>be applied to the<select>? Also, how would less-frequently-used timezones be chosen?type="text"with one of the round-tripping formats e.g.0:yyyy-MM-ddTHH:mm:ss.fffKinstead of auto-selectingdatetime-local.type="datetime-local"would still be supported when chosen in the .cshtml. One downside here is end users must be cognizant of their timezone as an offset from UTC. Another is correctly-formatted values aren't particularly usable. (That said, many desktop browsers treattype="datetime-local"as if it were text anyhow.)DamianEdwards commentedon Aug 14, 2017
Thanks Doug, but given that 'DateTimeOffset' is the generally recommended way to store dates in .NET now, I think we need to think more about a first-class way to support this better. It isn't always about the time-zone the user is in, but just the general proper handling of the data being bound. In this case, you suffer data loss which is terrible. We need to to mitigate that somehow, e.g. render a hidden field with original offset and have the model binder use it in connection with the value in text box when binding. Ideally we need a solution for user selection of the offset too though.
dougbu commentedon Aug 14, 2017
@DamianEdwards your "data loss" is my "incomplete" 😺 (I didn't mention the user's timezone except in 3️⃣)
To clarify, data loss occurs today in two ways: Users can't see what timezone a value is in and model binding can't infer what timezone a user meant. About all that works semi-cleanly is round-tripping when the user does not make a change. I agree a separate field (option 2️⃣ perhaps with an added hidden field or tag helper attributes to control the generated field) avoids the data loss in both directions.
Is there another generally-accepted approach I've missed?
Separately, should MVC generate something different when the user explicitly chooses
type="datetime-local"versus when thattypeis defaulted based on the expression?DamianEdwards commentedon Aug 14, 2017
Data is lost when the original value has an offset that is different to the server's system locale. That should be fixed as it's clearly a bug in our handling of this end-to-end.
First class support for offset selection is a more advanced matter that we should investigate.
dougbu commentedon Aug 14, 2017
Yes, the chosen RFC3339 format (
yyyy-MM-ddTHH:mm:ss.fff) simply truncates the offset. That breaks even the round-tripping case because the implicit assumption is the offset corresponds to the server's current timezone. Good news is this happens less often because .NET parses even values like2017/10/01T12:35+7:00into aDateTimeOffsetwith the current offset e.g.2017-09-30T22:35:00.000-07:00where I'm at.I'm unsure about offering a partial solution e.g. just providing the hidden field only helps with round-tripping data the server provided.
DamianEdwards commentedon Aug 14, 2017
We can talk in person then. It's simply broken for cross-timezone scenarios, and we have no guidance on allowing selection of time-zone on top of that. We need to do something here.
dougbu commentedon Aug 28, 2017
Looks like this is actually a regression in ASP.NET Core 2.0.0. We currently run in RFC 3339-compliant mode and render
DataTimeOffsetproperties usingtype="datetime-local"by default. It should take both changes to lose the offset and neither behaviour was the default in MVC 5.x.This appears to also be a regression from ASP.NET Core 1.1.0 behaviour. We switched to be RFC 3339-compliant by default in in 1.0.0-rc2 (see issue #4102, PR #4250 and 6b369ef). But, the change to use
type="datetime-local"didn't happen until 2.0.0-preview1 (see issue #4871, PR #5664 and f95d49c).I'll test ASP.NET Core 1.1 to confirm the defaults round-trip the offset later today or tomorrow…
dougbu commentedon Aug 29, 2017
Have confirmed problem does /not/ exist in ASP.NET Core 1.1.0.
16 remaining items
dougbu commentedon Sep 2, 2017
Agreed. I'm not going to make any changes undoing a developer's explicit selection of this
type. This issue is limited to MVC's auto-selection based on the datatypes.mattjohnsonpint commentedon Sep 2, 2017
Right, I mean that
datetime-localis currently the auto-selected choice for theDateTimetype, and I think it should remain so.dougbu commentedon Sep 2, 2017
FYI I edited my earlier comment because using
type="text"is actually quite simple.Auto-select `type="text"` for `DateTimeOffset` values
Auto-select `type="text"` for `DateTimeOffset` values
dougbu commentedon Sep 5, 2017
7e4a8fe
scottaddie commentedon Sep 6, 2017
@mj1856 Please let me know if you plan to submit a docs PR for this; otherwise, I'll add it to my list of things to do.
mattjohnsonpint commentedon Sep 7, 2017
@scottaddie - Yep, will do shortly. Thanks.
Auto-select `type="text"` for `DateTimeOffset` values
Add Switch.Microsoft.AspNetCore.Mvc.UseDateTimeTypeForDateTimeOffset …
Auto-select `type="text"` for `DateTimeOffset` values
Add Switch.Microsoft.AspNetCore.Mvc.UseDateTimeTypeForDateTimeOffset …