Compare Dates - Times

Good Afternoon,
I would like to compare 2 date times for example:

date 1: 14-01-13 14:15:00
and
date 2: 23-01-13 18:15:25
and
now = Now() --> for example 15-01-13 16:00:35

and now do something like that:

if now is between date1 and date2 ... do something ...

if Vb.net it's easy but in c++ a little bit less ,

so I've did something with Alarm.triggerOnce() but I need to specify more time element, I thinks that with kind of structure will be more light ...

could you help me ?

thanks for help,
Andrea

I would like to compare 2 date times for example:

How are those dates/times actually stored? As strings? Or, in some other format?

hi paul
at the mome t the date time are store in a byte like this

byte day = 20
byte month = 1

also for the mnutes second year hours sorry but writing crom smaerthphone ...

thanks gnux

and now do something like that:

if now is between date1 and date2 ... do something ...

If now() is a function, what does it return? If now is an instance of a class, what class, and what methods does it have?

If only time and date could be represented with a single number. :grin:

If only time and date could be represented with a single number.

They can. That's how Unix does it. Look at time_t.

In the end, time is defined as elapsed time unit from a specified starting point in something like unsigned long data type. The "start of the universe" can be defined as something like Jan 1st, 1970, for example. The time can be taken from an RTC, or it's just clock cycles from last power up.

Anyways, if you can convert whatever representation you have to, say, seconds, it's as easy as doing a regular comparison and you can do subtraction and all, and return that difference back to days, hours, minutes and so on. It would be a whole lot more complex trying to figure out comparisons in the component form. It would be a lot easier if the time was not represented in its component form in data structures, but as a simple unsigned 32- or 64-bit number. 32 bit will probably do if one second accuracy is enough..

For example in my sketch I retrieve data and time from RTC ... for use Alarm.TriggerOnce(); is necessary pass to the function maketime(timer, functiontocall); for example:

int TimerID;
TimeElements: timer
timer.Hours = 1;
timer.Minute = 1;
timer.Second = 00;
timer.day = 20;
timer.month = 01;
timer.year = 2013 - 1970;
TimerID = Alarm.triggerOnce(makeTime(timer), FunctiontoCall);
"TimerID should be equal to 0"

and in this way I can call a function in a specific time.
I want run more timer int the same that i need to specify "n" variable as TimeElements (timer1,timer2,timer3) until 255 maximum ... That means that will be present n TimerID for n TimeElements ... Correct ? in this way i will use for more cpu for do that ... if don't understand bad ...

Then I was thinking to "Compare Date" because after that will more light because :

  • I will call only one "Alarm.timerRepeat(15, FunctionToCall);

then inside to FunctionToCall I'll do:

  • Read my byte Array
  • check if the timer is enable with a "Flag" (1 enable, 0 disable)
    . If timer 1 is enable then My Timer1 is Between Now + or - 10 ?
    if yes do something
    this for all timer that i need ...

in this way is not necessary specify n TimeElements ... What do you think ?

thanks
gnux

The time library has all the functions to convert to and from Unix time. I'd use that and the comparisons become trivial since you only have the one number for date and time. Flip it back if/when you need a human to be able to read it.

If your dates are stored as separate elements, an alternative is to format them as yyyymmddhhmmss you can compare the strings or numbers directly. Use 24 hour format for the time. Works with 2 digit dates as long as they are all after 1999. Use a sprintf to get this format using leading zeros (eg %04d for the year).

thanks could be interested,

could you provide kindly a basic code in order to understand the concept from the code stand point ?

thanks for the support,
gnux

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

s is my string ok, what is this "%04d%02d%02d%02d%02d%02d"? for example %04 Means take 4 digits from year ? and "%02d" take 2 digits ?

after that I will a date store like this:

20130206065244

But now this, is string of character ... i need to compare with another string ... and then should match exactly the time now ... so I need to do some "string compare" ?

and if for example in that time the cpu was doing something else ? So I want to be sure that the cpu at right time will do the operations ...

make sense for you ? Otherwise I can use Alarm.TriggerOnce() with variable,

thanks
gnux

what is this "%04d%02d%02d%02d%02d%02d"?

You could look up the reference for sprintf()? It means 4 decimal digits with leading zero.

so I need to do some "string compare"

Try the function strcmp(). You should probably look up a C reference guide for string handling functions and get familiar with all the functions that are available. These are standard C functions that have existed for at least 30 years :wink:

and if for example in that time the cpu was doing something else

Like what? The cpu only does one thing at a time, so unless you are making it do soimething else it probably will not be.

make sense for you ?

Not really.

If you are looking to match a date and time and are trying to get it to the last second, then this is probably not the right way to go about it. Think about the problem. How far ahead are you planning to schedule events - years or seconds? Then decide the time resolution that you need. For example if you are only looking to trigger on the time, then why bother looking at the year, month or date?

Hi Marco,
thanks I have already used strcmp() then I know if the result will be == 0 the string are exactly .

So then I not understood bad your words I can retrieve from my RTC. day,month,year,hours, minute do my string and then I can compare with the string that I've inside my array ... if are equal do something correct ?

you have reason when you said : "These are standard C functions that have existed for at least 30 years " but I've started to develop software after 10 year and I'm a little bit rusty ... for the reason I ask help ... but after i give back always a feedback about my tried . I think is a respect for who help you and the "forum" sense ...

Thanks
gnux

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.

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

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

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

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