#OTOCHECK 2.00 CODE#
So here is some code that will take daylight savings into account: var now = DateTimeOffset.Now When you use the later there is no ambiguity between the first time during the night the wall clock is 2:30 AM and the second time because they have different UTC offsets and the UTC offset is part of the DateTimeOffset structure. To solve the ambiguity you need to switch from using local DateTime to DateTimeOffset. And even worse: if the local timestamp is between 2 AM and 3 AM it is ambiguous. In the same way the autumn night has one more hour in the local time zone so if the timestamp is before 2 AM the time difference until 8 AM local time is 1 hour more than you get by doing simple calculations. The spring night has one less hour in the local time zone so if the timestamp is before 2 AM the time difference until 8 AM local time is 1 hour less than you get by doing simple calculations.
Different countries will have different rules (or no daylight savings at all). At least in the area where I live there is a night in the spring where the clock on the wall will skip from 2 AM to 3 AM and another night in the autumn where the clock will skip from 3 AM and back to 2 AM once.
HoursLeftUntil8.Hours, hoursLeftUntil8.Minutes, hoursLeftUntil8.Seconds) Ĭonsole.WriteLine("It is not between 8PM and 8AM.") Ĭonsole.WriteLine("\nDone!\n\nPress any key to exit.") Īs far as I can see none of the answers provided so far take daylight savings into account. If it's currently before midnight, we subtract the current time from 24:00:00 and add 8 otherwise, we subtract the current time from 8:00:00: Console.WriteLine($"The current time is seconds left until 8AM", If so, then do some TimeSpan math to determine how much time is left until 8AM. First check if the current time is between the start and end times.