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:
- http://stackoverflow.com/questions/1765014/convert-string-from-date-into-a-time-t
- https://developer.mbed.org/users/petereiso/notebook/cc-time-functions/
- http://playground.arduino.cc/Main/DS1302
compile_time_to_system_time.h@2:c746c1b8671f, 2016-05-07 (annotated)
- Committer:
- joeata2wh
- Date:
- Sat May 07 02:06:11 2016 +0000
- Revision:
- 2:c746c1b8671f
- Parent:
- 1:3e55cf9d76e6
month was 1 digit to high
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
joeata2wh | 0:5f3730f44e19 | 1 | /* Function to convert compile time reported as |
joeata2wh | 0:5f3730f44e19 | 2 | MBED compiler macro into a System time we can |
joeata2wh | 0:5f3730f44e19 | 3 | use to set time and date in our chip. This can |
joeata2wh | 0:5f3730f44e19 | 4 | be a helpful way to initialize clock chips for |
joeata2wh | 0:5f3730f44e19 | 5 | low volume tests |
joeata2wh | 1:3e55cf9d76e6 | 6 | |
joeata2wh | 1:3e55cf9d76e6 | 7 | By Joseph Ellsworth CTO of A2WH |
joeata2wh | 1:3e55cf9d76e6 | 8 | Take a look at A2WH.com Producing Water from Air using Solar Energy |
joeata2wh | 1:3e55cf9d76e6 | 9 | March-2016 License: https://developer.mbed.org/handbook/MIT-Licence |
joeata2wh | 1:3e55cf9d76e6 | 10 | Please contact us http://a2wh.com for help with custom design projects. |
joeata2wh | 1:3e55cf9d76e6 | 11 | |
joeata2wh | 0:5f3730f44e19 | 12 | |
joeata2wh | 0:5f3730f44e19 | 13 | call with the command |
joeata2wh | 0:5f3730f44e19 | 14 | printf("compile time=%s date=%s\r\n",__TIME__,__DATE__); |
joeata2wh | 0:5f3730f44e19 | 15 | time_t build_time = cvt_date(__DATE__,__TIME__); |
joeata2wh | 0:5f3730f44e19 | 16 | printf("compile time reformate=%s r\n", ctime(&build_time)); |
joeata2wh | 0:5f3730f44e19 | 17 | |
joeata2wh | 1:3e55cf9d76e6 | 18 | |
joeata2wh | 1:3e55cf9d76e6 | 19 | TODO: Enhance to read time from clock chip and if unreasable |
joeata2wh | 1:3e55cf9d76e6 | 20 | then re-initialize the time. I have found a small number |
joeata2wh | 1:3e55cf9d76e6 | 21 | of instances when the clock lost the time due to battery |
joeata2wh | 1:3e55cf9d76e6 | 22 | run down but the init byte was still present. This enhancement |
joeata2wh | 1:3e55cf9d76e6 | 23 | should accomodate that. |
joeata2wh | 0:5f3730f44e19 | 24 | |
joeata2wh | 0:5f3730f44e19 | 25 | */ |
joeata2wh | 0:5f3730f44e19 | 26 | |
joeata2wh | 0:5f3730f44e19 | 27 | #ifndef compile_time_to_system_time_H |
joeata2wh | 0:5f3730f44e19 | 28 | #define compile_time_to_system_time_H |
joeata2wh | 0:5f3730f44e19 | 29 | |
joeata2wh | 0:5f3730f44e19 | 30 | |
joeata2wh | 0:5f3730f44e19 | 31 | // call with the command |
joeata2wh | 0:5f3730f44e19 | 32 | //time_t build_time = cvt_date(__DATE__,__TIME__); |
joeata2wh | 0:5f3730f44e19 | 33 | |
joeata2wh | 0:5f3730f44e19 | 34 | // Convert compile time to system time |
joeata2wh | 0:5f3730f44e19 | 35 | time_t cvt_date(char const *date, char const *time) |
joeata2wh | 0:5f3730f44e19 | 36 | { |
joeata2wh | 0:5f3730f44e19 | 37 | char s_month[5]; |
joeata2wh | 0:5f3730f44e19 | 38 | int year; |
joeata2wh | 0:5f3730f44e19 | 39 | struct tm t; |
joeata2wh | 0:5f3730f44e19 | 40 | static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec"; |
joeata2wh | 0:5f3730f44e19 | 41 | sscanf(date, "%s %d %d", s_month, &t.tm_mday, &year); |
joeata2wh | 0:5f3730f44e19 | 42 | sscanf(time, "%2d %*c %2d %*c %2d", &t.tm_hour, &t.tm_min, &t.tm_sec); |
joeata2wh | 0:5f3730f44e19 | 43 | // Find where is s_month in month_names. Deduce month value. |
joeata2wh | 2:c746c1b8671f | 44 | t.tm_mon = (strstr(month_names, s_month) - month_names) / 3; |
joeata2wh | 0:5f3730f44e19 | 45 | t.tm_year = year - 1900; |
joeata2wh | 0:5f3730f44e19 | 46 | return mktime(&t); |
joeata2wh | 0:5f3730f44e19 | 47 | } |
joeata2wh | 0:5f3730f44e19 | 48 | |
joeata2wh | 0:5f3730f44e19 | 49 | |
joeata2wh | 0:5f3730f44e19 | 50 | #endif |