mirror of
https://github.com/esphome/esphome.git
synced 2025-10-23 12:13:49 +01:00
[api][time] Refactor timezone update logic for cleaner code
This commit is contained in:
@@ -1081,13 +1081,8 @@ void APIConnection::on_get_time_response(const GetTimeResponse &value) {
|
|||||||
homeassistant::global_homeassistant_time->set_epoch_time(value.epoch_seconds);
|
homeassistant::global_homeassistant_time->set_epoch_time(value.epoch_seconds);
|
||||||
#ifdef USE_TIME_TIMEZONE
|
#ifdef USE_TIME_TIMEZONE
|
||||||
if (value.timezone_len > 0) {
|
if (value.timezone_len > 0) {
|
||||||
const std::string ¤t_tz = homeassistant::global_homeassistant_time->get_timezone();
|
homeassistant::global_homeassistant_time->set_timezone(reinterpret_cast<const char *>(value.timezone),
|
||||||
// Compare without allocating a string
|
value.timezone_len);
|
||||||
if (current_tz.length() != value.timezone_len ||
|
|
||||||
memcmp(current_tz.c_str(), value.timezone, value.timezone_len) != 0) {
|
|
||||||
homeassistant::global_homeassistant_time->set_timezone(
|
|
||||||
std::string(reinterpret_cast<const char *>(value.timezone), value.timezone_len));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
@@ -27,6 +27,14 @@ class RealTimeClock : public PollingComponent {
|
|||||||
this->apply_timezone_();
|
this->apply_timezone_();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set the time zone from raw buffer, only if it differs from the current one.
|
||||||
|
void set_timezone(const char *tz, size_t len) {
|
||||||
|
if (this->timezone_.length() != len || memcmp(this->timezone_.c_str(), tz, len) != 0) {
|
||||||
|
this->timezone_.assign(tz, len);
|
||||||
|
this->apply_timezone_();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Get the time zone currently in use.
|
/// Get the time zone currently in use.
|
||||||
std::string get_timezone() { return this->timezone_; }
|
std::string get_timezone() { return this->timezone_; }
|
||||||
#endif
|
#endif
|
||||||
|
Reference in New Issue
Block a user