diff --git a/src/Common/Settings.cpp b/src/Common/Settings.cpp index 041c6fec..4afe8f2e 100644 --- a/src/Common/Settings.cpp +++ b/src/Common/Settings.cpp @@ -84,31 +84,28 @@ static const char* KEY_HOSTNAME = "hostname"; static const char* KEY_AUTO_BRIGHTNESS_CTRL = "a_brightn_ctrl"; /** Plugin installation key */ -static const char* KEY_PLUGIN_INSTALLATION = "plugin_install"; +static const char* KEY_PLUGIN_INSTALLATION = "plugin_install"; -/** GMT offset key */ -static const char* KEY_GMTOFFSET = "gmt_offset"; - -/** Daylight saving time control key */ -static const char* KEY_DAYLIGHT_SAVING_CTRL = "dst_ctrl"; +/** POSIX timezone string key */ +static const char* KEY_TIMEZONE = "timezone"; /** NTP server key */ -static const char* KEY_NTP_SERVER = "ntp_server"; +static const char* KEY_NTP_SERVER = "ntp_server"; /** Time format key */ -static const char* KEY_TIME_FORMAT = "time_format"; +static const char* KEY_TIME_FORMAT = "time_format"; /** Date format key */ -static const char* KEY_DATE_FORMAT = "date_format"; +static const char* KEY_DATE_FORMAT = "date_format"; /** Max. number of display slots key */ -static const char* KEY_MAX_SLOTS = "max_slots"; +static const char* KEY_MAX_SLOTS = "max_slots"; /** Display slot configuration key */ -static const char* KEY_SLOT_CONFIG = "slot_cfg"; +static const char* KEY_SLOT_CONFIG = "slot_cfg"; /** Scroll pause key */ -static const char* KEY_SCROLL_PAUSE = "scroll_pause"; +static const char* KEY_SCROLL_PAUSE = "scroll_pause"; /* ---------- Key value pair names ---------- */ @@ -133,11 +130,8 @@ static const char* NAME_AUTO_BRIGHTNESS_CTRL = "Autom. brightness control /** Plugin installation name of key value pair */ static const char* NAME_PLUGIN_INSTALLATION = "Plugin installation"; -/** GMT offset name of key value pair */ -static const char* NAME_GMT_OFFSET = "GMT offset [s]"; - -/** DaylightSaving name of key value pair */ -static const char* NAME_DAYLIGHT_SAVING_CTRL = "Daylight saving control (DST)"; +/** POSIX timezone string name of key value pair. */ +static const char* NAME_TIMEZONE = "POSIX timezone string"; /** NTP server name of key value pair */ static const char* NAME_NTP_SERVER = "NTP server address"; @@ -180,11 +174,8 @@ static bool DEFAULT_AUTO_BRIGHTNESS_CTRL = false; /** Plugin installation default value */ static const char* DEFAULT_PLUGIN_INSTALLATION = ""; -/** GMT offset default value */ -static const int16_t DEFAULT_GMT_OFFSET = 0; - -/** Daylight saving control default value */ -static bool DEFAULT_DAYLIGHT_SAVING_CTRL = false; +/** POSIX timezone string default value */ +static const char* DEFAULT_TIMEZONE = "WEST-1DWEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00"; /** NTP server default value */ static const char* DEFAULT_NTP_SERVER = "pool.ntp.org"; @@ -226,10 +217,8 @@ static const size_t MIN_VALUE_HOSTNAME = 1U; /** Plugin installation min. length */ static const size_t MIN_VALUE_PLUGIN_INSTALLATION = 0U; -/** Min. GMT offset (-12h+60s = -43200s) length */ -static const int32_t MIN_VALUE_GMT_OFFSET = -43200; - -/* MIN_VALUE_DAYLIGHT_SAVING_CTRL */ +/** POSIX timezone min. length */ +static const size_t MIN_VALUE_TIMEZONE = 4U; /** NTP server address min. length */ static const size_t MIN_VALUE_NTP_SERVER = 12U; @@ -269,10 +258,8 @@ static const size_t MAX_VALUE_HOSTNAME = 63U; /** Plugin installation max. length */ static const size_t MAX_VALUE_PLUGIN_INSTALLATION = 512U; -/** Max. GMT offset (14h*60s = 50400s) length */ -static const int32_t MAX_VALUE_GMT_OFFSET = 50400; - -/* MAX_VALUE_DAYLIGHT_SAVING_CTRL */ +/** POSIX timezone max. length */ +static const size_t MAX_VALUE_TIMEZONE = 128U; /** NTP server address max. length */ static const size_t MAX_VALUE_NTP_SERVER = 30U; @@ -343,8 +330,7 @@ Settings::Settings() : m_hostname (m_preferences, KEY_HOSTNAME, NAME_HOSTNAME, DEFAULT_HOSTNAME, MIN_VALUE_HOSTNAME, MAX_VALUE_HOSTNAME), m_autoBrightnessCtrl (m_preferences, KEY_AUTO_BRIGHTNESS_CTRL, NAME_AUTO_BRIGHTNESS_CTRL, DEFAULT_AUTO_BRIGHTNESS_CTRL), m_pluginInstallation (m_preferences, KEY_PLUGIN_INSTALLATION, NAME_PLUGIN_INSTALLATION, DEFAULT_PLUGIN_INSTALLATION, MIN_VALUE_PLUGIN_INSTALLATION, MAX_VALUE_PLUGIN_INSTALLATION), - m_gmtOffset (m_preferences, KEY_GMTOFFSET, NAME_GMT_OFFSET, DEFAULT_GMT_OFFSET, MIN_VALUE_GMT_OFFSET, MAX_VALUE_GMT_OFFSET), - m_isDaylightSaving (m_preferences, KEY_DAYLIGHT_SAVING_CTRL, NAME_DAYLIGHT_SAVING_CTRL, DEFAULT_DAYLIGHT_SAVING_CTRL), + m_timezone (m_preferences, KEY_TIMEZONE, NAME_TIMEZONE, DEFAULT_TIMEZONE, MIN_VALUE_TIMEZONE, MAX_VALUE_TIMEZONE), m_ntpServer (m_preferences, KEY_NTP_SERVER, NAME_NTP_SERVER, DEFAULT_NTP_SERVER, MIN_VALUE_NTP_SERVER, MAX_VALUE_NTP_SERVER), m_timeFormatCtrl (m_preferences, KEY_TIME_FORMAT, NAME_TIME_FORMAT_CTRL, DEFAULT_TIME_FORMAT_CTRL), m_dateFormatCtrl (m_preferences, KEY_DATE_FORMAT, NAME_DATE_FORMAT_CTRL, DEFAULT_DATE_FORMAT_CTRL), @@ -359,14 +345,13 @@ Settings::Settings() : m_keyValueList[4] = &m_hostname; m_keyValueList[5] = &m_autoBrightnessCtrl; m_keyValueList[6] = &m_pluginInstallation; - m_keyValueList[7] = &m_gmtOffset; - m_keyValueList[8] = &m_isDaylightSaving; - m_keyValueList[9] = &m_ntpServer; - m_keyValueList[10] = &m_timeFormatCtrl; - m_keyValueList[11] = &m_dateFormatCtrl; - m_keyValueList[12] = &m_maxSlots; - m_keyValueList[13] = &m_slotConfig; - m_keyValueList[14] = &m_scrollPause; + m_keyValueList[7] = &m_timezone; + m_keyValueList[8] = &m_ntpServer; + m_keyValueList[9] = &m_timeFormatCtrl; + m_keyValueList[10] = &m_dateFormatCtrl; + m_keyValueList[11] = &m_maxSlots; + m_keyValueList[12] = &m_slotConfig; + m_keyValueList[13] = &m_scrollPause; } Settings::~Settings() diff --git a/src/Common/Settings.h b/src/Common/Settings.h index 55a46d65..41d440ea 100644 --- a/src/Common/Settings.h +++ b/src/Common/Settings.h @@ -167,23 +167,13 @@ class Settings } /** - * Get GMT offset. + * Get POSIX timezone string. * - * @return Key value pair - */ - KeyValueInt32& getGmtOffset() - { - return m_gmtOffset; - } - - /** - * Get state of daylight saving adjustment. - * - * @return Key value pair + * @return POSIC timezone string */ - KeyValueBool& getDaylightSavingAdjustment() + KeyValueString& getTimezone() { - return m_isDaylightSaving; + return m_timezone; } /** @@ -268,7 +258,7 @@ class Settings } /** Number of key value pairs. */ - static const uint8_t KEY_VALUE_PAIR_NUM = 15U; + static const uint8_t KEY_VALUE_PAIR_NUM = 14U; private: @@ -282,8 +272,7 @@ class Settings KeyValueString m_hostname; /**< Hostname */ KeyValueBool m_autoBrightnessCtrl; /**< Automatic brightness control switch */ KeyValueJson m_pluginInstallation; /**< Plugin installation */ - KeyValueInt32 m_gmtOffset; /**< GMT offset */ - KeyValueBool m_isDaylightSaving; /**< Daylight saving time switch */ + KeyValueString m_timezone; /**< POSIX timezone string */ KeyValueString m_ntpServer; /**< NTP server address */ KeyValueBool m_timeFormatCtrl; /**< Time format control */ KeyValueBool m_dateFormatCtrl; /**< Date format control */ diff --git a/src/Hal/ClockDrv.cpp b/src/Hal/ClockDrv.cpp index d2a1a7cb..1f5bd897 100644 --- a/src/Hal/ClockDrv.cpp +++ b/src/Hal/ClockDrv.cpp @@ -67,10 +67,8 @@ void ClockDrv::init() { if (false == m_isClockDrvInitialized) { - int32_t gmtOffset = 0; - int16_t daylightSavingValue = 0; + String timezone; String ntpServerAddress; - bool isDaylightSaving = false; struct tm timeInfo = { 0 }; /* Get the GMT offset, daylight saving enabled/disabled and NTP server address from persistent memory. */ @@ -78,27 +76,20 @@ void ClockDrv::init() { LOG_WARNING("Use default values for NTP request."); - gmtOffset = Settings::getInstance().getGmtOffset().getDefault(); - isDaylightSaving = Settings::getInstance().getDaylightSavingAdjustment().getDefault(); + timezone = Settings::getInstance().getTimezone().getDefault(); ntpServerAddress = Settings::getInstance().getNTPServerAddress().getDefault(); m_is24HourFormat = Settings::getInstance().getTimeFormatAdjustment().getDefault(); m_isDayMonthYear = Settings::getInstance().getDateFormatAdjustment().getDefault(); } else { - gmtOffset = Settings::getInstance().getGmtOffset().getValue(); - isDaylightSaving = Settings::getInstance().getDaylightSavingAdjustment().getValue(); + timezone = Settings::getInstance().getTimezone().getValue(); ntpServerAddress = Settings::getInstance().getNTPServerAddress().getValue(); m_is24HourFormat = Settings::getInstance().getTimeFormatAdjustment().getValue(); m_isDayMonthYear = Settings::getInstance().getDateFormatAdjustment().getValue(); Settings::getInstance().close(); } - if (true == isDaylightSaving) - { - daylightSavingValue = NTP_DAYLIGHT_OFFSET_SEC; - } - /* Configure NTP: * This will periodically synchronize the time. The time synchronization * period is determined by CONFIG_LWIP_SNTP_UPDATE_DELAY (default value is one hour). @@ -106,7 +97,7 @@ void ClockDrv::init() * https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/system_time.html * https://github.com/espressif/esp-idf/issues/4386 */ - configTime(gmtOffset, daylightSavingValue, ntpServerAddress.c_str()); + configTzTime(timezone.c_str(), ntpServerAddress.c_str()); /* Wait for synchronization (default 5s) */ if (false == getLocalTime(&timeInfo)) diff --git a/src/Hal/ClockDrv.h b/src/Hal/ClockDrv.h index 9a0d9634..d5f86e09 100644 --- a/src/Hal/ClockDrv.h +++ b/src/Hal/ClockDrv.h @@ -102,9 +102,6 @@ class ClockDrv */ bool getDateFormat(); - /** Daylight saving time offset in s */ - static const int16_t NTP_DAYLIGHT_OFFSET_SEC = 3600; - private: /** Flag indicating a initialized clock driver. */ diff --git a/src/Plugin/Plugins/SunrisePlugin.cpp b/src/Plugin/Plugins/SunrisePlugin.cpp index 09f690af..9ee8afeb 100644 --- a/src/Plugin/Plugins/SunrisePlugin.cpp +++ b/src/Plugin/Plugins/SunrisePlugin.cpp @@ -444,33 +444,31 @@ void SunrisePlugin::initHttpClient() String SunrisePlugin::addCurrentTimezoneValues(const String& dateTimeString) const { - tm timeInfo; + tm gmTimeInfo; + tm* lcTimeInfo = nullptr; + tm cvTimeInfo; + time_t gmTime; char timeBuffer[17] = { 0 }; - int16_t gmtOffset = 0; - int16_t isDaylightSaving = 0; - const char* formattedTimeString = ClockDrv::getInstance().getTimeFormat() ? "%H:%M":"%I:%M %p"; + const char* formattedTimeString = ClockDrv::getInstance().getTimeFormat() ? "%H:%M" : "%I:%M %p"; bool isPM = dateTimeString.endsWith("PM"); - /* Get the GMT offset and daylight saving enabled/disabled from persistent memory. */ - if (false == Settings::getInstance().open(true)) - { - LOG_WARNING("Use default values."); - gmtOffset = Settings::getInstance().getGmtOffset().getDefault(); - isDaylightSaving = Settings::getInstance().getDaylightSavingAdjustment().getDefault(); - } - else + /* Convert date/time string to GMT time information */ + (void)strptime(dateTimeString.c_str(), "%Y-%m-%dT%H:%M:%S", &gmTimeInfo); + + /* Convert to local time */ + gmTime = mktime(&gmTimeInfo); + lcTimeInfo = localtime(&gmTime); + + /* Consider AM/PM */ + cvTimeInfo = *lcTimeInfo; + + if (true == isPM) { - gmtOffset = Settings::getInstance().getGmtOffset().getValue(); - isDaylightSaving = Settings::getInstance().getDaylightSavingAdjustment().getValue(); - Settings::getInstance().close(); + cvTimeInfo.tm_hour += 12; } - strptime(dateTimeString.c_str(), "%Y-%m-%dT%H:%M:%S" ,&timeInfo); - timeInfo.tm_hour += gmtOffset /3600; - timeInfo.tm_hour += isDaylightSaving; - timeInfo.tm_hour += isPM * 12; - - strftime(timeBuffer, sizeof(timeBuffer), formattedTimeString, &timeInfo); + /* Convert time information to user friendly string. */ + (void)strftime(timeBuffer, sizeof(timeBuffer), formattedTimeString, &cvTimeInfo); return timeBuffer; } diff --git a/src/Plugin/Plugins/SunrisePlugin.h b/src/Plugin/Plugins/SunrisePlugin.h index e691f097..3c0ef81e 100644 --- a/src/Plugin/Plugins/SunrisePlugin.h +++ b/src/Plugin/Plugins/SunrisePlugin.h @@ -282,9 +282,10 @@ class SunrisePlugin : public Plugin /** * Add the daylight saving (if available) and GMT offset values to the given - * dateTime string + * date/time string. + * sunrise-sunset.org API: https://sunrise-sunset.org/api * - * @param[in] dateTimeString dateTime string received via calling the sunrise-sunset.org API. + * @param[in] dateTimeString Date/Time string in UTC received via calling the sunrise-sunset.org API. * * @return A formatted (timezone adjusted) time string according to the configured time format. */