Browse Source

Fixed DST code

Kevin Lee 1 year ago
parent
commit
1f11c352bd
2 changed files with 14 additions and 6 deletions
  1. 11 4
      Nixie_Firmware_Mbed/ds3231.cpp
  2. 3 2
      Nixie_Firmware_Mbed/main.cpp

+ 11 - 4
Nixie_Firmware_Mbed/ds3231.cpp

@@ -84,8 +84,15 @@ bool IsDst(int Day, int Date, int Month, int Hour_24) {
     if (Month < 3 || Month > 11) return false;
     // April through October are always in DST
     if (Month > 3 && Month < 11) return true;
-    // In March, in DST if previous sunday was on or after the 8th
-    if (Month == 3) return ((Date - Day) >= 8) && (Hour_24 >= 2);
-    // In November, in DST if before the first Sunday
-    return ((Date - Day) <= 0) && (Hour_24 < 2);
+
+    int prevSunday = Date - Day;
+    // In March, in DST if previous sunday is between 8th and 14th
+    if (Month == 3) {
+        if (prevSunday < 8) return false;
+        if (prevSunday > 14) return true;
+        return (Date == prevSunday) ? Hour_24 >= 2 : true;
+    } 
+    // In November, in DST if before the first Sunday           
+    if (prevSunday <= 0) return true;
+    return (prevSunday == Date && Date <= 7) ? (Hour_24 < 2) : false;
 }

+ 3 - 2
Nixie_Firmware_Mbed/main.cpp

@@ -229,8 +229,8 @@ int main() {
 
     // wait(3);
         
-    // DS3231_SetTime(00, 13, 21);
-    // DS3231_SetDate(MONDAY, 11, 3, 19, 0);
+    // DS3231_SetTime(00, 05, 00);
+    // DS3231_SetDate(FRIDAY, 15, 3, 19, 0);
 
     // Setup a ticker to refresh the display at 1kHz
     RefreshTicker.attach_us(RefreshTickerCallback, REFRESH_RATE_US);
@@ -276,6 +276,7 @@ int main() {
 
             // Compensate for daylight savings time
             nextHour = IsDst(day, date, month, nextHour) ? (nextHour + 1) % 12 : nextHour % 12;
+            if (nextHour == 0) nextHour = 12;
 
             // Update the display configuration based on the new/previous time
             if (startup || prevHour / 10 != nextHour / 10) {