Merge PR #434 - fix leap years

Improves handling of months in the Sensor Watch
by computing whether the given year is a leap year.

Reviewed-by: Matheus Afonso Martins Moreira <matheus@matheusmoreira.com>
GitHub-Pull-Request: https://github.com/joeycastillo/Sensor-Watch/pull/434
This commit is contained in:
Matheus Afonso Martins Moreira
2024-08-28 22:46:49 -03:00
7 changed files with 36 additions and 48 deletions

View File

@@ -26,8 +26,7 @@
#include <string.h>
#include "day_one_face.h"
#include "watch.h"
static const uint8_t days_in_month[12] = {31, 29, 31, 30, 31, 30, 30, 31, 30, 31, 30, 31};
#include "watch_utility.h"
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
@@ -66,13 +65,12 @@ static void _day_one_face_increment(day_one_state_t *state) {
break;
case PAGE_DAY:
state->birth_day = state->birth_day + 1;
if (state->birth_day == 0 || state->birth_day > days_in_month[state->birth_month - 1]) {
state->birth_day = 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(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr) {

View File

@@ -27,6 +27,7 @@
#include "time_left_face.h"
#include "watch.h"
#include "watch_private_display.h"
#include "watch_utility.h"
const char _state_titles[][3] = {{'D', 'L', ' '}, {'D', 'L', ' '}, {'D', 'A', ' '}, {'D', 'A', ' '}, {'Y', 'R', 'b'}, {'M', 'O', 'b'}, {'D', 'A', 'b'},
{'Y', 'R', 'd'}, {'M', 'O', 'd'}, {'D', 'A', 'd'}};
@@ -158,8 +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, 30, 31, 30, 31, 30, 31};
uint32_t tmp_day;
switch (state->current_page) {
case TIME_LEFT_FACE_SETTINGS_STATE: // birth year
state->birth_date.bit.year++;
@@ -169,14 +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
tmp_day = state->birth_date.bit.day; // use a temporary variable to avoid messing up the months
tmp_day++;
// handle February 29th on a leap year
if (((tmp_day > days_in_month[state->birth_date.bit.month - 1]) && (state->birth_date.bit.month != 2 || (state->birth_date.bit.year % 4) != 0))
|| (state->birth_date.bit.month == 2 && (state->birth_date.bit.year % 4) == 0 && tmp_day > 29)) {
tmp_day = 1;
}
state->birth_date.bit.day = tmp_day;
state->birth_date.bit.day++;
break;
case TIME_LEFT_FACE_SETTINGS_STATE + 3: // target year
state->target_date.bit.year++;
@@ -186,16 +178,13 @@ 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
tmp_day = state->target_date.bit.day;
tmp_day++;
// handle February 29th on a leap year
if (((tmp_day > days_in_month[state->target_date.bit.month - 1]) && (state->target_date.bit.month != 2 || (state->target_date.bit.year % 4) != 0))
|| (state->target_date.bit.month == 2 && (state->target_date.bit.year % 4) == 0 && tmp_day > 29)) {
tmp_day = 1;
}
state->target_date.bit.day = tmp_day;
state->target_date.bit.day++;
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) {