Why does ServiceStack emit local time even if the date was UTC in JSON?

In short, a date date worked through ServiceStack.Text. The JSON parser is losing timezone information. Oddly enough, DateTimeSerializerTests.DateTime_Is_Serialized_As_Utc_and_Deserialized_as_local() seems to expect this behavior, and DateTimeSerializer.Prepare() explicitly calls ToLocalTime() for each time object that was parsed as UTC!

Here is a test case example (MSTest, but simple enough to run in all cases). Local passes, but UTC and Unspecified - no, the type returned by the DateTime object is always "Local".

  [TestMethod] public void TestParseSingleDateTime_UTC() { // In canonical UTC format var date = "2014-06-03T14:26:20.0030000Z"; var raw = new DateTime(2014, 6, 3, 14, 26, 20, 3, DateTimeKind.Utc); var value = DateTimeSerializer.ParseShortestXsdDateTime(date); Assert.AreEqual(DateTimeKind.Utc, value.Kind); Assert.AreEqual(raw, value); } [TestMethod] public void TestParseSingleDateTime_Local() { // In local time zone var date = "2014-06-02T11:15:49.1480000-05:00"; var raw = new DateTime(2014, 6, 2, 11, 15, 49, 148, DateTimeKind.Local); var value = DateTimeSerializer.ParseShortestXsdDateTime(date); Assert.AreEqual(DateTimeKind.Local, value.Kind); Assert.AreEqual(raw, value); } [TestMethod] public void TestParseSingleDateTime_Unspecified() { // Unspecified time zone, as we would parse from Excel cells with dates var date = "2012-01-06T00:00:00.0000000"; var raw = new DateTime(2012, 1, 6, 0, 0, 0, DateTimeKind.Unspecified); var value = DateTimeSerializer.ParseShortestXsdDateTime(date); Assert.AreEqual(DateTimeKind.Unspecified, value.Kind); Assert.AreEqual(raw, value); } 

Why is this the default behavior? Using JsConfig.AlwaysUseUtc not a good workaround here, because then I cannot parse the local timestamp as local.

+7
c # servicestack servicestack-text
source share

No one has answered this question yet.

See related questions:

288
Convert UTC / GMT to local time
sixteen
ServiceStack - Is there a way to force all serialized dates to use a specific DateTimeKind?
14
How to convert DateTime of type DateTimeKind.Unspecified to DateTime.Kind.Utc in C # (.NET)
10
Convert DateTime to Utc only if it is no longer Utc
10
UTC for local time conversion for previously saved dates, if the time zone change rules
8
DateTime.ToLocalTime () in winter / summer
2
Getting C # to correctly set the Kind property for the ISO 8601 syntax sequence
one
C # Convert UTC to local time in function dataset
0
Processing Factory Time Zone Data
0
Parsing an ISO string in UTC until DateTimeKind.UTC

All Articles