Added a function to the app_utils collection which parses a time and date string to fill a struct tm
This commit is contained in:
parent
54e69e424a
commit
0549ebe68d
@ -1,5 +1,6 @@
|
||||
#include "app_utils.h"
|
||||
#include <math.h>
|
||||
#include "utils.h"
|
||||
#include "wm_crypto_hard.h"
|
||||
|
||||
static uint32_t millis_cnt = 0;
|
||||
@ -73,4 +74,37 @@ uint32_t random_gen_6_digit(void)
|
||||
}
|
||||
|
||||
return output_num;
|
||||
}
|
||||
}
|
||||
|
||||
void time_date_to_tm(const char *time_date, struct tm *time)
|
||||
{
|
||||
if(!time_date) return;
|
||||
|
||||
/* Months are on three letters :
|
||||
Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
|
||||
*/
|
||||
const char *MONTHS[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
|
||||
const unsigned long int MONTHS_CNT = sizeof(MONTHS)/sizeof(*MONTHS);
|
||||
char month[4];
|
||||
time->tm_mon = 0;
|
||||
|
||||
sscanf(time_date, "%d:%d:%d %s %d %d",
|
||||
&time->tm_hour,
|
||||
&time->tm_min,
|
||||
&time->tm_sec,
|
||||
month,
|
||||
&time->tm_mday,
|
||||
&time->tm_year);
|
||||
|
||||
for(unsigned long int i = 0; i < MONTHS_CNT; i++)
|
||||
{
|
||||
if(strcasecmp(MONTHS[i], month) == 0)
|
||||
{
|
||||
time->tm_mon = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Adjust year value
|
||||
time->tm_year -= 1900;
|
||||
}
|
||||
|
@ -13,4 +13,13 @@ void ms_delay(uint32_t ms);
|
||||
|
||||
uint32_t random_gen_6_digit(void);
|
||||
|
||||
/**
|
||||
* @brief Function parsing a string having format : __TIME__" "__DATE__
|
||||
* and filling the given struct tm accordingly.
|
||||
*
|
||||
* @param time_date a string having the proper time and date format
|
||||
* @param time a pointer to the struct tm to fill
|
||||
*/
|
||||
void time_date_to_tm(const char *time_date, struct tm *time);
|
||||
|
||||
#endif //APP_UTILS_H
|
Loading…
Reference in New Issue
Block a user