C#C
C#3y ago
Kiel

❔ Converting my project to NodaTime

Hi there, after some conversation in my previous partially related thread #Accounting for Daylight Savings Time skips at runtime, I'm planning on converting existing functionality in my project (a Discord bot) to NodaTime's types instead of the BCL types, to avoid issues that might occur from constantly converting to and from different time-related types.

There are a lot of classes in NodaTime that can be used to represent a "moment in time" so to speak, so I'd like to know which ones I should use for different situations.
Disclaimer: I am using EFCore with PostgreSQL, and will now be using Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime for storing appropriate types.

  • Most of my database entities utilize DateTimeOffset for their creation date and time - always stored in UTC as I believe EFCore cannot store DateTimeOffsets with a non-UTC offset. I'm unsure whether to use OffsetDateTime, ZonedDateTime, or Instant.
  • Many classes implement a timer interface, and utilize a DateTimeOffset for when the timer "expires". See the linked thread for issues related to Daylight Savings Time - I believe I will need to store a LocalTime or Instant and convert it to the user's timezone at runtime (see next point)
  • Users can set their timezone via a command - allowing using https://github.com/robbell/nChronic.Core for natural date parsing in their local time, instead of using UTC for everything. I will likely store their timezone as TimeZoneInfo instead of TimeSpan which I previously used.
Any suggestions for how to handle these types of situations? I'm trying to make sure I can make this transition as clean and bug-free as possible, since I have a lot of logic implemented that relies on the BCL time/date types.
Was this page helpful?