Daylight savings time

This topic contains 1 reply, has 2 voices, and was last updated by  David Fox 3 days, 9 hours ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #41445

    seanjgamble100
    Participant

    Is there any way to add a DST option in the NCS314? I would like an option to turn it on and have it spring forward and fall back automatically. Thanks!

    #52606

    David Fox
    Participant

    // This code is specific for Mountain Time Zone and other time zones that use the same dates.

    #include <Wire.h>
    #include “RTClib.h” // Library for DS3231 RTC module

    RTC_DS3231 rtc;

    void setup() {
    Serial.begin(9600);
    Wire.begin();
    rtc.begin();

    // If the RTC lost power and lost track of time, set the time to compile time
    if (!rtc.isrunning()) {
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    }
    }

    void loop() {
    DateTime now = rtc.now();
    adjustForDST(now);
    printTime(now);
    delay(1000); // Update once per second
    }

    void adjustForDST(DateTime &time) {
    int year = time.year();
    int month = time.month();
    int day = time.day();
    int hour = time.hour();

    // Adjust for DST if necessary
    if (isDST(year, month, day)) {
    hour++; // Add one hour for DST
    if (hour > 23) {
    hour = 0; // Wrap around to 0 if hour exceeds 23
    day++; // Move to the next day
    }
    }

    // Update the time object with adjusted values
    time = DateTime(year, month, day, hour, time.minute(), time.second());
    }

    bool isDST(int year, int month, int day) {
    // Check if the given date falls within DST period for Mountain Time Zone
    if (month < 3 || month > 11) return false; // January, February, and December are definitely not DST months
    if (month > 3 && month < 11) return true; // April to October are definitely DST months

    // Determine the exact transition dates for DST in Mountain Time Zone
    if (month == 3) {
    int previousSunday = day – (time.dayOfTheWeek() – 1);
    if (day >= 8 && previousSunday <= 14) { // Second Sunday
    return (day – previousSunday) >= 7;
    }
    }
    if (month == 11) {
    int previousSunday = day – (time.dayOfTheWeek() – 1);
    if (day <= 7 && previousSunday >= 1) { // First Sunday
    return (day – previousSunday) < 0;
    }
    }
    return false;
    }

    void printTime(DateTime time) {
    // Print the time in HH:MM:SS format
    Serial.print(time.hour(), DEC);
    Serial.print(‘:’);
    if (time.minute() < 10) {
    Serial.print(‘0’);
    }
    Serial.print(time.minute(), DEC);
    Serial.print(‘:’);
    if (time.second() < 10) {
    Serial.print(‘0’);
    }
    Serial.println(time.second(), DEC);
    }

    • This reply was modified 3 days, 9 hours ago by  David Fox.
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.