mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 08:19:52 +03:00
added string2minutes()
This commit is contained in:
@@ -783,4 +783,55 @@ const char * Helpers::translated_word(const char * const * strings, const bool f
|
|||||||
return strings[index];
|
return strings[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint16_t Helpers::string2minutes(const std::string & str) {
|
||||||
|
uint8_t i = 0;
|
||||||
|
uint16_t res = 0;
|
||||||
|
uint16_t tmp = 0;
|
||||||
|
uint8_t state = 0;
|
||||||
|
|
||||||
|
while (str[i] != '\0') {
|
||||||
|
// If we got a digit
|
||||||
|
if (str[i] >= '0' && str[i] <= '9') {
|
||||||
|
tmp = tmp * 10 + str[i] - '0';
|
||||||
|
}
|
||||||
|
// Or if we got a colon
|
||||||
|
else if (str[i] == ':') {
|
||||||
|
// If we were reading the hours
|
||||||
|
if (state == 0) {
|
||||||
|
res = 60 * tmp;
|
||||||
|
}
|
||||||
|
// Or if we were reading the minutes
|
||||||
|
else if (state == 1) {
|
||||||
|
if (tmp > 60) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
Serial.print("*");
|
||||||
|
Serial.print(tmp);
|
||||||
|
Serial.println("*");
|
||||||
|
|
||||||
|
res += tmp;
|
||||||
|
}
|
||||||
|
// Or we got an extra colon
|
||||||
|
else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
state++;
|
||||||
|
tmp = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Or we got something wrong
|
||||||
|
else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state == 1 && tmp < 60) {
|
||||||
|
return res + tmp;
|
||||||
|
} else {
|
||||||
|
return 0; // Or if we were not, something is wrong in the given string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace emsesp
|
} // namespace emsesp
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ class Helpers {
|
|||||||
static int atoint(const char * value);
|
static int atoint(const char * value);
|
||||||
static bool check_abs(const int32_t i);
|
static bool check_abs(const int32_t i);
|
||||||
static uint32_t abs(const int32_t i);
|
static uint32_t abs(const int32_t i);
|
||||||
|
static uint16_t string2minutes(const std::string & str);
|
||||||
|
|
||||||
static float transformNumFloat(float value, const int8_t numeric_operator, const uint8_t fahrenheit = 0);
|
static float transformNumFloat(float value, const int8_t numeric_operator, const uint8_t fahrenheit = 0);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user