Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialization of DateTime loses Kind and TimeZone #273

Open
MikaelUmaN opened this issue Oct 12, 2021 · 5 comments
Open

Serialization of DateTime loses Kind and TimeZone #273

MikaelUmaN opened this issue Oct 12, 2021 · 5 comments

Comments

@MikaelUmaN
Copy link

Hi.

This should be related to: Zaid-Ajaj/Fable.SimpleJson#21 and #108

Setup

  • dotnet new SAFE
  • Create API accepting DateTime parameter
  • From client, call server api with DateTime.Today (or DateTime.UtcNow.Date, ...)

Expected

  • DateTime instance is the same on client and server.

Actual

  • DateTime on client can be Kind=UTC (or Unspecified) but is always Local on the server.
  • DateTime has Time component 00:00 (because we explicitly use the Date part), but when deserializing it becomes e.g. 2021-10-11 22:00 in my container.

Why this is a problem for me
Basically I am on dotnet 5 and eagerly awaiting the "DateOnly" struct.

As it is now, I expect to be able to send string(myDateParameter) to an external http API on the server-side but instead of todays date (e.g. 2021-10-12) I get yesterdays.

Suggestions
Probably it's not possible to keep the "Kind" type across serialisation, but then I think it should explicitly call "ToUniversalTime" prior to serialization and explicitly read it as Kind=UTC on the server side when deserializing.

The the application layer can handle the rest.

@Zaid-Ajaj
Copy link
Owner

Hi @MikaelUmaN,

In order not to lose the time zone information it is recommended to use DateTimeOffset which preserves the timezone offset of the server and propagates it to the client (that is represented by Date).

As for the Kind, I believe we had that implemented before but I need to double check

@MikaelUmaN
Copy link
Author

Hi. Thanks for feedback.

I can confirm that DateTimeOffset works as expected, except it loses "kind" information. It is set to Unspecified.

I still think it would be nice to have DateTime working as well, though. Note that I am just trying to pass dates and not actually times; so it's doubly confusing that the server-side receives another date when transferring a DateTime.

The cleanest solution I think is to do it the way MessagePack does it; and just always rely on UTC when transferring; for example by going to ToUniversalTime and using Ticks.

From my side I will use DateTimeOffset as a workaround until DateOnly arrives in net 6.0 GA. Thanks for an excellent library.

@kerams
Copy link
Collaborator

kerams commented Oct 14, 2021

DateTimeOffset does not have any explicit Kind. If the offset is 0, you know it's UTC. Otherwise it's local.

until DateOnly arrives in net 6.0 GA

Yes, I'm looking forward to it too. However, it needs to be supported by Fable as well, and only then can it be implemented in SimpleJson and MsgPack.

@MikaelUmaN
Copy link
Author

Yes you are right of course, it does not have Kind. Sorry my mistake, it was because I converted it myself and looked at it.

The comment on DateTime still stands though. However, for my purposes you can close this issue in case it's not prioritized to fix.

@Zaid-Ajaj
Copy link
Owner

I think we can resolve this issue by documenting the current behavior and recommending people to use DateTimeOffset when the timezone is relevant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants