Which is better (dst check function)

I always used similar dst check function like this one:


<code>
long adjustDstEurope(long epoch)
{
  // last sunday of march
  int beginDSTDate= (31 - (5* year(epoch) /4 + 4) % 7);
  int beginDSTMonth=3;
  //last sunday of october
  int endDSTDate= (31 - (5 * year(epoch) /4 + 1) % 7);
  int endDSTMonth=10;
  // DST is valid as:
  if (((month(epoch) > beginDSTMonth) && (month(epoch) < endDSTMonth))
    || ((month(epoch) == beginDSTMonth) && (day(epoch) >= beginDSTDate))
    || ((month(epoch) == endDSTMonth) && (day(epoch) < endDSTDate)))
    return true; // DST europe = GMT +2
  else return false; // nonDST europe = GMT +1
}
</code>

but i never understood the logic of previous code and did not write it myself.
but now i wrote this:

<code>
boolean dst_check(long epoch) {
  //day();             // the day now (1-31)
  //weekday();         // day of the week (1-7), Sunday is day 1
  if (day(epoch)>= 24+weekday(epoch) && month(epoch) == 3) ||
     (month(epoch) > 3 && month(epoch) < 10) ||
     (day(epoch)< 24+weekday(epoch) && month(epoch) == 10 )
      return true; // DST europe = GMT +2
      else return false; // nonDST europe = GMT +1
      
  }
</code>

i want to use smallest code as possible and not extra libs.
Which is best?

Which Arduino board? ESP32 does all of that for you in the background. No effort required on your part.

Depends what you mean by, "best"... :thinking:

IMO, "best" (if possible) is to not use DST at all - do everything in UTC.

Why do you feel you need DST in your Arduino?

Administrations may alter (or abandon) DST on a whim - so no algorithm can be completely reliable.

If you really must do DST on the device, and have a comms link, then signal DST over that link.

It's a esp01.
The DST (European summer/wintertime) is needed for accurate logging and timekeeping events.
Here in Central Europe we go the last Sunday of March and October to +2 instead +1

utc time retrieved by a ntc server every hour.

But is my function not better than the 1st function?
It's hard to test, only once each month :wink: I should change the month number and i can test it betwen next Saturday and Sunday.

Official the DST changes on 02:00 and 03:00 AM. But for the working of the programming has not much of a impact.

The built-in functions mentioned by @gfvalvo also work for esp8266 including esp-01. Very simple to use.

https://werner.rothschopf.net/202011_arduino_esp8266_ntp_en.htm

1 Like

You don't need DST for that - just work in UTC!

You mean NTP?

Indeed it is - that's another good reason not to do it!

See post#9 in this thread.
Change the configTime values to where you live.
The timezone string can be found here. You should pick an NTP server in your country.
Leo..

as already mentioned: use the functionality of the ESP core.
The english page was already posted,
if you need it in German:
ESP8266 NTP Abfrage mit Sommerzeit / Winterzeit (NodeMCU, Wemos D1) (rothschopf.net)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.