Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Readds leap year checks #2

Merged
merged 4 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions movement/watch_faces/complication/day_one_face.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 1 addition & 9 deletions movement/watch_faces/complication/deadline_face.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 2 additions & 6 deletions movement/watch_faces/complication/time_left_face.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand All @@ -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) {
Expand Down
4 changes: 1 addition & 3 deletions movement/watch_faces/settings/set_time_hackwatch_face.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions watch-faces/settings/set_time_face.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,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 3: { // 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 4: // hour
date_time.unit.hour = (date_time.unit.hour + 1) % 24;
Expand All @@ -64,8 +64,6 @@ static void _handle_alarm_button(watch_date_time_t date_time, uint8_t current_pa
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);
}

Expand Down
8 changes: 8 additions & 0 deletions watch-library/shared/watch/watch_utility.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion watch-library/shared/watch/watch_utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading