I am running a process at a given clock time each day (in the United States–that is significant). I have been trying to calculate how to do that correctly for the 25 hour day. Consider the following C# code:
var referenceDate = new DateTime(2012, 11, 3);
var referenceTime = new TimeSpan(0, 19, 30, 0);
var originalTime = referenceDate.Add(referenceTime);
var tomorrow = referenceDate.AddDays(1);
var nextTimestamp = string.Format(
"{0} {1:00}:{2:00}",
tomorrow.ToShortDateString(),
referenceTime.Hours,
referenceTime.Minutes);
var nextRunTimestamp = DateTime.Parse(nextTimestamp);
I used the format/parse sequence to make sure that we can avoid time spans when crossing the DST transition time. Now what should be the result of the following expression?
nextRunTimestamp.Subtract(originalTime).TotalHours
Well, it turns out to be 24 but I think it should be 25 since November 4, 2012 is a 25-hour day (transitioning at 2 AM) so the difference between 19:30 on November 3 and 19:30 on November 4 should be 25 hours.
How do I get to the right answer for each date in the year?
Actually it sounds like you don’t need to do that at all. Your problem seems rather moot, since if you have something running at the same time on every day, it will auto adjust to your time change anyways.
If you are really interested though, you can get the DaylightTime of a particular year by utilizing:
The DaylightTime object has your start date and time and end date and time of DST for whatever timezone. From there, if you were so inclined, you could check if DST start or end falls in your time period and add or subtract an hour as needed.
Then in your code you could have logic such as (pseudo code):
EDIT: I think I misunderstood what you were doing. Though the above code can give you if your DST start or end is within your time period. You are looking for this:
I hope you are not puting your DateTime in a string format then trying to do a Parse in your actual code…