]> git.earman.xyz Git - sensor-watch.git/commitdiff
Made the days_in_month its own function
authorDavid Volovskiy <devolov@gmail.com>
Sat, 10 Aug 2024 11:40:52 +0000 (07:40 -0400)
committerDavid Volovskiy <devolov@gmail.com>
Sat, 10 Aug 2024 11:40:52 +0000 (07:40 -0400)
apps/beats-time/app.c
movement/watch_faces/complication/day_one_face.c
movement/watch_faces/complication/time_left_face.c
movement/watch_faces/settings/set_time_face.c
movement/watch_faces/settings/set_time_hackwatch_face.c
watch-library/shared/watch/watch_utility.c
watch-library/shared/watch/watch_utility.h

index f97924742742ddf40ef1f7888d972215bfb58892..8d6c1db9bfbc8063af908854c047732eb0631ef9 100644 (file)
@@ -204,7 +204,6 @@ void set_time_mode_handle_primary_button(void) {
 
 void set_time_mode_handle_secondary_button(void) {
     watch_date_time date_time = watch_rtc_get_date_time();
-    const uint8_t days_in_month[12] = {31, 28, 31, 30, 31, 30, 30, 31, 30, 31, 30, 31};
 
     switch (application_state.page) {
         case 0: // hour
@@ -227,7 +226,7 @@ void set_time_mode_handle_secondary_button(void) {
             date_time.unit.day = date_time.unit.day + 1;
             break;
     }
-    if (date_time.unit.day > days_in_month[date_time.unit.month - 1] + (is_leap(date_time.unit.year) && date_time.unit.month == 2))
+    if (date_time.unit.day > days_in_month(date_time.unit.month, date_time.unit.year + WATCH_RTC_REFERENCE_YEAR))
         date_time.unit.day = 1;
     watch_rtc_set_date_time(date_time);
 }
index 4429611eaccb6df569f80ed75fa7f3ef221c2374..aa65321e507fb3cf89d1967b3a948b5359c63bea 100644 (file)
@@ -28,8 +28,6 @@
 #include "watch.h"
 #include "watch_utility.h"
 
-static const uint8_t days_in_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
-
 static uint32_t _day_one_face_juliandaynum(uint16_t year, uint16_t month, uint16_t day) {
     // from here: https://en.wikipedia.org/wiki/Julian_day#Julian_day_number_calculation
     return (1461 * (year + 4800 + (month - 14) / 12)) / 4 + (367 * (month - 2 - 12 * ((month - 14) / 12))) / 12 - (3 * ((year + 4900 + (month - 14) / 12) / 100))/4 + day - 32075;
@@ -71,7 +69,7 @@ static void _day_one_face_increment(day_one_state_t *state) {
         default:
             break;
     }
-    if (state->birth_day == 0 || state->birth_day > (days_in_month[state->birth_month - 1] + (is_leap(state->birth_year) && state->birth_month == 2)))
+    if (state->birth_day == 0 || state->birth_day > days_in_month(state->birth_month, state->birth_year))
         state->birth_day = 1;
 }
 
index 74ed35b737a2ad4591a2d5a62f60d6708ce4e006..99b0f87fd9a64fdf3dfb22aa8f60d20a40765f6e 100644 (file)
@@ -159,7 +159,6 @@ static void _draw(time_left_state_t *state, uint8_t subsecond) {
 /// @brief handle short or long pressing the alarm button
 static void _handle_alarm_button(time_left_state_t *state) {
     
-    const uint8_t days_in_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
     switch (state->current_page) {
         case TIME_LEFT_FACE_SETTINGS_STATE: // birth year
             state->birth_date.bit.year++;
@@ -182,9 +181,9 @@ static void _handle_alarm_button(time_left_state_t *state) {
             state->target_date.bit.day++;
             break;
     }
-    if (state->birth_date.bit.day > (days_in_month[state->birth_date.bit.month - 1] + (is_leap(state->birth_date.bit.year) && state->birth_date.bit.month == 2)))
+    if (state->birth_date.bit.day > days_in_month(state->birth_date.bit.month, state->birth_date.bit.year))
         state->birth_date.bit.day = 1;
-    if (state->target_date.bit.day > (days_in_month[state->target_date.bit.month - 1] + (is_leap(state->target_date.bit.year) && state->target_date.bit.month == 2)))
+    if (state->target_date.bit.day > days_in_month(state->target_date.bit.month, state->birth_date.bit.year))
         state->target_date.bit.day = 1;
 }
 
index 02cbb17de2d0f2b08405b14cd12ca50a9760ec50..503dffc995a405fe5e4d02078e5a438e2a27b384 100644 (file)
@@ -34,7 +34,6 @@ static bool _quick_ticks_running;
 
 static void _handle_alarm_button(movement_settings_t *settings, watch_date_time date_time, uint8_t current_page) {
     // handles short or long pressing of the alarm button
-    const uint8_t days_in_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 
     switch (current_page) {
         case 0: // hour
@@ -61,7 +60,7 @@ static void _handle_alarm_button(movement_settings_t *settings, watch_date_time
             if (settings->bit.time_zone > 40) settings->bit.time_zone = 0;
             break;
     }
-    if (date_time.unit.day > (days_in_month[date_time.unit.month - 1] + (is_leap(date_time.unit.year) &&date_time.unit.month == 2)))
+    if (date_time.unit.day > days_in_month(date_time.unit.month, date_time.unit.year + WATCH_RTC_REFERENCE_YEAR))
         date_time.unit.day = 1;
     watch_rtc_set_date_time(date_time);
 }
index c760fd9b59a009e9c16ef1d22e5a1796a00be2e1..8ba56cba94f1781196a1675f59fd5d3823bcd274 100644 (file)
@@ -48,7 +48,6 @@ void set_time_hackwatch_face_activate(movement_settings_t *settings, void *conte
 
 bool set_time_hackwatch_face_loop(movement_event_t event, movement_settings_t *settings, void *context) {
     uint8_t current_page = *((uint8_t *)context);
-    const uint8_t days_in_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 
     if (event.subsecond == 15) // Delay displayed time update by ~0.5 seconds, to align phase exactly to main clock at 1Hz
         date_time_settings = watch_rtc_get_date_time();
@@ -121,7 +120,7 @@ bool set_time_hackwatch_face_loop(movement_event_t event, movement_settings_t *s
                 case 5: // day
                     date_time_settings.unit.day = date_time_settings.unit.day - 2;
                     if (date_time_settings.unit.day == 0) {
-                        date_time_settings.unit.day = days_in_month[date_time_settings.unit.month - 1] + (is_leap(date_time_settings.unit.year) && date_time_settings.unit.month == 2);
+                        date_time_settings.unit.day = days_in_month(date_time_settings.unit.month, date_time_settings.unit.year + WATCH_RTC_REFERENCE_YEAR);
                     } else
                         date_time_settings.unit.day++;
                     break;
@@ -172,7 +171,7 @@ bool set_time_hackwatch_face_loop(movement_event_t event, movement_settings_t *s
                     if (settings->bit.time_zone > 40) settings->bit.time_zone = 0;
                     break;
             }
-            if (date_time_settings.unit.day > (days_in_month[date_time_settings.unit.month - 1] + (is_leap(date_time_settings.unit.year) && date_time_settings.unit.month == 2)))
+            if (date_time_settings.unit.day > days_in_month(date_time_settings.unit.month, date_time_settings.unit.year + WATCH_RTC_REFERENCE_YEAR))
                 date_time_settings.unit.day = 1;
             if (current_page != 2) // Do not set time when we are at seconds, it was already set previously
                 watch_rtc_set_date_time(date_time_settings);
index 64b3bb791cfcd06ebe8f60542e326b7499b633df..c00791e76786605634d3c5b343e570a88f75bffc 100644 (file)
@@ -315,3 +315,11 @@ uint32_t watch_utility_offset_timestamp(uint32_t now, int8_t hours, int8_t minut
     new += seconds;
     return new;
 }
+
+uint8_t days_in_month(uint8_t month, uint16_t year) {
+    static const uint8_t days_in_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
+    uint8_t days = days_in_month[month - 1];
+    if (month == 2 && is_leap(year))
+        days += 1;
+    return days;
+}
index e2326d1318865fa34c7c3bba517a1def054b06ef..5533e19661c4d9dda1883f545e2bca448cb0f4a5 100644 (file)
@@ -164,4 +164,10 @@ float watch_utility_thermistor_temperature(uint16_t value, bool highside, float
  */
 uint32_t watch_utility_offset_timestamp(uint32_t now, int8_t hours, int8_t minutes, int8_t seconds);
 
+/** @brief Returns the number of days in a month. It also handles Leap Years for February.
+ * @param month The month of the date (1-12)
+ * @param year The year of the date (ex. 2022)
+ */
+uint8_t days_in_month(uint8_t month, uint16_t year);
+
 #endif