DST AUTOMATIC- client ntp

Hello everyone , I would like to ask if any of you have already made ​​a program client ntp server , which also includes the dst automatically according to your country

Do you mean Daylight Saving Time? The dates and times they change are different depending on the country.

I have code for the U.S.

Where are you and when does your DST change? By your local time on your profile, you are on the other side of the planet from me. Maybe Australia or Mongolia?

I live in Italy .. and change the time on the last weekend of October and the last in March ..

So your time changes on the last Sunday at 1am UTC in October and March? Let me play with that a while.

Here is a test sketch. It is what I use to test time stuff like DST changes. It requires Paul Stoffregen's time library from Github. Download and import it. Thanks to Paul for that great library! :slight_smile:

There are a couple things you need to change to test this. In setup, you can set the start time in the setTime call. That is in the following format:
hour, minute, second, day, month, year

Then you can change from every minute, every hour, or every day. Uncomment the one you want and comment out the others.

You can also change the time zone. That is the timeZone variable in the global variable section. That (+1) should be the correct time zone for Italy. If not, change it.

It updates the time twice per second so you don't have to wait to see if it works.

This is the Italian (and a lot of European countries) version. It changes last Sunday in March and last Sunday in October.

#include <Time.h>

char wkday[7][4] = {"SUN","MON","TUE","WED","THU","FRI","SAT"};

// set time zone for standard time
long timeZone = +1;

unsigned long testEpoch;

void setup() 
{
  Serial.begin(115200);

  Serial.println(F("Ready"));

// Set test time here. UTC/GMT
// hour, minutes, seconds, day, month, year
  setTime(1,0,0,24,3,2015);

// Get time
  testEpoch = now();
  
}

void loop() {

// This checks once a day for long range testing
//  testEpoch += 3600UL * 24UL;

// This checks every hour for the switch to and from DST
  testEpoch += 3600UL;

// This checks every minute for the switch to and from DST
  testEpoch += 60UL;

  printClock(testEpoch);
  delay(500);
}

void printClock(unsigned long thisEpoch) {
  bool dst = false;
  time_t t = thisEpoch + (timeZone * 3600UL);

  int thisMonth = month(t);
  int thisDay = day(t);
  int thisWeekday = weekday(t);
  int thisHour = hour(t);
  int thisMinute = minute(t);
  
  if(thisMonth == 10 && thisDay < (thisWeekday + 24))
  {
    dst=true;
  }

  if(thisMonth == 10 && thisDay > 24 && thisWeekday == 1 && thisHour < 2)
  {
    dst=true;
  }

  if(thisMonth < 10 && thisMonth > 3) dst = true;
  
  if(thisMonth == 3 && thisDay > 24 && thisDay >= (thisWeekday + 24))
  {
    if(!(thisWeekday == 1 && thisHour < 2)) dst = true;
  }
  
  // if dst, add one hour
  if(dst) t += 3600UL; 
  
  Serial.print(wkday[weekday(t) - 1]);
  Serial.print(" "); 

  if(hour(t) < 10) Serial.print(" ");
  Serial.print(hour(t));
  Serial.print(':');  
  // In the first 10 minutes of each hour, we'll want a leading '0'
  if ( minute(t) < 10 ) Serial.print('0');
  Serial.print(minute(t));
  Serial.print(" ");
  Serial.print(month(t));
  Serial.print("-");
  Serial.print(day(t));
  Serial.print("-");
  Serial.print(year(t));
  // if DST, print it
  if(dst) Serial.println(" DST");
  else Serial.println();
}

Once you determine it works, move the printClock function to your NTP sketch. If you need one, I have an old sketch I wrote a couple years ago. It uses this function to compute U.S. DST.

edit: Changed one small section of the code to correct a bug. It didn't exit DST at the right hour.

Here is the U.S. version of the printClock function. It changes second Sunday in March and first Sunday in November.

void printClock(unsigned long thisEpoch) {
  bool dst = false;
  time_t t = thisEpoch + (timeZone * 3600UL);

  int thisYear = year(t);
  int thisMonth = month(t);
  int thisDay = day(t);
  int thisWeekday = weekday(t);
  int thisHour = hour(t);
  int thisMinute = minute(t);
  
  if(thisMonth == 11 && thisDay < 8 && thisDay < thisWeekday)
  {
    dst=true;
  }
  
  if(thisMonth == 11 && thisDay < 8 && thisWeekday == 1 && thisHour < 1)
  {
    dst=true;
  }

  if(thisMonth < 11 && thisMonth > 3) dst = true;
  
  if(thisMonth == 3 && thisDay > 7 && thisDay >= (thisWeekday + 7))
  {
    if(!(thisWeekday == 1 && thisHour < 2)) dst = true;
  }
  
  if(dst) t += 3600UL; 

  Serial.print(wkday[weekday(t) - 1]);
  Serial.print(" "); 

  if(hour(t) < 10) Serial.print(" ");
  Serial.print(hour(t));
  Serial.print(':');  
  // In the first 10 minutes of each hour, we'll want a leading '0'
  if ( minute(t) < 10 ) Serial.print('0');
  Serial.print(minute(t));
  Serial.print(" ");
  Serial.print(month(t));
  Serial.print("-");
  Serial.print(day(t));
  Serial.print("-");
  Serial.print(year(t));
  // If DST, print it
  if(dst) Serial.println(" DST");
  else Serial.println();
}