2017-02-17 30 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()日時はあなたの書式に従って変換に似ています。