Skip to content

Commit

Permalink
Don't compile strptime unless its required (esphome#6424)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabest11 authored Mar 25, 2024
1 parent f5ac1bd commit 7cb8f99
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
6 changes: 6 additions & 0 deletions esphome/core/time.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#ifdef USE_DATETIME
#include <regex>
#endif

#include "helpers.h"
#include "time.h" // NOLINT
Expand Down Expand Up @@ -64,6 +66,8 @@ std::string ESPTime::strftime(const std::string &format) {
return timestr;
}

#ifdef USE_DATETIME

bool ESPTime::strptime(const std::string &time_to_parse, ESPTime &esp_time) {
// clang-format off
std::regex dt_regex(R"(^
Expand Down Expand Up @@ -102,6 +106,8 @@ bool ESPTime::strptime(const std::string &time_to_parse, ESPTime &esp_time) {
return true;
}

#endif

void ESPTime::increment_second() {
this->timestamp++;
if (!increment_time_value(this->second, 0, 60))
Expand Down
4 changes: 4 additions & 0 deletions esphome/core/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,17 @@ struct ESPTime {
this->day_of_year < 367 && this->month > 0 && this->month < 13;
}

#ifdef USE_DATETIME

/** Convert a string to ESPTime struct as specified by the format argument.
* @param time_to_parse null-terminated c string formatet like this: 2020-08-25 05:30:00.
* @param esp_time an instance of a ESPTime struct
* @return the success sate of the parsing
*/
static bool strptime(const std::string &time_to_parse, ESPTime &esp_time);

#endif

/// Convert a C tm struct instance with a C unix epoch timestamp to an ESPTime instance.
static ESPTime from_c_tm(struct tm *c_tm, time_t c_time);

Expand Down

0 comments on commit 7cb8f99

Please sign in to comment.