jskeet on master
Document ZoneInterval.Start beh… (compare)
Hey does anyone know if NodaTime Instant serialises/deserialises correctly when using Json.net and ASP.NET Core 3.0 Preview 9? I'm not able to get things to work at all when using the ConfigureForNodatime extension
services.AddMvc().AddNewtonsoftJson((options) => options.SerializerSettings.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb));
and if I just ignore adding the extension I just get 1 January 1970 as the Instant values - looking at Fiddler, the serialisation for the field is empty. Interestingly, Duration does serialise correctly in the same object over the same Http Get.
I'm calling the controller from a Blazor client side app if that makes a differenc too?
so should we store a OffsetDateTime + TimeZone anyways?
For which? You have two different things there.
For a logged event, the timezone is entirely irrelevant. You shouldn't be dealing with future notifications from this. Use
For a future event, humans usually like to schedule things in terms of their local timezone, and most often want to keep the local time if the zone rules change. Use
Persisting to a database is a little more interesting. Assuming SQL Server...
You can usually get away with just a
Instant, but if you do be explicit (ie, via column name) that the value is absolute/UTC. That is, consider the timezone the "unit" of the timestamp. You can also use custom types to more directly create a "raw" value (say, seconds+nanoseconds).
For future dates, generally you're going to want to use a
DATETIMEOFFSET with a string for the timezone id. I recommend you ignore the timezone functions in the database, and perform all timezone-related math in the application layer:
That last point should also make you cautious when dealing with distributed applications and cloud hosting: your application may be updated unevenly, and you may not even be in control of the system ruleset (packaging it with your application may help). Note you will have to be prepared for mismatches between the stored offset and the "correct" offset from the ruleset.
Ah, I meant more that it's the correct domain type to use server side.
From the look of it, yes, you'd need a custom format string on the client side -
fromFormatshould be all you need to be able to match the default output from NodaTime. (
fromISO probably doesn't do what you want, given the examples given).
Note that this does mean you potentially have two different rulesets in use at a single time.
2019-11-08T09:43:52 Australia/Perth (+08)rather than what was stated on the serialize page
2019-11-08T09:59:52+08:00 Australia/Perth. If someone else is using Luxon and comes across this, the correct pattern to use is
services.AddMvcCore().AddJsonOptions(...). But I can't see any extension method for
JsonOptionsin NodaTime.Serialization.JsonNet? (https://github.com/nodatime/nodatime.serialization/blob/master/src/NodaTime.Serialization.JsonNet/Extensions.cs). What am I missing?