mirror of
https://github.com/esphome/esphome.git
synced 2025-11-17 23:35:47 +00:00
[core] Simplify ESPTime::strftime() and save 20 bytes flash
This commit is contained in:
@@ -46,23 +46,17 @@ struct tm ESPTime::to_c_tm() {
|
|||||||
return c_tm;
|
return c_tm;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string ESPTime::strftime(const std::string &format) {
|
std::string ESPTime::strftime(const char *format, size_t format_len) {
|
||||||
std::string timestr;
|
|
||||||
timestr.resize(format.size() * 4);
|
|
||||||
struct tm c_tm = this->to_c_tm();
|
struct tm c_tm = this->to_c_tm();
|
||||||
size_t len = ::strftime(×tr[0], timestr.size(), format.c_str(), &c_tm);
|
char buf[128];
|
||||||
while (len == 0) {
|
size_t len = ::strftime(buf, sizeof(buf), format, &c_tm);
|
||||||
if (timestr.size() >= 128) {
|
if (len > 0) {
|
||||||
// strftime has failed for reasons unrelated to the size of the buffer
|
return std::string(buf, len);
|
||||||
// so return a formatting error
|
}
|
||||||
return "ERROR";
|
return "ERROR";
|
||||||
}
|
}
|
||||||
timestr.resize(timestr.size() * 2);
|
|
||||||
len = ::strftime(×tr[0], timestr.size(), format.c_str(), &c_tm);
|
std::string ESPTime::strftime(const std::string &format) { return this->strftime(format.c_str(), format.size()); }
|
||||||
}
|
|
||||||
timestr.resize(len);
|
|
||||||
return timestr;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ESPTime::strptime(const std::string &time_to_parse, ESPTime &esp_time) {
|
bool ESPTime::strptime(const std::string &time_to_parse, ESPTime &esp_time) {
|
||||||
uint16_t year;
|
uint16_t year;
|
||||||
|
|||||||
@@ -55,6 +55,23 @@ struct ESPTime {
|
|||||||
*/
|
*/
|
||||||
std::string strftime(const std::string &format);
|
std::string strftime(const std::string &format);
|
||||||
|
|
||||||
|
/** Convert this ESPTime struct to a string as specified by the format argument.
|
||||||
|
* @see https://www.gnu.org/software/libc/manual/html_node/Formatting-Calendar-Time.html#index-strftime
|
||||||
|
*
|
||||||
|
* This overload is optimized for string literals and avoids std::string parameter overhead.
|
||||||
|
*
|
||||||
|
* @param format The format string (null-terminated C string)
|
||||||
|
* @param format_len Optional length of the format string. If 0 (default), strlen() will be called.
|
||||||
|
*
|
||||||
|
* @warning This method uses dynamically allocated strings which can cause heap fragmentation with some
|
||||||
|
* microcontrollers.
|
||||||
|
*
|
||||||
|
* @warning This method can return "ERROR" when the underlying strftime() call fails, e.g. when the
|
||||||
|
* format string contains unsupported specifiers or when the format string doesn't produce any
|
||||||
|
* output.
|
||||||
|
*/
|
||||||
|
std::string strftime(const char *format, size_t format_len = 0);
|
||||||
|
|
||||||
/// Check if this ESPTime is valid (all fields in range and year is greater than 2018)
|
/// Check if this ESPTime is valid (all fields in range and year is greater than 2018)
|
||||||
bool is_valid() const { return this->year >= 2019 && this->fields_in_range(); }
|
bool is_valid() const { return this->year >= 2019 && this->fields_in_range(); }
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user