DateTime,字符串比较
DateTime, String comparison
我需要将传入字符串与DS1307 RTC模块的日期和时间进行比较。如果达到字符串的某个时间,我将触发事件。
我尝试使用转换为整数,但它不起作用。
String now_int = rtc.now();
错误说 conversion from DateTime to non-scalar type String is requested
如何将DateTime与字符串进行比较?
您可以使用sprintf和strcmp的组合来实现您所描述的行为,例如类似于此
// date and time from RTC
DateTime now = rtc.now();
// date and time to compare with - this is provided by you
String datetimeCompare = "1970/01/01 00:00:00";
// this buffer must be big enough for your complete datetime (depending on the format)
char datetimeBuffer[20] = "";
// convert current date and time to your specific format
sprintf(datetimeBuffer, "%04d/%02d/%02d %02d:%02d:%02d", now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());
// perform the comparison
if(strcmp(datetimeBuffer, datetimeCompare.c_str()) == 0)
{
// datetime strings are the same
}
或您按照Arduino stackexchange所述的格式转换rtc.now()dateTime。
相关文章:
- 视觉C++使用 map 来比较字符串中的每个单词
- 我正在尝试从输入文件中读取,然后使用它们的子字符串比较字符串的特定部分
- 比较字符串的 GetLine 工作一次,然后比较之后不起作用
- C++使用关系运算符比较字符串
- 通过默认复制构造函数比较 C++ 字符串是否会影响性能,原因为何?
- C++ TCP 服务器比较字符串失败
- 如何在不复制的情况下比较字符串的一部分?
- 比较 C++ 字符串打印中的两个整数数组
- 如何比较字符串形式的临时变量
- std::out_of_range 比较字符串中的符号时出错
- 比较字符串大小写不区分的简单方法是什么?
- 比较字符串忽略 NUL
- 比较字符串(arduino)时出现问题
- 如何在C++中比较字符串
- 比较字符串对象等于向量中的元素
- C 比较字符串操作速度
- 需要比较字符串的某些元素
- 比较字符串 .c_str() 和普通字符串的差异
- C 比较字符串的字符
- 通过使用两个不同的队列比较字符串