Web Connection
JSONSerializer Datetime UTC Conversion Issue
Gravatar is a globally recognized avatar based on your email address. JSONSerializer Datetime UTC Conversion Issue
  Rob Corbett
  All
  Nov 7, 2017 @ 05:14pm

Rick,

I'm not sure what's going on, but I ran the following code today and my times are skewed:

DO wwJsonSerializer  && Load Libs
goJSON = CREATEOBJECT("wwJsonSerializer")
loResult = CREATEOBJECT("empty")
ltTime = {09/07/2017 13:16:50}
ADDPROPERTY(loResult, "Time", ltTime)
?loResult.Time
lcJSON = goJSON.Serialize(loResult)
?lcJSON
loResult = goJSON.DeserializeJSON(lcJSON)
?loResult.Time

This was the output:

09/07/2017 13:16:50

{"time": "2017-09-07T18:16:50Z"}

09/07/2017 14:16:50

I'm in the Eastern Time Zone (UTC-05:00). The first value is correct by identity. The second value is off by -1 hour (should add +6 hours for timezone + daylight savings), the third value is off by +1 hour. My thoughts are that Web Connection is deciding whether or not to add an extra 60 minutes (for Daylight Savings) to datetime values based on the current time, when it should be based on the value being converted. I have no idea why the Newtonsoft.json.dll is only subtracting 4 hours to get me back to Local Time. Do you know? This test was performed on the latest Web Connection V6.17.

Gravatar is a globally recognized avatar based on your email address. re: JSONSerializer Datetime UTC Conversion Issue
  Rick Strahl
  Rob Corbett
  Nov 7, 2017 @ 09:54pm

Hmmm...

I'm pretty sure JSON.NET does this correctly. Quick check confirms:

Which is roughly the code that runs to deserialize anyway (WWWC serializes manually).

The problem here is that the serializer (Fox code) looks at current TimeZone offset in your timezone, not the timezone offset for the specific date, which effectively makes the date serialized not the correct UTC date if the Daylight savings time is different.

I think the way to solve this is to call into .NET and do the UTC conversion and date serialization there.

Surprised nobody has run into this before - this is a pretty major bug.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: JSONSerializer Datetime UTC Conversion Issue
  Rick Strahl
  Rob Corbett
  Nov 7, 2017 @ 10:17pm

I've pushed up a fix that should solve the problem.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: JSONSerializer Datetime UTC Conversion Issue
  Rob Corbett
  Rick Strahl
  Nov 7, 2017 @ 10:59pm

You're right about Newtonsoft being correct; I thought I needed to add an extra hour to the offset for Daylight savings, but it needs subtracted from instead. Thanks for the quick fix anyway.

I tested the new code and it works, but you will need to reinstate DTOT(), the check for EMPTY(), and do something about AssumeUtcDates:

FUNCTION WriteDate(lvValue)

IF VARTYPE(lvValue) = "D"
	lvValue = DTOT(lvValue)
ENDIF	
IF EMPTY(lvValue)
	lvValue = {^1970-1-1 :}
ENDIF
lcDate = JsonDate(lvValue, .F.)
this.cOutput = this.cOutput +  " " + lcDate

RETURN
Gravatar is a globally recognized avatar based on your email address. re: JSONSerializer Datetime UTC Conversion Issue
  Rick Strahl
  Rob Corbett
  Nov 8, 2017 @ 11:34am

Thanks Rob. This should do it:

************************************************************************
* wwJsonSerializer ::  WriteDate
****************************************
***  Function: Turns a date into an ISO formatted date value string
***    Assume: Input dates are assumed to be local dates
***            If you have UTC dates to start you'll need to convert
***            them to local dates first
***      Pass: Date Value
***    Return: Json Date 
************************************************************************
FUNCTION WriteDate(lvValue)

IF VARTYPE(lvValue) = "D"
	lvValue = DTOT(lvValue)
ENDIF	
IF EMPTY(lvValue)
	lvValue = {^1970-1-1 :}
ENDIF

this.cOutput = this.cOutput +  " " + JsonDate(lvValue, this.AssumeUtcDates)
RETURN

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: JSONSerializer Datetime UTC Conversion Issue
  Rob Corbett
  Rick Strahl
  Nov 8, 2017 @ 12:18pm

Ok, great...thanks Rick.

© 1996-2024