7 years ago.

Converting time to integer help

I am trying to write a program whereby the function will be executed whenever the hours (24hr format) is X from RTC time.

#include "mbed.h"

void function(){
...
}

int main(){
   set_time( ... );
   while(1){
      if(hour == X) {
         function();
      }
   }
}

Your help will be greatly appreciated.

1 Answer

7 years ago.

Hello Tan,

You may try for example the following:

void function(){
    ...
}
 
int main(){
    time_t  my_time;
    tm      my_tm;

    set_time( ... );

    while (1) {
        my_time = time(NULL);
        my_tm = *localtime(&my_time);
        if ((my_tm.tm_hour == X) && (my_tm.tm_sec == 0)) {
            function();
        }
        ...
    }
}


Accepted Answer

But a more efficient solution is to use Erik Olieman's RTC library which enables to utilize RTC interrupts.

posted by Zoltan Hudak 08 Nov 2017