mirror of
https://github.com/esphome/esphome.git
synced 2025-02-18 01:03:12 +01:00
Replace std::regex with sscanf calls (#6468)
* Replace std::regex with sscanf calls * Fix CI * Use regular formatting placeholders * Fix
This commit is contained in:
parent
f09bfa7311
commit
0148ebcaa6
2 changed files with 37 additions and 44 deletions
|
@ -1,9 +1,7 @@
|
||||||
#ifdef USE_DATETIME
|
|
||||||
#include <regex>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "helpers.h"
|
|
||||||
#include "time.h" // NOLINT
|
#include "time.h" // NOLINT
|
||||||
|
#include "helpers.h"
|
||||||
|
|
||||||
|
#include <cinttypes>
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
|
|
||||||
|
@ -66,48 +64,47 @@ std::string ESPTime::strftime(const std::string &format) {
|
||||||
return timestr;
|
return timestr;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef USE_DATETIME
|
|
||||||
|
|
||||||
bool ESPTime::strptime(const std::string &time_to_parse, ESPTime &esp_time) {
|
bool ESPTime::strptime(const std::string &time_to_parse, ESPTime &esp_time) {
|
||||||
// clang-format off
|
uint16_t year;
|
||||||
std::regex dt_regex(R"(^
|
uint8_t month;
|
||||||
(
|
uint8_t day;
|
||||||
(\d{4})-(\d{1,2})-(\d{1,2})
|
uint8_t hour;
|
||||||
(?:\s(?=.+))
|
uint8_t minute;
|
||||||
)?
|
uint8_t second;
|
||||||
(
|
int num;
|
||||||
(\d{1,2}):(\d{2})
|
|
||||||
(?::(\d{2}))?
|
|
||||||
)?
|
|
||||||
$)");
|
|
||||||
// clang-format on
|
|
||||||
|
|
||||||
std::smatch match;
|
if (sscanf(time_to_parse.c_str(), "%04hu-%02hhu-%02hhu %02hhu:%02hhu:%02hhu %n", &year, &month, &day, // NOLINT
|
||||||
if (std::regex_match(time_to_parse, match, dt_regex) == 0)
|
&hour, // NOLINT
|
||||||
|
&minute, // NOLINT
|
||||||
|
&second, &num) == 6 && // NOLINT
|
||||||
|
num == time_to_parse.size()) {
|
||||||
|
esp_time.year = year;
|
||||||
|
esp_time.month = month;
|
||||||
|
esp_time.day_of_month = day;
|
||||||
|
esp_time.hour = hour;
|
||||||
|
esp_time.minute = minute;
|
||||||
|
esp_time.second = second;
|
||||||
|
} else if (sscanf(time_to_parse.c_str(), "%02hhu:%02hhu:%02hhu %n", &hour, &minute, &second, &num) == 3 && // NOLINT
|
||||||
|
num == time_to_parse.size()) {
|
||||||
|
esp_time.hour = hour;
|
||||||
|
esp_time.minute = minute;
|
||||||
|
esp_time.second = second;
|
||||||
|
} else if (sscanf(time_to_parse.c_str(), "%02hhu:%02hhu %n", &hour, &minute, &num) == 2 && // NOLINT
|
||||||
|
num == time_to_parse.size()) {
|
||||||
|
esp_time.hour = hour;
|
||||||
|
esp_time.minute = minute;
|
||||||
|
esp_time.second = 0;
|
||||||
|
} else if (sscanf(time_to_parse.c_str(), "%04hu-%02hhu-%02hhu %n", &year, &month, &day, &num) == 3 && // NOLINT
|
||||||
|
num == time_to_parse.size()) {
|
||||||
|
esp_time.year = year;
|
||||||
|
esp_time.month = month;
|
||||||
|
esp_time.day_of_month = day;
|
||||||
|
} else {
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (match[1].matched) { // Has date parts
|
|
||||||
|
|
||||||
esp_time.year = parse_number<uint16_t>(match[2].str()).value_or(0);
|
|
||||||
esp_time.month = parse_number<uint8_t>(match[3].str()).value_or(0);
|
|
||||||
esp_time.day_of_month = parse_number<uint8_t>(match[4].str()).value_or(0);
|
|
||||||
}
|
}
|
||||||
if (match[5].matched) { // Has time parts
|
|
||||||
|
|
||||||
esp_time.hour = parse_number<uint8_t>(match[6].str()).value_or(0);
|
|
||||||
esp_time.minute = parse_number<uint8_t>(match[7].str()).value_or(0);
|
|
||||||
if (match[8].matched) {
|
|
||||||
esp_time.second = parse_number<uint8_t>(match[8].str()).value_or(0);
|
|
||||||
} else {
|
|
||||||
esp_time.second = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void ESPTime::increment_second() {
|
void ESPTime::increment_second() {
|
||||||
this->timestamp++;
|
this->timestamp++;
|
||||||
if (!increment_time_value(this->second, 0, 60))
|
if (!increment_time_value(this->second, 0, 60))
|
||||||
|
|
|
@ -67,8 +67,6 @@ struct ESPTime {
|
||||||
this->day_of_year < 367 && this->month > 0 && this->month < 13;
|
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.
|
/** 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 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
|
* @param esp_time an instance of a ESPTime struct
|
||||||
|
@ -76,8 +74,6 @@ struct ESPTime {
|
||||||
*/
|
*/
|
||||||
static bool strptime(const std::string &time_to_parse, ESPTime &esp_time);
|
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.
|
/// 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);
|
static ESPTime from_c_tm(struct tm *c_tm, time_t c_time);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue