Web Connection
Problem with date values in REST response.
Gravatar is a globally recognized avatar based on your email address. Problem with date values in REST response.
  Michele
  All
  Apr 20, 2022 @ 07:41am

My REST app makes a query and the result is this but when i receive data in json format on client side i have this result where date values are different from those in my cursor.... One day less.... Why ? Thanks

Gravatar is a globally recognized avatar based on your email address. re: Problem with date values in REST response.
  Michele
  Michele
  Apr 20, 2022 @ 08:40am

I found AssumeUtcDates property.... But if i want to change it i need to change directly in wwjsonserializer.prg ? or not ?

Gravatar is a globally recognized avatar based on your email address. re: Problem with date values in REST response.
  Tore Bleken
  Michele
  Apr 20, 2022 @ 08:52am

It's a property of the class, change it in the instantiated object.

loSer = CREATEOBJECT("wwJsonSerializer")
loSer.AssumeUtcDate = .T.

Gravatar is a globally recognized avatar based on your email address. re: Problem with date values in REST response.
  Rick Strahl
  Michele
  Apr 20, 2022 @ 01:21pm

You really should not mess with the date format in the JSON. By default the date will be in UTC format and will get adjusted when you deserialize. IOW, if you do two way serialization the date in and out will be the same.

DO wwJsonSerializer

ldDate = DATETIME()
? ldDate       && 04/20/2022 10:15:45 AM

loSer = CREATEOBJECT("wwJsonSerializer")
lcJson = loSer.Serialize(DATETIME())
? lcJson       && "2022-04-20T20:15:45Z"

ldDate2 = loSer.Deserialize(lcJson)
? ldDate2     && 04/20/2022 10:15:45 AM

ASSERT(ldDate2 == ldDate)

You don't want the date to be in local date format in the JSON. because then every client will have to explicitly know that the data is in some specific date format and has to be calculate the date offset if dates are deserialized in a different time zone.

Not recommended.

The only reason you should use the lAssumeUtcDates flag is to deserialize JSON that has dates that are not UTC in the first place (which should be rare).

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Problem with date values in REST response.
  Michele
  Rick Strahl
  May 26, 2022 @ 06:24am

I have similar problem when receiving data in json....i send this value in my json "p_dtin": "2022-01-01T23:00:00"

and when i look at loJson.p_dtin value in rest service i have {02-01-2022}

Exactly one hour after

Gravatar is a globally recognized avatar based on your email address. re: Problem with date values in REST response.
  Rick Strahl
  Michele
  May 26, 2022 @ 11:46am

Sounds that's the difference of your time zone. Date is in UTC and will convert back to local time when you deserialize.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Problem with date values in REST response.
  Michele
  Rick Strahl
  May 26, 2022 @ 02:19pm

Ok, but how can i have the correct date in my test? I need to store it with correct value, not in UTC...

Gravatar is a globally recognized avatar based on your email address. re: Problem with date values in REST response.
  Rick Strahl
  Michele
  May 26, 2022 @ 02:49pm

I don't understand your question. If you receive the data in JavaScript it'll be adjusted to local time by the JSON Serialization. If you use it in FoxPro wwJsonSerializer will give you the local date.

You shouldn't look at the date in JSON because that's not what you'll get when the data deserializes (unless you're on Greenwich Mean Time)

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Problem with date values in REST response.
  Michele
  Rick Strahl
  May 27, 2022 @ 09:05am

I send a date with a json, for example 12-31-2022 23:30 In my rest i receive 01-01-2023 and i store it my table... If i access the table from a BI directly without rest i have an uncorrect date. Not the same i sent at the beginning....

Gravatar is a globally recognized avatar based on your email address. re: Problem with date values in REST response.
  Rick Strahl
  Michele
  May 27, 2022 @ 09:44am

If that's the case your JSON client is not doing the right thing in deserializing the data.

Here's my point, to demonstrate, which is two-way conversion:

DO wwJsonSerializer

ltDate = DATETIME()
? ltDate

loSer = CREATEOBJECT("wwJsonSerializer")
lcJson = loSer.Serialize(ltDate)
? lcJson

dtDate = loSer.DeSerialize(lcJson)

? dtDate

Here's the output both in FoxPro, and also in .NET (to demonstrate that this is universal):

+++ Rick ---

© 1996-2024