Skip to content

Commit c09ce6a

Browse files
committed
Compute EPOCH_AT_COMPILE_TIME only once
1 parent bdca6b8 commit c09ce6a

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

src/utility/time/TimeService.cpp

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -398,32 +398,38 @@ unsigned long TimeServiceClass::getRTC()
398398

399399
time_t cvt_time(char const * time)
400400
{
401-
char s_month[5];
402-
int month, day, year;
403-
struct tm t =
404-
{
405-
0 /* tm_sec */,
406-
0 /* tm_min */,
407-
0 /* tm_hour */,
408-
0 /* tm_mday */,
409-
0 /* tm_mon */,
410-
0 /* tm_year */,
411-
0 /* tm_wday */,
412-
0 /* tm_yday */,
413-
0 /* tm_isdst */
414-
};
415-
static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
416-
417-
sscanf(time, "%s %d %d", s_month, &day, &year);
418-
419-
month = (strstr(month_names, s_month) - month_names) / 3;
420-
421-
t.tm_mon = month;
422-
t.tm_mday = day;
423-
t.tm_year = year - 1900;
424-
t.tm_isdst = -1;
401+
static time_t build_time = 0;
402+
403+
if (!build_time) {
404+
char s_month[5];
405+
int month, day, year;
406+
struct tm t =
407+
{
408+
0 /* tm_sec */,
409+
0 /* tm_min */,
410+
0 /* tm_hour */,
411+
0 /* tm_mday */,
412+
0 /* tm_mon */,
413+
0 /* tm_year */,
414+
0 /* tm_wday */,
415+
0 /* tm_yday */,
416+
0 /* tm_isdst */
417+
};
418+
static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
419+
420+
sscanf(time, "%s %d %d", s_month, &day, &year);
421+
422+
month = (strstr(month_names, s_month) - month_names) / 3;
423+
424+
t.tm_mon = month;
425+
t.tm_mday = day;
426+
t.tm_year = year - 1900;
427+
t.tm_isdst = -1;
428+
429+
build_time = mktime(&t);
430+
}
425431

426-
return mktime(&t);
432+
return build_time;
427433
}
428434

429435
#ifdef ARDUINO_ARCH_SAMD

0 commit comments

Comments
 (0)