Function to convert compiler macro __DATE__ and __TIME__ to a system time that can be used to initialize the CPU RTC. I use this to do a 1 time initialization of a RTC chip.

Dependents:   xj-Init-clock-to-compile-time-if-not-already-initialized-ds1302

Parse compiler DATE and TIME into a time_t structure

I use this to initialize the system time RTC using set_time() to a value parsed from the DATE and TIME strings. I find it easier to recompile and reload the firmware than it is to manually type the time string.

Sample Use

https://developer.mbed.org/users/joeata2wh/code/xj-Init-clock-to-compile-time-if-not-alr extends the use of this library to set the DS1302 clock chip to the compile time only the first time the utility runs. After that it detects a sentinel already stored in the clock chip and used the previously saved time.

Referenced:

compile_time_to_system_time.h

Committer:
joeata2wh
Date:
2016-05-07
Revision:
2:c746c1b8671f
Parent:
1:3e55cf9d76e6

File content as of revision 2:c746c1b8671f:

/*  Function to convert compile time reported as 
  MBED compiler macro into a System time we can 
  use to set time and date in our chip.  This can
  be a helpful way to initialize clock chips for
  low volume tests 
  
  By Joseph Ellsworth CTO of A2WH
  Take a look at A2WH.com Producing Water from Air using Solar Energy
  March-2016 License: https://developer.mbed.org/handbook/MIT-Licence 
  Please contact us http://a2wh.com for help with custom design projects.


 call with the command
   printf("compile time=%s date=%s\r\n",__TIME__,__DATE__);
   time_t build_time = cvt_date(__DATE__,__TIME__);
   printf("compile time reformate=%s r\n", ctime(&build_time));
   
   
 TODO: Enhance to read time from clock chip and if unreasable 
   then re-initialize the time.  I have found a small number
   of instances when the clock lost the time due to battery
   run down but the init byte was still present.  This enhancement
   should accomodate that. 
    
*/
  
#ifndef compile_time_to_system_time_H
#define compile_time_to_system_time_H

 
// call with the command
//time_t build_time = cvt_date(__DATE__,__TIME__);

// Convert compile time to system time 
time_t cvt_date(char const *date, char const *time)
{
    char s_month[5];
    int year;
    struct tm t;
    static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
    sscanf(date, "%s %d %d", s_month, &t.tm_mday, &year);
    sscanf(time, "%2d %*c %2d %*c %2d", &t.tm_hour, &t.tm_min, &t.tm_sec);
    // Find where is s_month in month_names. Deduce month value.
    t.tm_mon = (strstr(month_names, s_month) - month_names) / 3;    
    t.tm_year = year - 1900;    
    return mktime(&t);
}


#endif