Skip to content
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

Json.Net is modifying dates with explicit offsets #865

Closed
jetuser opened this issue Mar 31, 2016 · 6 comments
Closed

Json.Net is modifying dates with explicit offsets #865

jetuser opened this issue Mar 31, 2016 · 6 comments

Comments

@jetuser
Copy link

jetuser commented Mar 31, 2016

If a json property has a date specified with an offset, json.net appears to be affecting the actual time without changing the offset. This seems like a bug as if an offset is obviously part of a date string you should respect the time zone or adjust the offset approprioately:

"EndDate": "2016-03-31T12:00:55.2831445-07:00",

When casted to DateTimeOffset turns into:

endDate {3/31/2016 10:00:55 AM -07:00}

Edit: even if this is intentional by date settings, it is somewhat misleading as one would expect offsets are handled automatically by json.net if they are explicit

@oliverjanik
Copy link

Welcome to the club #525 #744. @JamesNK is very stubborn about this one and it causes me extreme pain to deal with.

I understand the compatibility argument, but this behaviour is very surprising and actively harmful. I find it unacceptable.

@JamesNK
Copy link
Owner

JamesNK commented Apr 29, 2016

Use the DateParseHandling.DateTimeOffset setting

@JamesNK JamesNK closed this as completed Apr 29, 2016
@nbevans
Copy link

nbevans commented May 31, 2016

Agreed with @oliverjanik.

This behaviour just caught me out big time. It is not type safe and breaks the principle of least surprise.

If a field contains a DateTime then it will be typed as a DateTime. Not a String. If my String field contains what looks like a DateTime then, by default, it should not be manipulated at all.

@jetuser
Copy link
Author

jetuser commented May 31, 2016

I'm with @nbevans and @oliverjanik in part. If the framework is going to be smart about date time parsing then you need to be completely consistent in handling dates.

In the case of the offset dates, you are violating the data contract implicitly by forcing an offset date into a date time. It should be left up to the caller to decide whether or not they convert to date time or keep it as an offset. The conversion can be handled gracefully both ways if the source is represented correctly, but not when its converted without the knowledge of the caller.

@nbevans
Copy link

nbevans commented May 31, 2016

@jetuser It's just a library, not a framework. If it were a framework then this behaviour MIGHT be acceptable.

@JamesNK
Copy link
Owner

JamesNK commented May 31, 2016

It should be left up to the caller to decide whether or not they convert to date time or keep it as an offset.

Use the DateParseHandling.DateTimeOffset setting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants