mirror of
https://github.com/esphome/esphome.git
synced 2024-11-29 18:24:13 +01:00
On epoch sync, restore local TZ (#3462)
Co-authored-by: Maurice Makaay <mmakaay1@xs4all.net>
This commit is contained in:
parent
f8a1bd4e79
commit
3f678e218d
2 changed files with 11 additions and 2 deletions
|
@ -13,11 +13,11 @@ static const char *const TAG = "time";
|
||||||
|
|
||||||
RealTimeClock::RealTimeClock() = default;
|
RealTimeClock::RealTimeClock() = default;
|
||||||
void RealTimeClock::call_setup() {
|
void RealTimeClock::call_setup() {
|
||||||
setenv("TZ", this->timezone_.c_str(), 1);
|
this->apply_timezone_();
|
||||||
tzset();
|
|
||||||
PollingComponent::call_setup();
|
PollingComponent::call_setup();
|
||||||
}
|
}
|
||||||
void RealTimeClock::synchronize_epoch_(uint32_t epoch) {
|
void RealTimeClock::synchronize_epoch_(uint32_t epoch) {
|
||||||
|
// Update UTC epoch time.
|
||||||
struct timeval timev {
|
struct timeval timev {
|
||||||
.tv_sec = static_cast<time_t>(epoch), .tv_usec = 0,
|
.tv_sec = static_cast<time_t>(epoch), .tv_usec = 0,
|
||||||
};
|
};
|
||||||
|
@ -30,6 +30,9 @@ void RealTimeClock::synchronize_epoch_(uint32_t epoch) {
|
||||||
ret = settimeofday(&timev, nullptr);
|
ret = settimeofday(&timev, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Move timezone back to local timezone.
|
||||||
|
this->apply_timezone_();
|
||||||
|
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
ESP_LOGW(TAG, "setimeofday() failed with code %d", ret);
|
ESP_LOGW(TAG, "setimeofday() failed with code %d", ret);
|
||||||
}
|
}
|
||||||
|
@ -41,6 +44,11 @@ void RealTimeClock::synchronize_epoch_(uint32_t epoch) {
|
||||||
this->time_sync_callback_.call();
|
this->time_sync_callback_.call();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RealTimeClock::apply_timezone_() {
|
||||||
|
setenv("TZ", this->timezone_.c_str(), 1);
|
||||||
|
tzset();
|
||||||
|
}
|
||||||
|
|
||||||
size_t ESPTime::strftime(char *buffer, size_t buffer_len, const char *format) {
|
size_t ESPTime::strftime(char *buffer, size_t buffer_len, const char *format) {
|
||||||
struct tm c_tm = this->to_c_tm();
|
struct tm c_tm = this->to_c_tm();
|
||||||
return ::strftime(buffer, buffer_len, format, &c_tm);
|
return ::strftime(buffer, buffer_len, format, &c_tm);
|
||||||
|
|
|
@ -137,6 +137,7 @@ class RealTimeClock : public PollingComponent {
|
||||||
void synchronize_epoch_(uint32_t epoch);
|
void synchronize_epoch_(uint32_t epoch);
|
||||||
|
|
||||||
std::string timezone_{};
|
std::string timezone_{};
|
||||||
|
void apply_timezone_();
|
||||||
|
|
||||||
CallbackManager<void()> time_sync_callback_;
|
CallbackManager<void()> time_sync_callback_;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue