Post topics, source code that relate to the Arduino Platform

User avatar
By katz
#26669 Hello all,

Thanks martinayotte, I got it to work!

Below I have included a full working code. It sets time to a moment just two minutes before DST is switched ON. If you wait for two minutes, you can actually see it happen.

To get it working in an ESP8266, you have to modify the TimeZone library. In particular, in the file 'Timezone.cpp' you have to comment-out lines 12, 28 - 31 and 184 - 200. That will take care of the EEPROM calls that are not (yet) supported by the ESP8266.

Enjoy, Peter.

Code: Select all#include <Time.h>        //http://www.arduino.cc/playground/Code/Time
#include <Timezone.h>    //https://github.com/JChristensen/Timezone

TimeChangeRule STon={"STon",Last,Sun,Mar,2,120};   // Central European Summer Time on
TimeChangeRule SToff={"SToff",Last,Sun,Oct,3,60};  // Central European Summer Time off
Timezone myTZ(STon,SToff);
TimeChangeRule *tcr;                               // pointer to TimeChangeRule, use it to get tzName
time_t utc,local;

void setup(){
    Serial.begin(115200);
    //setTime(01,55,00,11,3,2012);    // manually set the time (hr,min,sec,day,mnth,yr)
    setTime(0,58,0,29,3,2015);        // Set time to GMT-time just before change to summer time
    //setTime(0,58,0,25,10,2015);     // Set time to GMT-time just before change to winter time
}

void loop(){
  utc=now();
  Serial.print(getTimeDate(utc,"GMT"));
  Serial.print(" / ");
  local = myTZ.toLocal(utc,&tcr);
  Serial.print(getTimeDate(local,tcr->abbrev));
  Serial.println();
  delay(1000);
}

String getTimeDate(time_t t, const char *tzName){
  String TimeDate="";
  byte hh,mm,ss,MM,dd,wd;
  int yyyy;
 
  hh=hour(t);
  mm=minute(t);
  ss=second(t);
  wd=weekday(t);
  dd=day(t);
  MM=month(t);
  yyyy=year(t);
  if(hh<10)TimeDate+='0';
  TimeDate+=(String)hh+':';
  if(mm<10)TimeDate+='0';
  TimeDate+=(String)mm+':';
  if(ss<10)TimeDate+='0';
  TimeDate+=(String)ss+' ';
  TimeDate+=(String)dayShortStr(wd)+", ";
  if(dd<10)TimeDate+='0';
  TimeDate+=(String)dd+'-';
  TimeDate+=(String)monthShortStr(MM)+'-';
  TimeDate+=(String)yyyy+' ';
  TimeDate+=tzName;
  return TimeDate;
}