diff --git a/openrtx/include/state.h b/openrtx/include/state.h index afe96ce4..3217887a 100644 --- a/openrtx/include/state.h +++ b/openrtx/include/state.h @@ -128,9 +128,15 @@ void state_init(); void state_terminate(); /** - * This function applies the selected timezone after reading the time from - * the RTC. + * The RTC and state.time are set to UTC time + * Use this function to get local time from UTC time based on timezone setting */ -void state_applyTimezone(); +curTime_t state_getLocalTime(curTime_t utc_time); + +/** + * The RTC and state.time are set to UTC time + * Use this function to get UTC time from local time based on timezone setting + */ +curTime_t state_getUTCTime(curTime_t local_time); #endif /* STATE_H */ diff --git a/openrtx/src/state.c b/openrtx/src/state.c index 14b24bb8..9be7f030 100644 --- a/openrtx/src/state.c +++ b/openrtx/src/state.c @@ -37,7 +37,6 @@ void state_init() state.radioStateUpdated = true; #ifdef HAS_RTC state.time = rtc_getTime(); - state_applyTimezone(); #endif state.v_bat = platform_getVbat(); state.charge = battery_getCharge(state.v_bat); @@ -93,18 +92,26 @@ void state_terminate() //nvm_writeSettings(&state.settings); } -void state_applyTimezone() +curTime_t state_getLocalTime(curTime_t utc_time) { - if(state.time.hour + state.settings.utc_timezone >= 24) - { - state.time.hour = state.time.hour - 24 + state.settings.utc_timezone; - } - else if(state.time.hour + state.settings.utc_timezone < 0) - { - state.time.hour = state.time.hour + 24 + state.settings.utc_timezone; - } + curTime_t local_time = utc_time; + if(local_time.hour + state.settings.utc_timezone >= 24) + local_time.hour = local_time.hour - 24 + state.settings.utc_timezone; + else if(local_time.hour + state.settings.utc_timezone < 0) + local_time.hour = local_time.hour + 24 + state.settings.utc_timezone; else - { - state.time.hour += state.settings.utc_timezone; - } + local_time.hour += state.settings.utc_timezone; + return local_time; +} + +curTime_t state_getUTCTime(curTime_t local_time) +{ + curTime_t utc_time = local_time; + if(utc_time.hour - state.settings.utc_timezone >= 24) + utc_time.hour = utc_time.hour - 24 - state.settings.utc_timezone; + else if(utc_time.hour - state.settings.utc_timezone < 0) + utc_time.hour = utc_time.hour + 24 - state.settings.utc_timezone; + else + utc_time.hour -= state.settings.utc_timezone; + return utc_time; } diff --git a/openrtx/src/threads.c b/openrtx/src/threads.c index 9d9ed8c3..f6126b17 100644 --- a/openrtx/src/threads.c +++ b/openrtx/src/threads.c @@ -222,7 +222,6 @@ void *dev_task(void *arg) #ifdef HAS_RTC state.time = rtc_getTime(); - state_applyTimezone(); #endif // Low-pass filtering with a time constant of 10s when updated at 1Hz diff --git a/openrtx/src/ui/ui.c b/openrtx/src/ui/ui.c index 6ee76b8c..f643f8e5 100644 --- a/openrtx/src/ui/ui.c +++ b/openrtx/src/ui/ui.c @@ -1036,16 +1036,10 @@ void ui_updateFSM(event_t event, bool *sync_rtx) if(ui_state.input_position < TIMEDATE_DIGITS) break; // Return to Time&Date menu, saving values - // NOTE: The user inserted a local time, we save an UTC time to the RTC - if(ui_state.new_timedate.hour - state.settings.utc_timezone >= 24) - ui_state.new_timedate.hour = ui_state.new_timedate.hour - 24 - - state.settings.utc_timezone; - else if(ui_state.new_timedate.hour - state.settings.utc_timezone < 0) - ui_state.new_timedate.hour = ui_state.new_timedate.hour + 24 - - state.settings.utc_timezone; - else - ui_state.new_timedate.hour += state.settings.utc_timezone; - rtc_setTime(ui_state.new_timedate); + // NOTE: The user inserted a local time, we must save an UTC time + curTime_t utc_time = state_getUTCTime(ui_state.new_timedate); + rtc_setTime(utc_time); + state.time = utc_time; state.ui_screen = SETTINGS_TIMEDATE; } else if(msg.keys & KEY_ESC) diff --git a/openrtx/src/ui/ui_main.c b/openrtx/src/ui/ui_main.c index 961e60d6..7fdce81b 100644 --- a/openrtx/src/ui/ui_main.c +++ b/openrtx/src/ui/ui_main.c @@ -35,9 +35,10 @@ void _ui_drawMainTop() { #ifdef HAS_RTC // Print clock on top bar + curTime_t local_time = state_getLocalTime(last_state.time); gfx_print(layout.top_pos, layout.top_font, TEXT_ALIGN_CENTER, - color_white, "%02d:%02d:%02d", last_state.time.hour, - last_state.time.minute, last_state.time.second); + color_white, "%02d:%02d:%02d", local_time.hour, + local_time.minute, local_time.second); #endif // If the radio has no built-in battery, print input voltage #ifdef BAT_NONE diff --git a/openrtx/src/ui/ui_menu.c b/openrtx/src/ui/ui_menu.c index f23d1499..6c9de830 100644 --- a/openrtx/src/ui/ui_menu.c +++ b/openrtx/src/ui/ui_menu.c @@ -447,16 +447,17 @@ void _ui_drawSettingsGPS(ui_state_t* ui_state) void _ui_drawSettingsTimeDate() { gfx_clearScreen(); + curTime_t local_time = state_getLocalTime(last_state.time); // Print "Time&Date" on top bar gfx_print(layout.top_pos, layout.top_font, TEXT_ALIGN_CENTER, color_white, "Time&Date"); // Print current time and date gfx_print(layout.line2_pos, layout.input_font, TEXT_ALIGN_CENTER, color_white, "%02d/%02d/%02d", - last_state.time.date, last_state.time.month, last_state.time.year); + local_time.date, local_time.month, local_time.year); gfx_print(layout.line3_pos, layout.input_font, TEXT_ALIGN_CENTER, color_white, "%02d:%02d:%02d", - last_state.time.hour, last_state.time.minute, last_state.time.second); + local_time.hour, local_time.minute, local_time.second); } void _ui_drawSettingsTimeDateSet(ui_state_t* ui_state)