0

I have an Angular App with a C# webapi. I'm trying to schedule jobs at 12pm, once per week for 5 weeks.

The dates are saved in the database in UTC time (and I'm in Melbourne time +11UTC). So as of now, all times would be stored at 1am. The problem is, we go out of daylight saving time soon (+10UTC), so the times stored will be incorrect and displayed to the user as 11am

I think what I have to do is add one hour to the times that are not in daylight saving time. But in C#, how can I determine what the current UTC offset is and what the UTC offset of a date is in the future?

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
CaptainMorgan
  • 1,193
  • 2
  • 25
  • 55
  • Why do you believe the times will be incorrect? .NET is aware of daylight savings, so at the point you convert to UTC it should convert to the correct time relative to daylight savings at that point in time. If the times are incorrect, it's probably much more related to your technique for generating the dates rather than storing them as UTC. Since you haven't provided any code, it's impossible to say. – ProgrammingLlama Mar 27 '20 at 00:58
  • [As you can see](https://rextester.com/GHX23626), .NET is aware of daylight savings. – ProgrammingLlama Mar 27 '20 at 01:01
  • This is a problem : "The dates are saved in the database in UTC time" Can you change it? Dates in the future should be saved as the intended local time, and time zone in a separate field. Time zone rules change several times per year – Joni Mar 27 '20 at 01:05
  • The database doesn't store the timezones. So if I start with a date of 25 March 2020 1:00 AM, and loop 3 times and add 7 days each time, there will be a time change. I was the UI to display 12pm always. Is there any way to do that? – CaptainMorgan Mar 27 '20 at 01:05
  • As a side note, I see that you generally don't accept answers to your questions. You should probably go back and do that. Questions that aren't marked as "accepted" will occasionally be bumped back up, sometimes even years later, since the system deems them as still awaiting answers. – ProgrammingLlama Mar 27 '20 at 01:06
  • Isn't the UI capable of converting UTC to local time correctly? Is it not aware of daylight savings? – ProgrammingLlama Mar 27 '20 at 01:06
  • The UI does convert corrently, but displaying a UTC time for 1am for today, will be displayed as 12pm. On the same screen we are also displaying the UTC time for 1am on April 7, this will be didsplayed as 11am if looking at it today, it needs to be displayed as 12pm – CaptainMorgan Mar 27 '20 at 01:11
  • So your UTC times are wrong then... – ProgrammingLlama Mar 27 '20 at 01:11
  • 2
    @Joni - I understand that the standard way to do it is store UTC and convert to local. Storing local is generally a bad idea. – Enigmativity Mar 27 '20 at 01:13
  • 4
    @enigma Storing utc is fine for past events. It's a disaster for future events. This has been discussed extensively before, see for example https://stackoverflow.com/questions/43558412/how-to-save-future-dates-in-a-database and the articles linked there – Joni Mar 27 '20 at 01:17
  • OP, what I'm saying is this: March 27 12 pm AEDT = March 27 **1 am UTC**. April 5 12 pm AEST = April 5 **2 am UTC**. – ProgrammingLlama Mar 27 '20 at 01:18
  • 1
    @Joni - That's a good argument. It makes a lot of sense. Thanks for the example. – Enigmativity Mar 27 '20 at 01:25
  • UTC is not suitable to store "12pm local time". What is your reason to use UTC for that? – Alexei Levenkov Mar 27 '20 at 01:56
  • 1
    More links - Jon Skeet on the topic - https://codeblog.jonskeet.uk/2019/03/27/storing-utc-is-not-a-silver-bullet/ – Alexei Levenkov Mar 27 '20 at 01:58

0 Answers0