Skip to content

Commit

Permalink
check_and_act_on_daylight_savings now only occurs in one spot
Browse files Browse the repository at this point in the history
  • Loading branch information
voloved committed Aug 4, 2024
1 parent 2824a62 commit 598e876
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 33 deletions.
48 changes: 26 additions & 22 deletions movement/movement.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,31 @@ static inline void _movement_disable_fast_tick_if_possible(void) {
}
}

static bool _check_and_act_on_daylight_savings(void) {
if (!movement_state.settings.bit.dst_active) return false;
watch_date_time date_time = watch_rtc_get_date_time();
// No need for all of the unix time calculations for times not at the beginning or end of the hour
if (date_time.unit.minute > 1 && date_time.unit.minute < 59) return false;
uint8_t dst_result = get_dst_status(date_time);
bool dst_skip_rolling_back = get_dst_skip_rolling_back();

if (dst_skip_rolling_back && (dst_result == DST_ENDED)) {
clear_dst_skip_rolling_back();
}
else if (dst_result == DST_ENDING && !dst_skip_rolling_back) {
date_time.unit.hour = (date_time.unit.hour + 24 - 1) % 24;
watch_rtc_set_date_time(date_time);
set_dst_skip_rolling_back();
return true;
}
else if (dst_result == DST_STARTING) {
date_time.unit.hour = (date_time.unit.hour + 1) % 24;
watch_rtc_set_date_time(date_time);
return true;
}
return false;
}

static void _movement_handle_background_tasks(void) {
for(uint8_t i = 0; i < MOVEMENT_NUM_FACES; i++) {
// For each face, if the watch face wants a background task...
Expand All @@ -256,6 +281,7 @@ static void _movement_handle_background_tasks(void) {
watch_faces[i].loop(background_event, &movement_state.settings, watch_face_contexts[i]);
}
}
_check_and_act_on_daylight_savings();
movement_state.needs_background_tasks_handled = false;
}

Expand Down Expand Up @@ -430,28 +456,6 @@ uint8_t movement_claim_backup_register(void) {
return movement_state.next_available_backup_register++;
}

bool check_and_act_on_daylight_savings(watch_date_time date_time) {
if (!movement_state.settings.bit.dst_active) return false;
uint8_t dst_result = get_dst_status(date_time);
bool dst_skip_rolling_back = get_dst_skip_rolling_back();

if (dst_skip_rolling_back && (dst_result == DST_ENDED)) {
clear_dst_skip_rolling_back();
}
else if (dst_result == DST_ENDING && !dst_skip_rolling_back) {
date_time.unit.hour = (date_time.unit.hour + 24 - 1) % 24;
watch_rtc_set_date_time(date_time);
set_dst_skip_rolling_back();
return true;
}
else if (dst_result == DST_STARTING) {
date_time.unit.hour = (date_time.unit.hour + 1) % 24;
watch_rtc_set_date_time(date_time);
return true;
}
return false;
}

int16_t get_timezone_offset(uint8_t timezone_idx, watch_date_time date_time) {
if (!movement_state.settings.bit.dst_active) return movement_timezone_offsets[timezone_idx];
if (dst_occurring(date_time))
Expand Down
1 change: 0 additions & 1 deletion movement/movement.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@ void movement_play_alarm(void);
void movement_play_alarm_beeps(uint8_t rounds, BuzzerNote alarm_note);

uint8_t movement_claim_backup_register(void);
bool check_and_act_on_daylight_savings(watch_date_time date_time); // Returns if the time was changed due to DST
int16_t get_timezone_offset(uint8_t timezone_idx, watch_date_time date_time);

#endif // MOVEMENT_H_
4 changes: 2 additions & 2 deletions movement/watch_faces/clock/clock_face.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,9 @@ void clock_face_resign(movement_settings_t *settings, void *context) {
bool clock_face_wants_background_task(movement_settings_t *settings, void *context) {
(void) settings;
clock_state_t *state = (clock_state_t *) context;
watch_date_time date_time = watch_rtc_get_date_time();
check_and_act_on_daylight_savings(date_time);
if (!state->time_signal_enabled) return false;

watch_date_time date_time = watch_rtc_get_date_time();

return date_time.unit.minute == 0;
}
4 changes: 2 additions & 2 deletions movement/watch_faces/clock/minute_repeater_decimal_face.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ void minute_repeater_decimal_face_resign(movement_settings_t *settings, void *co
bool minute_repeater_decimal_face_wants_background_task(movement_settings_t *settings, void *context) {
(void) settings;
minute_repeater_decimal_state_t *state = (minute_repeater_decimal_state_t *)context;
watch_date_time date_time = watch_rtc_get_date_time();
check_and_act_on_daylight_savings(date_time);
if (!state->signal_enabled) return false;

watch_date_time date_time = watch_rtc_get_date_time();

return date_time.unit.minute == 0;
}
4 changes: 2 additions & 2 deletions movement/watch_faces/clock/repetition_minute_face.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ void repetition_minute_face_resign(movement_settings_t *settings, void *context)
bool repetition_minute_face_wants_background_task(movement_settings_t *settings, void *context) {
(void) settings;
repetition_minute_state_t *state = (repetition_minute_state_t *)context;
watch_date_time date_time = watch_rtc_get_date_time();
check_and_act_on_daylight_savings(date_time);
if (!state->signal_enabled) return false;

watch_date_time date_time = watch_rtc_get_date_time();

return date_time.unit.minute == 0;
}
4 changes: 2 additions & 2 deletions movement/watch_faces/clock/simple_clock_bin_led_face.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,9 @@ void simple_clock_bin_led_face_resign(movement_settings_t *settings, void *conte
bool simple_clock_bin_led_face_wants_background_task(movement_settings_t *settings, void *context) {
(void) settings;
simple_clock_bin_led_state_t *state = (simple_clock_bin_led_state_t *)context;
watch_date_time date_time = watch_rtc_get_date_time();
check_and_act_on_daylight_savings(date_time);
if (!state->signal_enabled) return false;

watch_date_time date_time = watch_rtc_get_date_time();

return date_time.unit.minute == 0;
}
4 changes: 2 additions & 2 deletions movement/watch_faces/clock/simple_clock_face.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ void simple_clock_face_resign(movement_settings_t *settings, void *context) {
bool simple_clock_face_wants_background_task(movement_settings_t *settings, void *context) {
(void) settings;
simple_clock_state_t *state = (simple_clock_state_t *)context;
watch_date_time date_time = watch_rtc_get_date_time();
check_and_act_on_daylight_savings(date_time);
if (!state->signal_enabled) return false;

watch_date_time date_time = watch_rtc_get_date_time();

return date_time.unit.minute == 0;
}

0 comments on commit 598e876

Please sign in to comment.