2017-02-17 2 views
0

들어오는 문자열을 DS1307 RTC 모듈의 날짜와 시간과 비교해야합니다. 문자열에서 특정 시간에 도달하면 이벤트를 트리거 할 것입니다.DateTime, 문자열 비교

정수로 변환을 시도했지만 작동하지 않습니다.

String now_int = rtc.now(); 

오류가 conversion from DateTime to non-scalar type String is requested

가 어떻게 문자열로 날짜를 비교할 수 있다고?

+0

'now()'는 문자열이나 int가 아닌'DateTime' 객체를 반환합니다. –

+1

가능한 [C++에서 문자열을 datetime으로 변환하는 방법] (http://stackoverflow.com/questions/4781852/how-to-convert-a-string-to-datetime-in-c) – Shawn

+0

게시 할 수 있습니다 당신의 완전한 예, 특히 당신이 수행하려고하는 비교? – BNT

답변

0

설명한 동작을 수행하려면 sprintfstrcmp의 조합을 사용할 수 있습니다. this

// 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() 날짜 시간을 변환합니다.