Skip to content

Commit abdd2bd

Browse files
authored
simple TZ api: bypass sprintf/sscanf: + 7KB (#7109)
* simple TZ api: bypass sprintf/sscanf: + 7KB
1 parent 4b2bf45 commit abdd2bd

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

cores/esp8266/time.cpp

+38-2
Original file line numberDiff line numberDiff line change
@@ -133,21 +133,57 @@ int _gettimeofday_r(struct _reent* unused, struct timeval *tp, void *tzp)
133133

134134
void configTime(int timezone_sec, int daylightOffset_sec, const char* server1, const char* server2, const char* server3)
135135
{
136-
char tzstr [64];
137-
138136
// There is no way to tell when DST starts or stop with this API
139137
// So DST is always integrated in TZ
140138
// The other API should be preferred
141139

140+
/*** portable version using posix API
141+
(calls sprintf here, then sscanf internally)
142+
142143
int tzs = daylightOffset_sec + timezone_sec;
143144
int tzh = tzs / 3600;
144145
tzs -= tzh * 3600;
145146
int tzm = tzs / 60;
146147
tzs -= tzm * 60;
147148
148149
// man tzset:
150+
char tzstr [64];
149151
snprintf(tzstr, sizeof tzstr, "ESPUSER<%+d:%02d:%02d>", tzh, tzm, tzs);
150152
return configTime(tzstr, server1, server2, server3);
153+
154+
Replaced by light code found from
155+
newlib inspection and internal structure hacking
156+
(no sprintf, no sscanf, -7584 flash bytes):
157+
158+
***/
159+
160+
static char gmt[] = "GMT";
161+
162+
_timezone = timezone_sec + daylightOffset_sec;
163+
_daylight = 0;
164+
_tzname[0] = gmt;
165+
_tzname[1] = gmt;
166+
auto tz = __gettzinfo();
167+
tz->__tznorth = 1;
168+
tz->__tzyear = 0;
169+
for (int i = 0; i < 2; i++)
170+
{
171+
auto tzr = &tz->__tzrule[i];
172+
tzr->ch = 74;
173+
tzr->m = 0;
174+
tzr->n = 0;
175+
tzr->d = 0;
176+
tzr->s = 0;
177+
tzr->change = 0;
178+
tzr->offset = _timezone;
179+
}
180+
181+
// sntp servers
182+
setServer(0, server1);
183+
setServer(1, server2);
184+
setServer(2, server3);
185+
186+
/*** end of posix replacement ***/
151187
}
152188

153189
void configTime(const char* tz, const char* server1, const char* server2, const char* server3)

0 commit comments

Comments
 (0)