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 ?
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).
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,
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
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 ...
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.