Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
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

Closed

Description

@DamianEdwards

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.

@rynowak @Eilon

Activity

  1. added this to the 2.1.0 milestone on Aug 14, 2017
  2. Eilon commented on Aug 14, 2017

    @Eilon
    Contributor

    @dougbu can you take a look and share thoughts on this?

  3. dougbu commented on Aug 14, 2017

    @dougbu
    Contributor

    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:

    1. 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.
    2. 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?
    3. 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…
    4. 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.)
  4. DamianEdwards commented on Aug 14, 2017

    @DamianEdwards
    MemberAuthor

    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.

  5. dougbu commented on Aug 14, 2017

    @dougbu
    Contributor

    @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?

  6. DamianEdwards commented on Aug 14, 2017

    @DamianEdwards
    MemberAuthor

    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.

  7. dougbu commented on Aug 14, 2017

    @dougbu
    Contributor

    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.

  8. DamianEdwards commented on Aug 14, 2017

    @DamianEdwards
    MemberAuthor

    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.

  9. dougbu commented on Aug 28, 2017

    @dougbu
    Contributor

    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…

  10. dougbu commented on Aug 29, 2017

    @dougbu
    Contributor

    Have confirmed problem does /not/ exist in ASP.NET Core 1.1.0.

  11. 16 remaining items

  12. dougbu commented on Sep 2, 2017

    @dougbu
    Contributor

    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.

  13. mattjohnsonpint commented on Sep 2, 2017

    @mattjohnsonpint

    Right, I mean that datetime-local is currently the auto-selected choice for the DateTime type, and I think it should remain so.

  14. dougbu commented on Sep 2, 2017

    @dougbu
    Contributor

    FYI I edited my earlier comment because using type="text" is actually quite simple.

    The ASP.NET Core 2.0.x patch may use type="datetime" again if that temporary fix is simpler.

  15. dougbu commented on Sep 5, 2017

    @dougbu
    Contributor
  16. scottaddie commented on Sep 6, 2017

    @scottaddie
    Member

    @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.

  17. mattjohnsonpint commented on Sep 7, 2017

    @mattjohnsonpint

    @scottaddie - Yep, will do shortly. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions