🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    Core2 RTC adjusting for DST

    Arduino
    2
    3
    5.0k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J
      johno
      last edited by

      I'd like to have the time automatically adjust for Daylight Saving time without having ge an NTP update.
      The following code sets my timezone and DST correctly when it does an NTP update. But if I explicitly set the time and date to just before a DST change and observe the time, it doesn't change to the DST time.
      I'd be grateful for any observations and suggesions.

      bool timeSync() {
      configTime(0, 0, ntpServer); // get UTC time from NTP server
      setenv("TZ", timezone, 1); // Set the TZ.
      tzset();
      if (!getLocalTime(&timeinfo)) {
      Serial.print("Failed to obtain time from ");
      Serial.println(ntpServer);
      return false;
      }
      // Copy the time and date from the struct tm format into the M5Core2
      // TimeStruct and write it into the RTC.
      TimeStruct.Hours = timeinfo.tm_hour;
      TimeStruct.Minutes = timeinfo.tm_min;
      TimeStruct.Seconds = timeinfo.tm_sec;
      DateStruct.Year = (timeinfo.tm_year + 1900);
      DateStruct.Month = (timeinfo.tm_mon + 1);
      DateStruct.Date = timeinfo.tm_mday;
      DateStruct.WeekDay = timeinfo.tm_wday; // day of week. 0 = Sunday

      // TimeStruct.Hours = 2;
      // TimeStruct.Minutes = 58;
      // DateStruct.Month = 4;
      // DateStruct.Date = 3;
      // DateStruct.WeekDay = 0;
      M5.Rtc.SetTime(&TimeStruct);
      M5.Rtc.SetDate(&DateStruct);

      return true;
      }

      C 1 Reply Last reply Reply Quote 0
      • C
        ChrisZang @johno
        last edited by

        @johno try this:

        #include "time.h"

        // Set offset time in seconds to adjust for your timezone, for example:
        #define GMT+1 3600
        #define GMT+8 28800
        #define GMT-1 -3600
        #define GMT0 0
        #define PST -28800
        #define DST 3600

        // this sets the timezone to PT with automatic switch for DST
        const char* ntpServer = "pool.ntp.org";
        const long gmtOffset_sec = PST;
        const int daylightOffset_sec = DST;

        void (setup ()
        {
        configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
        SetRTC() ;
        }

        bool SetRTC()
        {
        struct tm timeinfo;
        if(!getLocalTime(&timeinfo))
        {
        Serial.println("Failed to obtain time");
        return (false);
        }
        RTCtime.Hours = timeinfo.tm_hour;
        RTCtime.Minutes = timeinfo.tm_min;
        RTCtime.Seconds = timeinfo.tm_sec;
        RTCDate.Year = timeinfo.tm_year+1900;
        RTCDate.Month = timeinfo.tm_mon;
        RTCDate.Date = timeinfo.tm_mday;
        return (true);
        }

        1 Reply Last reply Reply Quote 0
        • J
          johno
          last edited by

          @ChrisZang thanks for the reply.
          The problem is that will only set up the time and DST offset if I request the time from the NTP server. When a DST transition occurs sometime later the clock will not automatically adjust without doing another request from the NTP server.
          I have solved the problem by simply adding the code to advance or retard the clock at the appropriate time based on my timezone. i.e. first Sunday in April at 03:00 I change the clock to 02:00 and the first Sunday in October at 02:00 I change the clock to 03:00.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post