Json serialization DateTime error

There is an error in deserialization of DateTime, the time is off by ±20 secods and it doesent parse timezone correctly.

  1. This is what the App sends to Device
  2. This is what serializer receives as string
  3. This is what it outputs when deserialized - notice the time difference in seconds
  4. This is where it does it


GitHub issue?

1 Like

Json serialization DateTime error · Issue #1005 · ghi-electronics/TinyCLR-Libraries (github.com)

1 Like

I can take a look at this today - looks like the TZ might not be getting applied correctly and fractional seconds parsing might be messing up the seconds field.

@mcalsyn
This is still not fixed. Or if it is can i get a clarification?
I know that winforms app knows current culture, and tinyclr does not, and its always UTC (is that correct?) so if you set time like below, whats the use of ToLocal, ToUniversal?

                            Time setTime = Json.DeserializeTime(rawData);
                            rtc.SetRtcTime(setTime.DateTime);
                            rtc.SetSystemTime();
                            Debug.WriteLine(setTime.DateTime.ToString());
                            Debug.WriteLine(setTime.DateTime.ToLocalTime().ToString());
                            Debug.WriteLine(setTime.DateTime.ToUniversalTime().ToString());
                            Debug.WriteLine(DateTime.Now.ToString());
                            Debug.WriteLine(DateTime.Now.ToLocalTime().ToString());
                            Debug.WriteLine(DateTime.Now.ToUniversalTime().ToString());

STRING
    2021-04-19T16:47:24.1167125+02:00
COMMA
STRING
    Instruction
COLON
NUMBER
    3
RBRACE
END
04/19/2021 14:43:45 RTC Set Rtc: 04/19/2021 14:47:24
04/19/2021 14:47:24 RTC Set System Time: 04/19/2021 14:47:24
04/19/2021 14:47:24
04/19/2021 14:47:24
04/19/2021 14:47:24
04/19/2021 14:47:24
04/19/2021 14:47:24
04/19/2021 14:47:24

The JSON code is always representing the time as UTC. That is, it is adding the negation of the timezone offset to the time and setting the DateTime.Kind to DateTimeKind.UTC.

Because the DateTimeKind is UTC, ToUniversalTime shouldn’t do anything. ToLocalTime() will only return a local time if you have set a timezone offset with SystemTime.SetTime(DateTime time, int offset). The set functions that you called in your code do not establish a TZ offset.

ok, thanks, will try