Now state.time is UTC, add functions to convert from UTC to local and reverse

This commit is contained in:
Federico Amedeo Izzo 2021-04-10 15:39:59 +02:00
parent 56c598da7e
commit 341c46a263
6 changed files with 39 additions and 31 deletions

View File

@ -128,9 +128,15 @@ void state_init();
void state_terminate(); void state_terminate();
/** /**
* This function applies the selected timezone after reading the time from * The RTC and state.time are set to UTC time
* the RTC. * 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 */ #endif /* STATE_H */

View File

@ -37,7 +37,6 @@ void state_init()
state.radioStateUpdated = true; state.radioStateUpdated = true;
#ifdef HAS_RTC #ifdef HAS_RTC
state.time = rtc_getTime(); state.time = rtc_getTime();
state_applyTimezone();
#endif #endif
state.v_bat = platform_getVbat(); state.v_bat = platform_getVbat();
state.charge = battery_getCharge(state.v_bat); state.charge = battery_getCharge(state.v_bat);
@ -93,18 +92,26 @@ void state_terminate()
//nvm_writeSettings(&state.settings); //nvm_writeSettings(&state.settings);
} }
void state_applyTimezone() curTime_t state_getLocalTime(curTime_t utc_time)
{ {
if(state.time.hour + state.settings.utc_timezone >= 24) curTime_t local_time = utc_time;
{ if(local_time.hour + state.settings.utc_timezone >= 24)
state.time.hour = state.time.hour - 24 + state.settings.utc_timezone; local_time.hour = local_time.hour - 24 + state.settings.utc_timezone;
} else if(local_time.hour + state.settings.utc_timezone < 0)
else if(state.time.hour + state.settings.utc_timezone < 0) local_time.hour = local_time.hour + 24 + state.settings.utc_timezone;
{
state.time.hour = state.time.hour + 24 + state.settings.utc_timezone;
}
else else
{ local_time.hour += state.settings.utc_timezone;
state.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;
} }

View File

@ -222,7 +222,6 @@ void *dev_task(void *arg)
#ifdef HAS_RTC #ifdef HAS_RTC
state.time = rtc_getTime(); state.time = rtc_getTime();
state_applyTimezone();
#endif #endif
// Low-pass filtering with a time constant of 10s when updated at 1Hz // Low-pass filtering with a time constant of 10s when updated at 1Hz

View File

@ -1036,16 +1036,10 @@ void ui_updateFSM(event_t event, bool *sync_rtx)
if(ui_state.input_position < TIMEDATE_DIGITS) if(ui_state.input_position < TIMEDATE_DIGITS)
break; break;
// Return to Time&Date menu, saving values // Return to Time&Date menu, saving values
// NOTE: The user inserted a local time, we save an UTC time to the RTC // NOTE: The user inserted a local time, we must save an UTC time
if(ui_state.new_timedate.hour - state.settings.utc_timezone >= 24) curTime_t utc_time = state_getUTCTime(ui_state.new_timedate);
ui_state.new_timedate.hour = ui_state.new_timedate.hour - 24 - rtc_setTime(utc_time);
state.settings.utc_timezone; state.time = utc_time;
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);
state.ui_screen = SETTINGS_TIMEDATE; state.ui_screen = SETTINGS_TIMEDATE;
} }
else if(msg.keys & KEY_ESC) else if(msg.keys & KEY_ESC)

View File

@ -35,9 +35,10 @@ void _ui_drawMainTop()
{ {
#ifdef HAS_RTC #ifdef HAS_RTC
// Print clock on top bar // 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, gfx_print(layout.top_pos, layout.top_font, TEXT_ALIGN_CENTER,
color_white, "%02d:%02d:%02d", last_state.time.hour, color_white, "%02d:%02d:%02d", local_time.hour,
last_state.time.minute, last_state.time.second); local_time.minute, local_time.second);
#endif #endif
// If the radio has no built-in battery, print input voltage // If the radio has no built-in battery, print input voltage
#ifdef BAT_NONE #ifdef BAT_NONE

View File

@ -447,16 +447,17 @@ void _ui_drawSettingsGPS(ui_state_t* ui_state)
void _ui_drawSettingsTimeDate() void _ui_drawSettingsTimeDate()
{ {
gfx_clearScreen(); gfx_clearScreen();
curTime_t local_time = state_getLocalTime(last_state.time);
// Print "Time&Date" on top bar // Print "Time&Date" on top bar
gfx_print(layout.top_pos, layout.top_font, TEXT_ALIGN_CENTER, gfx_print(layout.top_pos, layout.top_font, TEXT_ALIGN_CENTER,
color_white, "Time&Date"); color_white, "Time&Date");
// Print current time and date // Print current time and date
gfx_print(layout.line2_pos, layout.input_font, TEXT_ALIGN_CENTER, gfx_print(layout.line2_pos, layout.input_font, TEXT_ALIGN_CENTER,
color_white, "%02d/%02d/%02d", 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, gfx_print(layout.line3_pos, layout.input_font, TEXT_ALIGN_CENTER,
color_white, "%02d:%02d:%02d", 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) void _ui_drawSettingsTimeDateSet(ui_state_t* ui_state)