diff --git a/movement/watch_faces/complication/day_one_face.c b/movement/watch_faces/complication/day_one_face.c index cdb6fc7..2315233 100644 --- a/movement/watch_faces/complication/day_one_face.c +++ b/movement/watch_faces/complication/day_one_face.c @@ -64,13 +64,11 @@ static void _day_one_face_increment(day_one_state_t *state) { state->birth_month = (state->birth_month % 12) + 1; break; case PAGE_DAY: - state->birth_day = state->birth_day + 1; + state->birth_day = (state->birth_day % watch_utility_days_in_month(state->birth_month, state->birth_year)) + 1; break; default: break; } - if (state->birth_day == 0 || state->birth_day > days_in_month(state->birth_month, state->birth_year)) - state->birth_day = 1; } void day_one_face_setup(uint8_t watch_face_index, void ** context_ptr) { diff --git a/movement/watch_faces/complication/deadline_face.c b/movement/watch_faces/complication/deadline_face.c index 113d118..66d6378 100644 --- a/movement/watch_faces/complication/deadline_face.c +++ b/movement/watch_faces/complication/deadline_face.c @@ -269,15 +269,7 @@ static void _increment_date(deadline_state_t *state, watch_date_time_t date_time date_time.unit.month = (date_time.unit.month % 12) + 1; break; case 2: - date_time.unit.day = date_time.unit.day + 1; - - /* Check for leap years */ - int8_t days = days_in_month[date_time.unit.month - 1]; - if (date_time.unit.month == 2 && _is_leap(date_time.unit.year)) - days++; - - if (date_time.unit.day > days) - date_time.unit.day = 1; + date_time.unit.day = (date_time.unit.day % watch_utility_days_in_month(date_time.unit.month, date_time.unit.year + WATCH_RTC_REFERENCE_YEAR)) + 1; break; case 3: date_time.unit.hour = (date_time.unit.hour + 1) % 24; diff --git a/movement/watch_faces/complication/time_left_face.c b/movement/watch_faces/complication/time_left_face.c index 2780d47..29a76fe 100644 --- a/movement/watch_faces/complication/time_left_face.c +++ b/movement/watch_faces/complication/time_left_face.c @@ -168,7 +168,7 @@ static void _handle_alarm_button(time_left_state_t *state) { state->birth_date.bit.month = (state->birth_date.bit.month % 12) + 1; break; case TIME_LEFT_FACE_SETTINGS_STATE + 2: // birth day - state->birth_date.bit.day++; + state->birth_date.bit.day = (state->birth_date.bit.day % watch_utility_days_in_month(state->birth_date.bit.month, state->birth_date.bit.year)) + 1; break; case TIME_LEFT_FACE_SETTINGS_STATE + 3: // target year state->target_date.bit.year++; @@ -178,13 +178,9 @@ static void _handle_alarm_button(time_left_state_t *state) { state->target_date.bit.month = (state->target_date.bit.month % 12) + 1; break; case TIME_LEFT_FACE_SETTINGS_STATE + 5: // target day - state->target_date.bit.day++; + state->target_date.bit.day = (state->target_date.bit.day % watch_utility_days_in_month(state->target_date.bit.month, state->birth_date.bit.year)) + 1; break; } - 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, state->birth_date.bit.year)) - state->target_date.bit.day = 1; } static void _initiate_setting(time_left_state_t *state) { diff --git a/movement/watch_faces/settings/set_time_hackwatch_face.c b/movement/watch_faces/settings/set_time_hackwatch_face.c index 94557be..6551019 100644 --- a/movement/watch_faces/settings/set_time_hackwatch_face.c +++ b/movement/watch_faces/settings/set_time_hackwatch_face.c @@ -163,15 +163,13 @@ bool set_time_hackwatch_face_loop(movement_event_t event, void *context) { date_time_settings.unit.month = (date_time_settings.unit.month % 12) + 1; break; case 5: // day - date_time_settings.unit.day = date_time_settings.unit.day + 1; + date_time_settings.unit.day = (date_time_settings.unit.day % watch_utility_days_in_month(date_time_settings.unit.month, date_time_settings.unit.year + WATCH_RTC_REFERENCE_YEAR)) + 1; break; case 6: // time zone movement_set_timezone_index(movement_get_timezone_index() + 1); if (movement_get_timezone_index() >= NUM_ZONE_NAMES) movement_set_timezone_index(0); break; } - 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); //TODO: Do not update whole RTC, just what we are changing diff --git a/watch-faces/settings/set_time_face.c b/watch-faces/settings/set_time_face.c index 8092e87..3d3714c 100644 --- a/watch-faces/settings/set_time_face.c +++ b/watch-faces/settings/set_time_face.c @@ -55,7 +55,7 @@ static void _handle_alarm_button(watch_date_time_t date_time, uint8_t current_pa date_time.unit.month = (date_time.unit.month % 12) + 1; break; case 5: { // day - date_time.unit.day = date_time.unit.day + 1; + date_time.unit.day = (date_time.unit.day % watch_utility_days_in_month(date_time.unit.month, date_time.unit.year + WATCH_RTC_REFERENCE_YEAR)) + 1; break; } case 6: // time zone @@ -64,8 +64,6 @@ static void _handle_alarm_button(watch_date_time_t date_time, uint8_t current_pa current_offset = movement_get_current_timezone_offset_for_zone(movement_get_timezone_index()); break; } - 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; movement_set_local_date_time(date_time); } diff --git a/watch-library/shared/watch/watch_utility.c b/watch-library/shared/watch/watch_utility.c index 81211f6..69cf95d 100644 --- a/watch-library/shared/watch/watch_utility.c +++ b/watch-library/shared/watch/watch_utility.c @@ -320,3 +320,11 @@ uint32_t watch_utility_offset_timestamp(uint32_t now, int8_t hours, int8_t minut new += seconds; return new; } + +uint8_t watch_utility_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; +} diff --git a/watch-library/shared/watch/watch_utility.h b/watch-library/shared/watch/watch_utility.h index 169e573..63f6a54 100644 --- a/watch-library/shared/watch/watch_utility.h +++ b/watch-library/shared/watch/watch_utility.h @@ -172,6 +172,6 @@ uint32_t watch_utility_offset_timestamp(uint32_t now, int8_t hours, int8_t minut * @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); +uint8_t watch_utility_days_in_month(uint8_t month, uint16_t year); #endif