Pages: 1 [2]   Go Down
Author Topic: Compare Dates - Times  (Read 1415 times)
0 Members and 1 Guest are viewing this topic.
Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1260
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

One other thing I forgot to mention is that you should be careful not to just check if they are equal but check if the current time is >= the set time, because being just one second out will void the equality condition.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Sr. Member
****
Karma: 2
Posts: 428
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, then for do that how I can do ?

What I've understand is that I'm not comparing to datatime type but the fact 2 array char....

So in vb it's simple to do, but i c++ I've to find the way for do that ...

so if you have an example of compare date time i think could be more clear for understand what there is behind i think.

thanks gnux
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1260
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Strcmp() returns different values depending on whether the strings are equal, greater than or less than.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Sr. Member
****
Karma: 2
Posts: 428
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yes but In my case i think that is correct if are equal ...

so, from you your point of view how do you'll implement it ?

Thanks
gnux
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1260
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If in your case equal is correct then implement it the way you think it should be.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Sr. Member
****
Karma: 2
Posts: 428
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ciao Marco,

Looking the documentation about the sprintf

http://linux.die.net/man/3/sprintf

I don't understand one thing: so into my array I've byte ...with the function below reported will build my string that after will be compared.
but if for example into hour there is "9" the function for example for the hour take 2 digits will take 9 or 09 ? because my string lentgh will be different if i not understood bad ... than i need to check also if the hour > 9 take hour if hour < 9 take 0+hour 09 ... is correct ?

char s[15];
sprintf(s, "%04d%02d%02d%02d%02d%02d", year, month, day, hour, minute, second);

thanks
gnux
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1260
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

%02d puts the leading zero in for you.  What you need to do is make sure the time is in 24 hour format so that 1 am is 01 and 1 pm is 13. The string will always be the same length.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Sr. Member
****
Karma: 2
Posts: 428
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yes for sure the time is 24 format ... I will try then I'll let you know ...

Thanks,
gnux
Logged

Offline Offline
Sr. Member
****
Karma: 2
Posts: 428
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ciao Marco,
so I've did this simple sketch could be a good starting point:

Code:
#include <Time.h> 

char s[15];
char s1[15];
byte y1,mon1,d1,h1,min1,sec1;

void setup(){
  Serial.begin(9600);
  setTime(9,25,00,6,2,13);
  y1 = 13;
  mon1 = 2;
  d1 = 6;
  h1 = 9;
  min1 = 25;
  sec1 = 01;
}

void loop(){
  time_t t = now();
  sprintf(s, "%04d%02d%02d%02d%02d%02d", year(t), month(t), day(t), hour(t), minute(t), second(t));
  sprintf(s1, "%04d%02d%02d%02d%02d%02d", (y1+2000), mon1, d1, h1, min1, sec1);
  // Debug
  Serial.println("Stampo S ");
  Serial.println(s);
  Serial.println("Stampo S1 ");
  Serial.println(s1);
 
 
  if (strcmp(s,s1) == 0)
    {
      Serial.println("Condition Verified");
    }
 
  delay(1000);
}

so now just to think about the structure how use it ?

do you think that could be fine ?

thanks
gnux
Logged

Saskatchewan
Offline Offline
Sr. Member
****
Karma: 19
Posts: 363
When the going gets weird, the weird turn pro. - Hunter S. Thompson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As you are using seconds your comparison will only be true for one second. If your sketch is doing other things it could miss. However to change to one minute resolution just drop seconds from your strings. Then they will be equal within one minute. Drop the minutes for one hour resolution.

If you really need one second resolution then Unix time might be a better solution.
Logged

Pages: 1 [2]   Go Up
Jump to: