Pages: [1] 2   Go Down
Author Topic: Compare Dates - Times  (Read 1057 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Sr. Member
****
Karma: 2
Posts: 408
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

 
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46204
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I would like to compare 2 date times for example:
How are those dates/times actually stored? As strings? Or, in some other format?
Logged

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

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46204
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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?
Logged

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

If only time and date could be represented with a single number.  smiley-mr-green
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46204
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If only time and date could be represented with a single number.
They can. That's how Unix does it. Look at time_t.
Logged

Finland
Offline Offline
Jr. Member
**
Karma: 1
Posts: 84
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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..
Logged

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

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
     




 

Logged

Offline Offline
God Member
*****
Karma: 10
Posts: 651
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

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).
Logged

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

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

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
Logged

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

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

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

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

Code:
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
 
Logged

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

Quote
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.

Quote
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 smiley-wink

Quote
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.

Quote
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?
Logged

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

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

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
Logged

Pages: [1] 2   Go Up
Jump to: