Skip to content

Commit

Permalink
Add option to set timezone minutes offset & Fix wrong timezone hour o…
Browse files Browse the repository at this point in the history
…ffset
  • Loading branch information
bkerler authored and vorner committed Jan 23, 2024
1 parent 0ba6a80 commit 89985d8
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 8 deletions.
39 changes: 39 additions & 0 deletions src/gui/MItem_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,45 @@ void MI_TIMEZONE::OnClick() {
config_store().timezone.set(timezone);
}

/*****************************************************************************/
// MI_TIMEZONE_MIN
MI_TIMEZONE_MIN::MI_TIMEZONE_MIN()
: WI_SWITCH_t<3>(static_cast<uint8_t>(time_tools::get_timezone_minutes_offset()), _(label), nullptr, is_enabled_t::yes, is_hidden_t::no, _(str_0min), _(str_30min), _(str_45min)) {}

void MI_TIMEZONE_MIN::OnChange([[maybe_unused]] size_t old_index) {
switch (index) {
case 0:
time_tools::set_timezone_minutes_offset(time_tools::TimeOffsetMinutes::_0min);
break;
case 1:
time_tools::set_timezone_minutes_offset(time_tools::TimeOffsetMinutes::_30min);
break;
case 2:
time_tools::set_timezone_minutes_offset(time_tools::TimeOffsetMinutes::_45min);
break;
default:
assert(0);
}
}

/*****************************************************************************/
// MI_TIMEZONE_SUMMER
MI_TIMEZONE_SUMMER::MI_TIMEZONE_SUMMER()
: WI_SWITCH_t<2>(static_cast<uint8_t>(time_tools::get_timezone_summertime_offset()), _(label), nullptr, is_enabled_t::yes, is_hidden_t::no, _(str_wintertime), _(str_summertime)) {}

void MI_TIMEZONE_SUMMER::OnChange([[maybe_unused]] size_t old_index) {
switch (index) {
case 0:
time_tools::set_timezone_summertime_offset(time_tools::TimeOffsetSummerTime::_wintertime);
break;
case 1:
time_tools::set_timezone_summertime_offset(time_tools::TimeOffsetSummerTime::_summertime);
break;
default:
assert(0);
}
}

/*****************************************************************************/
// MI_TIME_FORMAT
MI_TIME_FORMAT::MI_TIME_FORMAT()
Expand Down
25 changes: 24 additions & 1 deletion src/gui/MItem_tools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,36 @@ class MI_SOUND_VOLUME : public WiSpinInt {
};

class MI_TIMEZONE : public WiSpinInt {
constexpr static const char *const label = N_("Time Zone Offset");
constexpr static const char *const label = N_("Time Zone Hour Offset");

public:
MI_TIMEZONE();
virtual void OnClick() override;
};

class MI_TIMEZONE_MIN : public WI_SWITCH_t<3> {
constexpr static const char *const label = N_("Time Zone Minute Offset");

constexpr static const char *str_0min = N_("00 min");
constexpr static const char *str_30min = N_("30 min");
constexpr static const char *str_45min = N_("45 min");

public:
MI_TIMEZONE_MIN();
virtual void OnChange(size_t old_index) override;
};

class MI_TIMEZONE_SUMMER : public WI_SWITCH_t<2> {
constexpr static const char *const label = N_("Time Zone Summertime");

constexpr static const char *str_wintertime = N_("disabled");
constexpr static const char *str_summertime = N_("enabled");

public:
MI_TIMEZONE_SUMMER();
virtual void OnChange(size_t old_index) override;
};

class MI_TIME_FORMAT : public WI_SWITCH_t<2> {
constexpr static const char *const label = N_("Time Format");

Expand Down
8 changes: 6 additions & 2 deletions src/gui/print_time_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ PT_t PrintTime::update_loop(PT_t screen_format, window_text_t *out_print_end, [[

// Timestamp
const int8_t timezone_diff = config_store().timezone.get();
const time_t local_cur_sec = curr_sec + timezone_diff * 3600;
const int8_t timezone_summertime = time_tools::get_current_timezone_summertime();
const int8_t timezone_min_diff = time_tools::get_current_timezone_minutes();
const time_t local_cur_sec = curr_sec + ((timezone_diff + timezone_summertime) * 3600) + (timezone_min_diff * 60);
time_end_format = PT_t::timestamp;
generate_timestamp_string(local_cur_sec, time_to_end);

Expand Down Expand Up @@ -119,7 +121,9 @@ bool PrintTime::print_end_time(const uint32_t time_to_end, std::span<char> buffe
}

const int8_t timezone_diff = config_store().timezone.get();
curr_sec += timezone_diff * 3600;
const int8_t timezone_summertime = time_tools::get_current_timezone_summertime();
const int8_t timezone_min_diff = time_tools::get_current_timezone_minutes();
curr_sec += ((timezone_diff + timezone_summertime) * 3600) + (timezone_min_diff * 60);

print_timestamp_string_to_buffer(curr_sec, time_to_end, buffer);
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/gui/screen_menu_lang_and_time.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "MItem_menus.hpp"
#include "menu_items_languages.hpp"

using ScreenMenuLangAndTime__ = ScreenMenu<GuiDefaults::MenuFooter, MI_RETURN, MI_LANGUAGE, MI_TIMEZONE, MI_TIME_FORMAT
using ScreenMenuLangAndTime__ = ScreenMenu<GuiDefaults::MenuFooter, MI_RETURN, MI_LANGUAGE, MI_TIMEZONE, MI_TIMEZONE_MIN, MI_TIMEZONE_SUMMER, MI_TIME_FORMAT
#if PRINTER_IS_PRUSA_MINI
,
MI_TIME_NOW // Mini does not show time in header, so show it here
Expand Down
2 changes: 1 addition & 1 deletion src/gui/screen_menu_tune.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ using ScreenMenuTune__ = ScreenMenu<EFooter::On, MI_RETURN,
#if (!PRINTER_IS_PRUSA_MINI) || defined(_DEBUG) // Save space in MINI release
MI_HARDWARE_TUNE,
#endif /*(!PRINTER_IS_PRUSA_MINI) || defined(_DEBUG)*/
MI_TIMEZONE, MI_INFO, MI_TRIGGER_POWER_PANIC,
MI_TIMEZONE, MI_TIMEZONE_MIN, MI_TIMEZONE_SUMMER, MI_INFO, MI_TRIGGER_POWER_PANIC,

#ifdef _DEBUG
MI_TEST,
Expand Down
3 changes: 2 additions & 1 deletion src/gui/screen_printing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,8 @@ void screen_printing_data_t::start_showing_end_result() {
localtime_r(&print_time, &print_tm);
});

print_tm.tm_hour += config_store().timezone.get();
print_tm.tm_hour += config_store().timezone.get() + time_tools::get_current_timezone_summertime();
print_tm.tm_min += time_tools::get_current_timezone_minutes();

const time_t adjusted_print_time = mktime(&print_tm);
localtime_r(&adjusted_print_time, &print_tm);
Expand Down
57 changes: 56 additions & 1 deletion src/gui/time_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,58 @@ TimeFormat get_time_format() {
return config_store().time_format.get();
}

int8_t get_current_timezone_minutes() {
bool neg = config_store().timezone.get() < 0;
switch (config_store().timezone_minutes.get()) {
case TimeOffsetMinutes::_0min:
return 0;
case TimeOffsetMinutes::_30min:
if (neg) {
return -30;
} else {
return 30;
}
case TimeOffsetMinutes::_45min:
if (neg) {
return -45;
} else {
return 45;
}
default:
assert(0);
}
return 0;
}

void set_timezone_minutes_offset(TimeOffsetMinutes new_offset) {
config_store().timezone_minutes.set(new_offset);
}

TimeOffsetMinutes get_timezone_minutes_offset() {
return config_store().timezone_minutes.get();
}

int8_t get_current_timezone_summertime() {
switch (config_store().timezone_summer.get()) {
case TimeOffsetSummerTime::_summertime:
return 1;
case TimeOffsetSummerTime::_wintertime:
return 0;
default:
assert(0);
break;
}
return 0;
}

void set_timezone_summertime_offset(TimeOffsetSummerTime new_offset) {
config_store().timezone_summer.set(new_offset);
}

TimeOffsetSummerTime get_timezone_summertime_offset() {
return config_store().timezone_summer.get();
}

namespace {
char text_buffer[] = "--:-- --"; ///< Buffer for time string, needs to fit "01:23 am"
struct tm last_time = {}; ///< Last time used to print to text_buffer
Expand All @@ -23,7 +75,10 @@ bool update_time() {
if (t != (time_t)-1) { // Time is initialized in RTC (from sNTP)
struct tm now;
int8_t timezone_diff = config_store().timezone.get();
t += timezone_diff * 3600;
int8_t timezone_summertime = get_current_timezone_summertime();
int8_t timezone_min_diff = get_current_timezone_minutes();
t += (timezone_diff + timezone_summertime) * 3600;
t += timezone_min_diff * 60;
localtime_r(&t, &now);

TimeFormat current_format = config_store().time_format.get();
Expand Down
39 changes: 39 additions & 0 deletions src/gui/time_tools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,45 @@ enum class TimeFormat : uint8_t {
_24h,
};

/**
* @brief Time zone offset in minutes.
* @warning Never change these values. They are stored in config_store.
*/

enum class TimeOffsetMinutes : int8_t {
_0min,
_30min,
_45min,
};

/**
* @brief Time zone summertime offset.
* @warning Never change these values. They are stored in config_store.
*/

/// @return current timezone offset in minutes as integer value (minutes * seconds)
int8_t get_current_timezone_minutes();

/// @param new_offset set timezone minute offset
void set_timezone_minutes_offset(TimeOffsetMinutes new_offset);

/// @return current timezone minutes offset
TimeOffsetMinutes get_timezone_minutes_offset();

enum class TimeOffsetSummerTime : int8_t {
_wintertime,
_summertime
};

/// @return current timezone summertime offset in hours
int8_t get_current_timezone_summertime();

/// @param new_offset set timezone summertime offset
void set_timezone_summertime_offset(TimeOffsetSummerTime new_offset);

/// @return current timezone summertime offset
TimeOffsetSummerTime get_timezone_summertime_offset();

/// @param new_format set time format
void set_time_format(TimeFormat new_format);

Expand Down
4 changes: 3 additions & 1 deletion src/guiapi/src/menu_spin_config_with_units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ static constexpr const char *Celsius = "\xC2\xB0\x43"; // degree Celsius
static constexpr const char *Percent = "%";
static constexpr const char *None = "";
static constexpr const char *Hour = "h";
static constexpr const char *Minutes = "min";
static constexpr const char *mm = "mm";
static constexpr const char *mA = "mA";
static constexpr const char *rpm = "rpm"; // todo should I translate it?
Expand All @@ -23,7 +24,8 @@ const SpinConfigInt SpinCnf::bed = SpinConfigInt(MenuVars::GetBedRange(), Celsiu
const SpinConfigInt SpinCnf::printfan = SpinConfigInt(MenuVars::percent_range, Percent, spin_off_opt_t::yes);
const SpinConfigInt SpinCnf::feedrate = SpinConfigInt(MenuVars::feedrate_range, Percent);
const SpinConfigInt SpinCnf::flowfact = SpinConfigInt(MenuVars::flowfact_range, Percent);
const SpinConfigInt SpinCnf::timezone_range = { { -12, 12, 1 }, Hour };
const SpinConfigInt SpinCnf::timezone_range = { { -12, 14, 1 }, Hour };

#if BOARD_IS_BUDDY
const SpinConfigInt SpinCnf::volume_range = { { 0, 11, 1 }, None, spin_off_opt_t::yes }; // crank it up to 11
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ namespace defaults {

inline constexpr std::array<char, lan_hostname_max_len + 1> net_hostname { LAN_HOSTNAME_DEF };
inline constexpr int8_t lan_timezone { 1 };
inline constexpr time_tools::TimeOffsetMinutes timezone_minutes { time_tools::TimeOffsetMinutes::_0min };
inline constexpr time_tools::TimeOffsetSummerTime timezone_summer { time_tools::TimeOffsetSummerTime::_wintertime };
inline constexpr std::array<char, wifi_max_ssid_len + 1> wifi_ap_ssid { "" };
inline constexpr std::array<char, wifi_max_passwd_len + 1> wifi_ap_password { "" };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ struct CurrentStore : public journal::CurrentStoreConfig<journal::Backend, backe
StoreItem<std::array<char, lan_hostname_max_len + 1>, defaults::net_hostname, journal::hash("LAN Hostname")> lan_hostname;

StoreItem<int8_t, defaults::lan_timezone, journal::hash("LAN Timezone")> timezone; // hour difference from UTC
StoreItem<time_tools::TimeOffsetMinutes, defaults::timezone_minutes, journal::hash("Timezone Minutes")> timezone_minutes; // minutes offset for hour difference from UTC
StoreItem<time_tools::TimeOffsetSummerTime, defaults::timezone_summer, journal::hash("Timezone Summertime")> timezone_summer; // Summertime hour offset

// WIFI settings
// wifi_flag & 1 -> On = 0/off = 1, lan_flag & 2 -> dhcp = 0/static = 1, wifi_flag & 0b1100 -> reserved, previously ap_sec_t security
Expand Down

0 comments on commit 89985d8

Please sign in to comment.