Skip to content

Commit 3b69163

Browse files
committed
Change time zone handling to store the text (ie; 'Europe/Stockholm) instead of the actual POSIX format, since multiple places uses the same time zone string.
1 parent 98bf218 commit 3b69163

File tree

5 files changed

+39
-20
lines changed

5 files changed

+39
-20
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=JBWopr
2-
version=1.3.0
2+
version=1.3.1
33
author=Jonny Bergdahl
44
maintainer=Jonny Bergdahl <[email protected]>
55
sentence=Support library for the Unexpected Maker W.O.P.R. boards

src/jbwoprhelpers.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@
66
/// @date Created: 2023-10-02
77
/// @copyright Copyright© 2023, Jonny Bergdahl
88
#include "jbwoprhelpers.h"
9+
#include "tz_data.h"
910
#include <WiFi.h>
1011
#include <HTTPClient.h>
1112

1213
bool JBTimeHelper::_isInitialized = false;
1314
std::string JBTimeHelper::_ntpServer = "";
14-
std::string JBTimeHelper::_tzString = "";
15+
std::string JBTimeHelper::_tzName = "";
1516
JBLogger* JBTimeHelper::_log = nullptr;
1617

17-
void JBTimeHelper::configure(JBLogger* log, std::string ntpServer, std::string tzString) {
18+
void JBTimeHelper::configure(JBLogger* log, std::string ntpServer, std::string tzName) {
1819
_log = log;
1920
_ntpServer = ntpServer;
20-
_tzString = tzString;
21+
_tzName = tzName;
2122
}
2223

2324
bool JBTimeHelper::getTime(tm* info) {
@@ -27,9 +28,23 @@ bool JBTimeHelper::getTime(tm* info) {
2728
if (hasWiFi && !_isInitialized) {
2829
_log->trace("Obtain local time");
2930

30-
if (!_tzString.empty()) {
31-
_log->trace("Using TZ string: %s", _tzString.c_str());
32-
configTzTime(_tzString.c_str(), _ntpServer.c_str());
31+
if (!_tzName.empty()) {
32+
std::string tzString = "";
33+
for (int i = 0; i < TZ_DATA_COUNT; i++) {
34+
if (_tzName == TZ_DATA[i].n) {
35+
tzString = TZ_DATA[i].v;
36+
break;
37+
}
38+
}
39+
40+
if (!tzString.empty()) {
41+
_log->trace("Using TZ string: %s (%s)", tzString.c_str(), _tzName.c_str());
42+
configTzTime(tzString.c_str(), _ntpServer.c_str());
43+
}
44+
else {
45+
_log->warning("Timezone name not found: %s. Using UTC.", _tzName.c_str());
46+
configTime(0, 0, _ntpServer.c_str());
47+
}
3348
}
3449
else {
3550
_log->trace("Using UTC");

src/jbwoprhelpers.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ enum JBTextAlignment {
3434
class JBTimeHelper {
3535
public:
3636
/// @brief Configure helper with NTP Server and time offset
37-
/// @param config WiFi configuration
38-
static void configure(JBLogger* log, std::string ntpServer = "", std::string tzString = "");
37+
/// @param log Logger
38+
/// @param ntpServer NTP server address
39+
/// @param tzName Timezone name (e.g. Europe/Stockholm)
40+
static void configure(JBLogger* log, std::string ntpServer = "", std::string tzName = "");
3941

4042
/// @brief Get local time
4143
/// @param ntpServer NTP server address
@@ -54,7 +56,7 @@ class JBTimeHelper {
5456
private:
5557
static bool _isInitialized; ///< True if done initializing
5658
static std::string _ntpServer; ///< NTP server
57-
static std::string _tzString; ///< Timezone string
59+
static std::string _tzName; ///< Timezone name
5860
static JBLogger* _log; ///< Local logger
5961
};
6062

src/jbwoprwifi.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ bool JBWoprWiFiDevice::begin(JBWoprBoardVariant variant, JBWoprBoardPins pins) {
4545
_loadConfiguration();
4646
}
4747

48-
JBTimeHelper::configure(_log, _wifiConfig.ntpServer, _wifiConfig.tzString);
48+
JBTimeHelper::configure(_log, _wifiConfig.ntpServer, _wifiConfig.tzName);
4949
_setupWiFiManager();
5050

5151
displayShowText("Start WiFi");
@@ -225,7 +225,7 @@ void JBWoprWiFiDevice::_setConfigFromJsonDocument(const JsonDocument &jsonDoc) {
225225
_wifiConfig.ntpServer = jsonDoc[JSON_KEY_WIFI_NTP_SERVER].as<std::string>();
226226
}
227227
if (!jsonDoc[JSON_KEY_WIFI_TIMEZONE].isNull()) {
228-
_wifiConfig.tzString = jsonDoc[JSON_KEY_WIFI_TIMEZONE].as<std::string>();
228+
_wifiConfig.tzName = jsonDoc[JSON_KEY_WIFI_TIMEZONE].as<std::string>();
229229
}
230230
if (!jsonDoc[JSON_KEY_WIFI_USE_WEB_PORTAL].isNull()) {
231231
_wifiConfig.useWebPortal = jsonDoc[JSON_KEY_WIFI_USE_WEB_PORTAL].as<bool>();
@@ -240,7 +240,7 @@ void JBWoprWiFiDevice::_setJsonDocumentFromConfig(JsonDocument &jsonDoc) {
240240
jsonDoc[JSON_KEY_EFFECTS_TIMEOUT] = _config.effectsTimeout;
241241
jsonDoc[JSON_KEY_WIFI_HOST_NAME] = _wifiConfig.hostName;
242242
jsonDoc[JSON_KEY_WIFI_NTP_SERVER] = _wifiConfig.ntpServer;
243-
jsonDoc[JSON_KEY_WIFI_TIMEZONE] = _wifiConfig.tzString;
243+
jsonDoc[JSON_KEY_WIFI_TIMEZONE] = _wifiConfig.tzName;
244244
jsonDoc[JSON_KEY_WIFI_USE_WEB_PORTAL] = _wifiConfig.useWebPortal;
245245
}
246246

@@ -253,7 +253,7 @@ void JBWoprWiFiDevice::_dumpConfig() {
253253
_log->trace(" Effects timeout: %u", _config.effectsTimeout);
254254
_log->trace(" Host name: %s", _wifiConfig.hostName.c_str());
255255
_log->trace(" NTP server: %s", _wifiConfig.ntpServer.c_str());
256-
_log->trace(" Timezone string: %s", _wifiConfig.tzString.c_str());
256+
_log->trace(" Timezone name: %s", _wifiConfig.tzName.c_str());
257257
_log->trace(" Use web portal: %s", _wifiConfig.useWebPortal ? "true" : "false");
258258
}
259259

@@ -297,21 +297,23 @@ void JBWoprWiFiDevice::_setupWiFiManager() {
297297
_hostNameParam = new WiFiManagerParameter(JSON_KEY_WIFI_HOST_NAME, "Host name", _wifiConfig.hostName.c_str(), 40);
298298
_ntpServerNameParam = new WiFiManagerParameter(JSON_KEY_WIFI_NTP_SERVER, "NTP server", _wifiConfig.ntpServer.c_str(), 40);
299299

300-
_tzHtml = "<br/><label for='tz_select'>Timezone</label><select id='tz_select' name='tz_select'>";
300+
_tzHtml = "<input id='tzString' name='tzString' type='hidden' value='";
301+
_tzHtml += _wifiConfig.tzName;
302+
_tzHtml += "'><br/><label for='tz_select'>Timezone</label><select id='tz_select' name='tz_select'>";
301303
_tzHtml.reserve(40 * 1024);
302304
_tzHtml += "<option value=''>UTC</option>";
303305
for (int i = 0; i < TZ_DATA_COUNT; i++) {
304306
_tzHtml += "<option value='";
305-
_tzHtml += TZ_DATA[i].v;
307+
_tzHtml += TZ_DATA[i].n;
306308
_tzHtml += "'";
307-
if (_wifiConfig.tzString == TZ_DATA[i].v) {
309+
if (_wifiConfig.tzName == TZ_DATA[i].n) {
308310
_tzHtml += " selected";
309311
}
310312
_tzHtml += ">";
311313
_tzHtml += TZ_DATA[i].n;
312314
_tzHtml += "</option>";
313315
}
314-
_tzHtml += "</select>";
316+
_tzHtml += "</select><script>document.getElementById('tz_select').onchange = function() { document.getElementById('tzString').value = this.value; };</script>";
315317
_tzStringParam = new WiFiManagerParameter(_tzHtml.c_str());
316318

317319
_useWebPortalParam = new WiFiManagerParameter(JSON_KEY_WIFI_USE_WEB_PORTAL, "Use web portal", "T", 2, _wifiConfig.useWebPortal ? HTML_CHECKBOX_TRUE : HTML_CHECKBOX_FALSE, WFM_LABEL_AFTER);
@@ -342,7 +344,7 @@ void JBWoprWiFiDevice::_saveParamsCallback () {
342344
_config.effectsTimeout = atoi(_effectsTimeoutParam->getValue());
343345
_wifiConfig.hostName = _hostNameParam->getValue();
344346
_wifiConfig.ntpServer = _ntpServerNameParam->getValue();
345-
_wifiConfig.tzString = _wifiManager->server->arg("tz_select").c_str();
347+
_wifiConfig.tzName = _wifiManager->server->arg(JSON_KEY_WIFI_TIMEZONE).c_str();
346348
_wifiConfig.useWebPortal = strncmp(_useWebPortalParam->getValue(), "T", 1) == 0;
347349
_shouldSaveConfig = true;
348350
}

src/jbwoprwifi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
struct JBWoprWiFiConfiguration {
2525
std::string hostName; ///< Host name
2626
std::string ntpServer; ///< NTP server address
27-
std::string tzString; ///< Timezone string (POSIX format)
27+
std::string tzName; ///< Timezone name (e.g. Europe/Stockholm)
2828
bool useWebPortal; ///< Use web portal
2929
};
3030

0 commit comments

Comments
 (0)