I encountered an issue while materializing a date/time value as DateTimeOffset.
Please see https://stackoverflow.com/questions/50628374 where I initially asked for help.
public class RootObject
{
[JsonProperty("revisedDate", NullValueHandling = NullValueHandling.Ignore)]
public DateTimeOffset? RevisedDate { get; set; }
}
{
"revisedDate": "0001-01-01T00:00:00"
}
In the abscence of a timezone, I expected Json.NET to in this case deserialize the value and return a valid DateTimeOffset equal to DateTimeOffset.MinValue.
An exception was thrown in the DateTimeOffset.Parse method, and Json.NET reported "Could not convert string to DateTimeOffset: 0001-01-01T00:00:00. Path 'resource.revisedDate', line 20, position 44." to the caller.
Please refer to the answer provided by dbc for steps to reproduce the issue:
https://stackoverflow.com/a/50631270/1503584
I also met this issue
Looking for solution to this issue too.
Having the same issue, the workaround seems really hacky in my opinion :/
Also having this problem with an android app using xamarin.
It was a pain to find out the problem, cause i'm in UTC+0, while the clients are in UTC+n
Thanks a lot for solution!
This issue isn't limited to Json.NET, Microsoft's System.Text.Json does also raise an exception.
This is because DateTimeOffset values are converted to UTC internally, and when the value "0001-01-01T00:00:00" is deserialized to DateTimeOffset, it will use the local timezone. When your timezone offset is positive, it results in the value "0001-01-01T00:00:00+01:00" which is less than DateTimeOffset.MinValue when converted to UTC and thus an invalid DateTimeOffset value.
https://docs.microsoft.com/en-us/dotnet/api/system.datetimeoffset.minvalue:
Any DateTimeOffset value is converted to Coordinated Universal Time (UTC) before the method performs the comparison with MinValue. This means that a DateTimeOffset value whose date and time are close to the minimum range, but whose offset is positive, may throw an exception. For example, the value 1/1/0001 1:00:00 AM +02:00 is out of range because it is one hour earlier than MinValue when it is converted to UTC.
I have found no fix for this other than to write a custom (and complex) JsonConverter to handle the invalid DateTimeOffset values.
I also have this issue. (Thanks for the explanation and work around)
+1
this needs to be solved to avoid ugly hacks
Most helpful comment
I also met this issue