Daylight savings problem

This works for me in the UK and currently all EU states.

// returns true or false should DST changed event be sent
// according to the time zone if (0.0(exc. Iceland), +1.0, +2.0) Europe exc. Russia/Belarus
// essentially from the last Sunday in March to the last Sunday in October DST is enabled at UTC 01.00 -> 02.00 and disabled at UTC 02.00 -> 01.00
// automatically begins DST at UTC 01.00 or ends DST at UTC 02.00
bool isDSTactive()
{
    float tz = Time.zone();                                 //offset from UTC
    int dayOfMonth = Time.day();
    int hour = ((Time.now() % 86400) / 3600);
    int month = Time.month();
    int dayOfWeek = Time.weekday() - 1;                     //make Sunday 0 .. Saturday 6
    bool shouldDSTbeEnabled = false;                        //not in European time zones for which coded DST rules apply by default
    if (tz == 0.0 || tz == 1.0 || tz == 2.0)                //European time zones (WET/GMT, CET, EET)
    {
        switch (month)
        {
            case 1:                                         //Jan, Feb, Nov and Dec are definitely standard time
            case 2:
            case 11:
            case 12:
                shouldDSTbeEnabled = false;
                break;
            case 4:                                         //Apr to Sep definitely DST
            case 5:
            case 6:
            case 7:
            case 8:
            case 9:
                shouldDSTbeEnabled = true;
                break;
            case 10:                                        //March and October need deeper examination
            case 3:
                if (!(dayOfMonth - dayOfWeek > 24))         //but before switching Sunday     
                {
                    shouldDSTbeEnabled = (month == 10);     //for October DST will be true, for March not
                }
                else if (dayOfWeek > 0)                     //after the switching Sunday (dayOfWeek !=)
                {
                    shouldDSTbeEnabled = (month == 3);      //for March DST is true, for October not
                }
                else                                        //switching Sunday (dayOfWeek = 0)
                {
                    if (hour >= 1 && month == 3)  shouldDSTbeEnabled = true; //time is 01:00 or later on switching sunday in march then DST should be ON
                    if (hour >= 2 && month == 10) shouldDSTbeEnabled = false; //time is 02:00 or later on switching sunday in october then DST should be OFF
                }
                break;
        }
        //then if a change condition check if switch required from current setting
        if (shouldDSTbeEnabled && !Time.isDST())            //if it should be on but is off
        {
            Time.beginDST();
            param.isDst = true;
            putParameters();
            return true;
        }
        else if (!shouldDSTbeEnabled && Time.isDST())       //if it should be off but is on
        {
            Time.endDST();
            param.isDst = false;
            putParameters();
            return true;
        }
        else
        {
            return false;
        }
    }
    else
    {
        return false;
    }
}
4 Likes