core
core copied to clipboard
Add support for DateTimeKind
Currently DateTime only works as local time on the client, both its constructor and properties. (If using only the normal .NET methods, the .JS extension property converts it to a JavaScript Date so further options are available).
But actually it is represented as an UTC timestamp, so it can be sent to the server, where it is deserialized to an UTC date.
This covers some common use cases but not all:
- When used only in client side context,
DateTime.Nowand construction from Y/M/D... behaves as expected - When sending timestamps to the client, server has uniform UTC dates, independent of where the clients is located
- When returning UTC timestamps, the exact time point will arrive on the client, although there it will behave like local time.
Missing features:
- Constructing and handling UTC timestamps on the client without
WebSharper.JavaScripttrickery - Sending to server or returning a DateTime with an unspecified, no conversions, having exact Y/M/D... on both sides.
Proposal
Add support for constructors using DateTimeKind and modify client-side representation to include it. Remoting could work like this:
- DateTime values with Kind=Utc/Uspecified can be used for exact round-tripping.
- DateTime values with Kind=Local are converted to Utc when de/serializing. The client and server can be in different time zones so this keeps the exact time point while sacrificing representation - DateTimeOffset can be used for that.
- DateTime constructors without explicit DateTimeKind create instance with Kind=Unspecified, as in .NET
- The
.JSproperty converts Local timestamps to Utc with the help ofgetTimezoneOffsetand creates theDateobject from that. Unspecified DateTimes will be treated as local too for this to keep some back-compatibility.
This is a breaking change as client-side representation and remoting contract changes so this must come with a WebSharper version bump.
Earlier tickets without full solution: #636 #955 #1088
#1080 can be traced back to this issue as well