Teensy 3.2 RTC DST support

Status
Not open for further replies.

Fluxanode

Well-known member
Hi, does anyone know if the teensy 3.2 (or any teensy with a built in RTC) RTC will change time from standard time to DST automatically, or do I need to do it in code to make the time change?
What about leap year?

Thanks
 
Thanks Paul.

I put together a test program to do the time change and am having a problem with this if statement:
if (month() == 3 && day() == (14 - x) && hour() >= 2)

It seems to ignore the day and the hour parts and set the DSTchk to one before the correct day is reached.
Using teensy 3.2

Here's my test program:

Code:
#include <TimeLib.h>                    //Time lib
#include "sdios.h"                      //Std IO lib




void setup() {
  Serial.begin(9600);
  setSyncProvider(getTeensy3Time); 
}


void loop() {
  int i;
  i = Sunday_Offset();
  Serial.println(GetTimeNow());
  Serial.print("offset ");
  Serial.println(i);
  delay(500);
  Serial.print("dst ");
  Serial.println(dst());
  delay(500);


}
/* Functions */


//Sunday_Offset() **********************************************************
int Sunday_Offset() {
  int y = year() - 2000;
  int x = (y + y / 4 + 2) % 7;
  return x;
}
//..........................................................................


// DST() *******************************************************************
int dst(void) {
  int DSTchk;


  ////************ calc sunday offset 
  
  int x = Sunday_Offset();
  
  //// *********** Test DST: BEGINS on 2nd Sunday of March @ 2:00 AM *********
  if (month() == 3 && day() == (14 - x) && hour() >= 2)
  {
    DSTchk = 1;                                // Daylight Savings Time is TRUE (add one hour)
  }
//  if (month() == 3 && day() > (14 - x) || month() > 3)
//  {
//    DSTchk = 1;
//  }
  //// ************* Test DST: ENDS on 1st Sunday of Nov @ 2:00 AM ************
   if (month() == 11 && day() == (7 - x) && hour() >= 2)
       {
        DSTchk = 0;                                // daylight savings time is FALSE (Standard time)
       }
   if (month() == 11 && day() > (7 - x) || month() > 11 || month() < 3)
       {
        DSTchk = 0;
       }
   if (DSTchk == 1)                                 // Test DST and add one hour if = 1 (TRUE)
       {
        return 1;
       }
   else {
        return 0;
       }
}


//********** GetTimeNow **************************
// Gets current date & time from the System and returns it as a string "09/09/2020,11:09:00,"


String GetTimeNow() {
  String time_now;
  String Hour = hour();
  if (Hour.toInt() < 10) Hour = "0" + Hour;
  String Min = minute();
  if (Min.toInt() < 10) Min = "0" + Min;
  String Sec = second();
  if (Sec.toInt() < 10) Sec = "0" + Sec;
  String Mo = month();
  if (Mo.toInt() < 10) Mo = "0" + Mo;
  String Day = day();
  if (Day.toInt() < 10) Day = "0" + Day;
  String Yr = year();


  time_now = time_now + Mo + "/" + Day + "/" + Yr + "," + Hour + ":" + Min + ":" + Sec + ",";
  return time_now;
}


//********** getTeensy3time ********************************
time_t getTeensy3Time()
{
  return Teensy3Clock.get();
}
 
Status
Not open for further replies.
Back
Top