Readds leap year checks (#2)
This commit is contained in:
parent
c0514ad39a
commit
d05a851d94
@ -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) {
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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;
|
||||
@ -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);
|
||||
}
|
||||
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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…
x
Reference in New Issue
Block a user