29

I am storing schedules in the database as a day of the week, hour and minute. When the data is read we create a DateTime object for the next occurrence of that day, hour and minute, but I need to modify this to be DST-aware. I am able to modify the database if necessary.

I know that DateTimeOffset stores a UTC date/time and an offset. I also know from this MSDN blog entry that DateTimeOffset should be used to "Work with daylight saving times".

What I'm struggling to understand is exactly how DateTimeOffset "work(s) with daylight saving times". My understanding, little that there is, is that daylight saving times are a political decision and cannot be inferred from purely an offset. How can it be that this structure is DST friendly if it only stores an offset and not a named timezone or country?

4
  • 3
    What if the "next" day and time fall into the gap during a DST transition? I.e. that time never occurs on that day, since the time jumps ahead by a whole hour. I don't think there's a magic solution that can just be plugged in. Sep 26, 2011 at 10:59
  • 1
    @Damien_The_Unbeliever makes a good point, and the reverse should be considered too - a local time can occur twice on the same day. Noda Time forces you to consider these options when you convert from LocalDateTime to ZonedDateTime; .NET doesn't :(
    – Jon Skeet
    Sep 26, 2011 at 11:00
  • 2
    Job is mostly done and I'm testing it now - Noda-Time was the magic solution as it tells me all about skipped times, ambiguities in mapping etc :) Nov 10, 2011 at 18:45
  • In the database I store the day of week, hour, minute and timezone. The scheduler calculates the local time when the event should next fire using this data and Noda-Time. If there are unresolved ambiguities coming up it emails me. Nov 2, 2014 at 15:36

1 Answer 1

43

DateTimeOffset itself isn't really DST-aware, but TimeZoneInfo is. A DateTimeOffset represents a fixed instant in time - so you get to a DateTimeOffset via something that is time zone aware. In other words, if I asked for a DateTimeOffset now in the UK, I'd end up with something with an offset of +1 hour from UTC. If I asked for a DateTimeOffset for some time in December in the UK, I'd end up with something with an offset of 0 hours from UTC.

If you change your database to include the offset and you create the DateTimeOffset from the user's chosen DateTime (which should be of kind "unspecified") and their time zone, then that should give you the correct offset taking DST into account.

One thing to be aware of though: if I schedule something now for "2 years time" and you determine the offset now, that offset may not be correct in the future - for example, the government could change when DST applies, and obviously that's not going to change what's stored in your database.

8
  • 1
    @ Jon Is Noda Time ready for production use in a scenario like this? Sep 26, 2011 at 11:05
  • @StephenKennedy: I don't know of any problems around time zone calculations etc - the bulk of the remaining pre-1.0 work is around text formatting/parsing. Given that we haven't hit 1.0, I would be cautious about adopting it - if I were in your shoes I'd want a lot of tests for your specific use case - but if you want any support on using Noda Time, I'd be very happy to help, and I'm sure other members of the mailing list would be too.
    – Jon Skeet
    Sep 26, 2011 at 11:34
  • 1
    @StephenKennedy: Doing everything in UTC should be fine - basically you should be storing something which corresponds to a single UTC time, whether that's a DateTimeOffset or whatever. I'd be really interested to hear your thoughts about Noda Time though - please mail me about it.
    – Jon Skeet
    Sep 26, 2011 at 18:29
  • 1
    @Damien_The_Unbeliever Jon's answer was spot on, but we moved on a lot from there. I implemented the calculations using Noda-Time, along with ambiguity handling to address the issues raised by Damien, and then mapped the result to a DateTimeOffset and fed that into the scheduler. Am very impressed with the ease with which this was done and happy with the preliminary results. Nov 10, 2011 at 18:52
  • 1
    @Triynko: It sounds like this should all be in a new question, to be honest.
    – Jon Skeet
    Feb 18, 2016 at 21:04

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.