Daylight Savings Time

Greetings,

I apologize in advance, but I have no idea how this coding works. I made a Back to the Future Clock and the coding is for Daylight Savings Time in the United States. The code would require me to change the code for the date to automatically change the clock. I read online that there is coding to reach a time server that would change it automatically. Does that exist? If so, would anyone be kind enough to write the code in the current program that I have? Since I am a new user, it would not let me upload the file. Any help would be appreciated.

Hello @workingforaliving ,

Post your formatted code here using the <CODE> button... and you will get some/many replies for assistance for Daylight Saving time code.

All daylight savings time in the US is set state by state. Some have it, some don’t have it. That can change at any time. My state has been debating removing DST for quite a while.

My suggestion would be to use the DeLorean to travel back to the 1800s and forget about Daylight Saving Time. :grinning_face:

1 Like

Which board are you using, and does it have WiFi/network access? If you don't currently have that, then using a time server would require extensive modification of the code as well as to the hardware.

One method is to set the clock to UTC, and then calculate the local time as an offset from that, based on the current date/time. There are libraries to handle this automatically, such as the Timezone library by J Christensen.

It will not let me upload a file. It is saying I am new user. I will upload the file once this has passed. Hopefully it will be sooner than later.

What is "it"?

Please, copy the error you receive when you try to upload, and paste that error here, using the <CODE> button in the Reply box.

Post your formatted code (second request).

While you are doing that...

  • Verify you are using a DATA cable for the USB.

  • Verify you have selected your board and port in the IDE
    IDE >> TOOLS >> BOARD >> your board
    IDE >> TOOLS >> PORT >> your port.

The code file. I would rather upload the file than the post the whole code. I am not getting any error message. I would rather have someone rewrite the code with the automatic daylight savings time, than manually adjusting it in the code.

You can ask a moderator to move the thread to the Jobs and Paid Consultancy forum section (use the flag button on the lower right of the first post). You will be asked for a budget.

No, upload the code.

It can be automated. See Post #5.

:rofl::rofl::rofl:


#include "Arduino.h"
#include "TM1637Display.h"
#include "ArduinoJson.h"
#include "WiFiManager.h"
#include "NTPClient.h"


// Module connection pins (Digital Pins)
#define red_CLK 13
#define red1_DIO 14
#define red2_DIO 15
#define red3_DIO 16

#define green_CLK 17
#define green1_DIO 18
#define green2_DIO 19
#define green3_DIO 21

#define orange_CLK 22
#define orange1_DIO 23
#define orange2_DIO 25
#define orange3_DIO 26

#define greenAM 32
#define greenPM 33

bool res;
int Hour = 0;
//========================USEFUL VARIABLES=============================
int UTC = -5; // UTC + value in hour - Summer time
const long utcOffsetInSeconds = 3600; // Offset in second
int Display_backlight = 50;
const char *ssid     = "SSID"; 
const char *password = "password";

//Set the red displays
int red_day = 26;
int red_month = 10;
int red_year = 1985;
int red_hour = 01;
int red_minute = 21;

//set the orange displays
int orange_day = 26;
int orange_month = 10;
int orange_year = 1985;
int orange_hour = 01;
int orange_minute = 20;

//=====================================================================

// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds*UTC);

TM1637Display green1(green_CLK, green1_DIO);
TM1637Display green2(green_CLK, green2_DIO);
TM1637Display green3(green_CLK, green3_DIO);

TM1637Display red1(red_CLK, red1_DIO);
TM1637Display red2(red_CLK, red2_DIO);
TM1637Display red3(red_CLK, red3_DIO);

TM1637Display orange1(orange_CLK, orange1_DIO);
TM1637Display orange2(orange_CLK, orange2_DIO);
TM1637Display orange3(orange_CLK, orange3_DIO);

void setup()
{ 
  pinMode(32, OUTPUT);
  pinMode(33, OUTPUT);
  Serial.begin(115200);
  
     Serial.println("\n Starting");

  WiFi.begin(ssid, password);


  while ( WiFi.status() != WL_CONNECTED ) {
    delay ( 500 );
    Serial.print ( "." );
  }   
     

  timeClient.begin();
  red1.setBrightness(Display_backlight);
  red2.setBrightness(Display_backlight);
  red3.setBrightness(Display_backlight);
  
  green1.setBrightness(Display_backlight);
  green2.setBrightness(Display_backlight);
  green3.setBrightness(Display_backlight);

  orange1.setBrightness(Display_backlight);
  orange2.setBrightness(Display_backlight);
  orange3.setBrightness(Display_backlight);

}
  
void loop()
{
  timeClient.update();
  Serial.print("Time: ");
  Serial.println(timeClient.getFormattedTime());
  unsigned long epochTime = timeClient.getEpochTime();
  struct tm *ptm = gmtime ((time_t *)&epochTime); 
  int currentYear = ptm->tm_year+1900;
  Serial.print("Year: ");
  Serial.println(currentYear);

  int monthDay = ptm->tm_mday;
  Serial.print("Month day: ");
  Serial.println(monthDay);

  int currentMonth = ptm->tm_mon+1;
  Serial.print("Month: ");
  Serial.println(currentMonth);

      if (timeClient.getHours() == 0)
  { 
    Hour = 12;
    digitalWrite(greenAM, 1);
    digitalWrite(greenPM, 0);
    Serial.write(" =0");
  }

  else if (timeClient.getHours() == 12)
  { 
    Hour = timeClient.getHours();
    digitalWrite(greenAM, 0);
    digitalWrite(greenPM, 1);
    Serial.write(" =12");
  }
  
  else if (timeClient.getHours() >= 13) {
    Hour = timeClient.getHours() - 12;
    digitalWrite(greenAM, 0);
    digitalWrite(greenPM, 1);
    Serial.write(" >=13");
  }

  else {
    Hour = timeClient.getHours();
    digitalWrite(greenAM, 1);
    digitalWrite(greenPM, 0);
    Serial.write("else");
  }
  
red1.showNumberDecEx(red_month,0b01000000,true,2,0);
red1.showNumberDecEx(red_day,0b01000000,true,2,2);
red2.showNumberDecEx(red_year,0b00000000,true);
red3.showNumberDecEx(red_hour,0b01000000,true,2,0);
red3.showNumberDecEx(red_minute,0b01000000,true,2,2);


green1.showNumberDecEx(currentMonth,0b01000000,true,2,0);
green1.showNumberDecEx(monthDay,0b01000000,true,2,2);
green2.showNumberDecEx(currentYear,0b00000000,true);
green3.showNumberDecEx(Hour,0b01000000,true,2,0);
green3.showNumberDecEx(timeClient.getMinutes(),0b01000000,true,2,2);


orange1.showNumberDecEx(orange_month,0b01000000,true,2,0);
orange1.showNumberDecEx(orange_day,0b01000000,true,2,2);
orange2.showNumberDecEx(orange_year,0b00000000,true);
orange3.showNumberDecEx(orange_hour,0b01000000,true,2,0);
orange3.showNumberDecEx(orange_minute,0b01000000,true,2,2);

if((currentMonth*30 + monthDay) >= 67 && (currentMonth*30 + monthDay) < 306){
timeClient.setTimeOffset(utcOffsetInSeconds*UTC);} // Change daylight saving time - Summer
else {timeClient.setTimeOffset((utcOffsetInSeconds*UTC) - 3600);} // Change daylight saving time - Winter

if(timeClient.getHours()>=13){
  digitalWrite(greenAM,0);
  digitalWrite(greenPM,1);}
  
else if(timeClient.getHours()==12){
  digitalWrite(greenAM,0);
  digitalWrite(greenPM,1);}

else{
  digitalWrite(greenAM,1);
  digitalWrite(greenPM,0);}

delay(20);

}

NTP + daylight saving is on some microccontrollers just setting of the timezone:

Example for the ESP32 NTP for the ESP32 including day light saving without 3rd party library

Example for the ESP8266 NTP for the ESP8266 including day light saving time (DST) without 3rd party library

@workingforaliving which microcontroller are you using?

It's simple enough.
From UK Gov

"In the UK the clocks go forward 1 hour at 1am on the last Sunday in March, and back 1 hour at 2am on the last Sunday in October.

The period when the clocks are 1 hour ahead is called British Summer Time (BST). There’s more daylight in the evenings and less in the mornings (sometimes called Daylight Saving Time).

When the clocks go back, the UK is on Greenwich Mean Time (GMT)."

With an RTC, it would be straightforward to add or subtract an hour at the appropriate day and hour if it wasn't built in.

Alternatively, use a time signal radio clock or module - WWW 60KHz in the States - to do it automatically.

There are some outstanding clocks built by enthusiasts that exploit the time signal coding.

I really don't understand the thinking behind retaining DST/GMT. Personally, I would stick with one or other.

The argument goes that adding one hour in summer let the locals involved in harvests got an extra hour in the evening. There's also a suggestion that kids get to go to school in the daylight. Either way, it's a pain. These days, the locals don't turn out to get the crops in. More likely a contractor will work through the night with modern, well lit combines.

You can only combine when the humidity is low enough to let the grain kernels break free from the stem. If the machine beats the grain too hard, the kernels are broken and the grain is worthless.

1 Like

Also combustion and mold. Summers of my youth were spent raking and bailing (small farm, 150 head) only on the hottest, driest, days of the year... then into the mow to stack and salt the bails to further prevent mold.

U.S. "saving" time now starts the Sunday before Election day... depending on the beans counted... can either encourage or discourage showing to the polling station.

The UK summers have changed dramatically in the last few years.
Higher temperatures and weeks on end without rain.
On chalk where I live, any moisture just disappears.
We hit 40 degrees C a couple of years ago
Right now, it's just the opposite. Cold and very wet. Huge amount of flooding in the Southwest.
The fields here are quite small, so a well equipped contractor can pick and choose when to cut.

Arizona does not do saving time! West coast states trying to join that plan, too. The year my wife and I married, 1961, DST was by county. We lives in a “non” county and she worked in a DST county. What a mess!

1 Like

Back to the original question.
I make my clocks with XIAO ESP32-C3 boards. Nice and small and low power.
I get NTP off the internet with a couple of lines of code.
That "NOW" value (UTC) can be converted to local time (with DST) with a location line.
Days can be added/subtracted by adding/subtracting "seconds in a day" to the NOW value.
That new fictional UTC value can be converted again to local time elements.
This is the framework. No library used, because it's all included in the ESP core.
Can make a full blown sketch if needed.
Leo..

#include <WiFi.h>  // ESP32
time_t now;  // holds epoch
tm tm;       // time structure

void setup() {
  WiFi.begin("My_SSID", "My_PASS"); // credentials
  configTzTime("CST6CDT,M3.2.0,M11.1.0", "pool.ntp.org");  // https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
}

void loop() {
  time(&now); // get UTC
  localtime_r(&now, &tm); // fill time struct with local time elements
}

The links in post#14 show how to get/print those time elements.
Example: Serial.print(tm.tm_hour);

1 Like