瀏覽代碼

finalized

Toby Chui 1 年之前
父節點
當前提交
47841d9434
共有 2 個文件被更改,包括 21 次插入3 次删除
  1. 2 2
      InkyDash/draw.ino
  2. 19 1
      InkyDash/time.ino

+ 2 - 2
InkyDash/draw.ino

@@ -108,7 +108,7 @@ void drawCalender(int y) {
   // Time Calculations
   time_t epochTime = ntpClient.getEpochTime();
   struct tm *ptm = gmtime((time_t *)&epochTime);
-  int dayInMonth = getNumberOfDayByMonth(ptm->tm_mon);
+  int dayInMonth = getNumberOfDayByMonth(ptm->tm_mon, ptm->tm_year + 1900);
   int firstDayOfWeek = getFirstDayDayOfWeek() + 1;
   int dayCounter = (1 - firstDayOfWeek);
 
@@ -133,7 +133,7 @@ void drawCalender(int y) {
   } else {
     previousMonthID = previousMonthID - 1;
   }
-  int dayInPrevMonth = getNumberOfDayByMonth(previousMonthID);
+  int dayInPrevMonth = getNumberOfDayByMonth(previousMonthID, ptm->tm_year + 1900);
   int nextMonthDayCounter = 1;
   
   //Render the calender dates

+ 19 - 1
InkyDash/time.ino

@@ -55,11 +55,16 @@ int getFirstDayDayOfWeek() {
 }
 
 //Get the number of days of a month
-int getNumberOfDayByMonth(int monthId) {
+int getNumberOfDayByMonth(int monthId, int currentYear) {
   if (monthId < 0 || monthId > 11) {
     return -1; // Invalid monthId
   }
   int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
+
+  if (monthId == 1 && isLeapYear(currentYear)){
+    //Feb in leap year
+    return 29;
+  }
   return daysInMonth[monthId];
 }
 
@@ -71,6 +76,19 @@ bool isNight() {
   return (currentHour >= 19 || currentHour < 7);
 }
 
+//Check if given year is leap year
+bool isLeapYear(int year) {
+    // Check if the year is divisible by 4
+    if (year % 4 == 0) {
+        // If it's divisible by 100, it should also be divisible by 400 to be a leap year
+        if (year % 100 == 0) {
+            return year % 400 == 0;
+        }
+        return true;
+    }
+    return false;
+}
+
 //Grab a random NTP server
 const char* getRandomNtpServer(const char* ntpServers[]) {
   int randomIndex = ESP8266TrueRandom.random(ntpServerCounts - 1);