Snippet - Simple class to manage daylight saving

Simple class to manage daylight saving

The class automatically calculate which date has to be considered for winter and spring daylight saving, and also give the hour offset to apply regarding to the given date. This can be used as a first approach in fixing system time according to this offset parameter.

The code you posted works for Europe, but not the USA. The US uses a different schedule.

Copied from Daylight Saving Time - When do we change our clocks?

"Most of the United States begins Daylight Saving Time at 2:00 a.m. on the second Sunday in March and reverts to standard time on the first Sunday in November. In the U.S., each time zone switches at a different time.

In the European Union, Summer Time begins and ends at 1:00 a.m. Universal Time (Greenwich Mean Time). It begins the last Sunday in March and ends the last Sunday in October. In the EU, all time zones change at the same moment."

… and I am sure there’s enough variations to the rules around the globe too… hope you planned for us southern-hemispherites :wink:

It’s even worse than what the above would suggest for the USA, since there are both individual states and some cities that have their own rules (Arizona being one of the states that has its own set of rules.

If I were emperor of the world, the first thing I’d do would be to abolish DST entirely, and implement standardized time throughout the world. No offsets, no adjustments, just a single time worldwide.

OK, maybe it wouldn’t be the [em]first[/em] thing I’d do…but it’d definitely be in the top 10. I absolutely [em]loathe[/em] dealing with timezones.

@ devhammer -

Hmmmm. Spend a lot of time thinking about what you will do when you rule the world?

It’s good to have goals, right?

Anyway, it’s something to do when I’m not writing my upcoming best-seller, “Everything I need to know I learned from Pinky and the Brain”. :smiley:

No, don’t abolish DST, abolish that it’s optional, and abolish the ability for individual states/territories/cities/countries from complying with it. Just make the rules simple and standard, world wide.

Seriously, the 2007 energy bill in the US was a great thing - whether it achieves the stated goal of reducing energy consumption, I dunno, but I’m not a statistician to do that follow up research :slight_smile:

I hope you’ll forgive me if I beg to differ. Thanks to Congress’ desire to mess with DST again, I now have a clock that requires re-setting twice a year, and a couple of light timers that are off by an hour for a few weeks because I refuse to take the time to reset them, because it’s a PITA.

Yes, you could argue that the clock and lightswitch makers should’ve provided a way to update the DST tables, but that misses the point.

Anyway, this is fairly far afield of the original topic, so… :wink:

Can I be your Secretary of Time? I’d love to be the one that receives that order. Probably the hardest code I ever wrote was for doing time zone conversions / standardization. I spent 2.5 weeks on it and still didn’t have all the cases worked out. I can’t imagine how many man hours have been wasted worldwide because of the fluctuations in time zones / DST…

If you need global detailed time zone information: good news, you already have it :slight_smile:

In your Windows Registry every detail of almost every timezone is there and is normally up-to-date if you update your Windows regulary.

Maybe you can export a large blob of information from the registry and use this as a resource in the NETMF project.

See Windows time functions: Time Functions - Win32 apps | Microsoft Learn
Look for the EnumDynamicTimeZoneInformation function: EnumDynamicTimeZoneInformation function (timezoneapi.h) - Win32 apps | Microsoft Learn

@ WouterH - Appreciate the tip.

That said, I still want to do away with timezones. It’s just too blasted complicated!

And why won’t these rotten kids stay off my lawn!

It’s certainly easier today than it was a few years ago. SQL Server actually has time functions now that have Timezone info. Kids these days are just spoiled :wink:

Waoh !

I did not think when posting this snippet That DST was so complicated for US !

If could, i would say that for once, something is given easier in Europe than it is in US…but only if
I could of course ! :slight_smile:

Apologies for resurrecting this post, but I wanted to both say thanks to Louis for the initial snippet, and post my modified version with enhancements.

The original version only looked at the date, not the time of the day, and since DST changes at 1am UTC in EU, the calculation was not correct between midnight at 1am both in spring and fall.
Also there was a bug in the method of determining the last Sunday in March/October. It started counting back from Apr 1st and Nov 1st respectively, however if those dates happen to be a Sunday in a given year, then they will return Apr 1 for LastSundayOfMarch or Nov 1 for LastSundayOfOctober.

I hope this will help other people as it’s taken me many hours to get here. Thanks again to Louis for his initial version.

using System;
namespace Hyperbox.UtilitiesLibrary.Treatments
    /// <summary>
    /// Date Time utilities functions
    /// </summary>
    public class DateTimeUtils
        /// <summary>
        /// Return the day that is the last sunday of march
        /// </summary>
        /// <returns>last sunday of march</returns>
        public DateTime LastSundayOfMarch(DateTime refDate)
            // Make reference to last day of March
            DateTime newDate = new DateTime(refDate.Year, 3, 31);
            // Move to the first sunday in the previous month
            while (newDate.DayOfWeek != DayOfWeek.Sunday)
                newDate = newDate.AddDays(-1);
            return newDate.AddHours(1); // add 1 hour to value as DST changes at 1am UTC. Change this to reflect the hour at which DST changes in your TZ.
        /// <summary>
        /// Return the day that is the last sunday of october
        /// </summary>
        /// <returns>last sunday of october</returns>
        public DateTime LastSundayOfOctober(DateTime refDate)
            // Make reference to last day of October
            DateTime newDate = new DateTime(refDate.Year, 10, 31);
            // Move to the first sunday in the previous month
            while (newDate.DayOfWeek != DayOfWeek.Sunday)
                newDate = newDate.AddDays(-1);
		return newDate.AddHours(1); // add 1 hour to value as DST changes at 1am UTC. Change this to reflect the hour at which DST changes in your TZ.
        /// <summary>
        /// Get the Winter Spring Offset to apply
        /// </summary>
        /// <param name="refDate"></param>
        /// <returns></returns>
        public int GetWinterSpringOffset(DateTime refDate)
            if (LastSundayOfMarch(refDate).Ticks <= refDate.Ticks & refDate.Ticks < LastSundayOfOctober(refDate).Ticks)
                return 1;
                return 0;

1 Like