Skip to content

Commit 95f5263

Browse files
committed
TimeLib: Workaround for newlib bug with <+-nn> timezone names
local WA for both esp8266 and esp32 'till fixed in both core's Fix for 8266 already in master esp8266/Arduino#7702 Signed-off-by: Emil Muratov <gpm@hotplug.ru>
1 parent 262f5aa commit 95f5263

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

EmbUI/timeProcessor.cpp

+25-13
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@
2424
#endif
2525

2626
#define TZ_DEFAULT PSTR("GMT0") // default Time-Zone
27+
static const char P_LOC[] PROGMEM = "LOC";
2728

2829
TimeProcessor::TimeProcessor()
2930
{
30-
// moved to the embui wifi management
31-
// eGotIPHandler = WiFi.onStationModeGotIP(std::bind(&TimeProcessor::onSTAGotIP, this, std::placeholders::_1));
32-
// eDisconnectHandler = WiFi.onStationModeDisconnected(std::bind(&TimeProcessor::onSTADisconnected, this, std::placeholders::_1));
3331
//configTzTime(); for esp32 https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-time.c
3432

3533
#ifdef ESP8266
@@ -44,18 +42,10 @@ TimeProcessor::TimeProcessor()
4442
*/
4543

4644
#ifdef TZONE
47-
#ifdef ESP8266
48-
configTime(TZONE, NTP1ADDRESS, NTP2ADDRESS);
49-
#elif defined ESP32
5045
configTzTime(TZONE, NTP1ADDRESS, NTP2ADDRESS);
51-
#endif
5246
LOG(print, F("TIME: Time Zone set to: ")); LOG(print, TZONE);
5347
#else
54-
#ifdef ESP8266
55-
configTime(TZ_DEFAULT, NTP1ADDRESS, NTP2ADDRESS);
56-
#elif defined ESP32
5748
configTzTime(TZ_DEFAULT, NTP1ADDRESS, NTP2ADDRESS);
58-
#endif
5949
#endif
6050

6151
sntp_stop(); // отключаем ntp пока нет подключения к AP
@@ -122,9 +112,31 @@ void TimeProcessor::setTime(const String &timestr){
122112
void TimeProcessor::tzsetup(const char* tz){
123113
// https://stackoverflow.com/questions/56412864/esp8266-timezone-issues
124114
if (!tz || !*tz)
125-
return;
115+
return;
116+
117+
/*
118+
* newlib has issues with TZ strings with quoted <+-nn> names
119+
* this has been fixed in https://github.com/esp8266/Arduino/pull/7702 for esp8266 (still not in stable as of Nov 16 2020)
120+
* but it also affects ESP32 and who knows when to expect a fix there
121+
* So let's fix such zones in-place untill core support for both platforms available
122+
*/
123+
if (tz[0] == 0x3C){ // check if first char is '<'
124+
String _tz(tz);
125+
String _tzfix((char *)0);
126+
_tzfix.reserve(sizeof(tz)) ;
127+
_tzfix += FPSTR(P_LOC);
128+
if (_tz.indexOf('<',1) > 0){ // there might be two <> quotes
129+
//LOG(print, "2nd pos: "); LOG(println, _tz.indexOf('<',1));
130+
_tzfix += _tz.substring(_tz.indexOf('>')+1, _tz.indexOf('<',1));
131+
_tzfix += FPSTR(P_LOC);
132+
}
133+
_tzfix += _tz.substring(_tz.lastIndexOf('>')+1, _tz.length());
134+
setenv("TZ", _tzfix.c_str(), 1/*overwrite*/);
135+
LOG(printf_P, PSTR("TIME: TZ fix applied: %s\n"), _tzfix.c_str());
136+
} else {
137+
setenv("TZ", tz, 1/*overwrite*/);
138+
}
126139

127-
setenv("TZ", tz, 1/*overwrite*/);
128140
tzset();
129141
tzone = ""; // сбрасываем костыльную зону
130142
usehttpzone = false; // запрещаем использование http

0 commit comments

Comments
 (0)