From 0549ebe68dbc6a35a8b1b30e806702752c169050 Mon Sep 17 00:00:00 2001 From: Anatole SCHRAMM Date: Thu, 31 Jul 2025 16:15:31 +0200 Subject: [PATCH] Added a function to the app_utils collection which parses a time and date string to fill a struct tm --- app/app_lib/app_utils.c | 36 +++++++++++++++++++++++++++++++++++- app/app_lib/app_utils.h | 9 +++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/app/app_lib/app_utils.c b/app/app_lib/app_utils.c index 308a531..e91eb2e 100644 --- a/app/app_lib/app_utils.c +++ b/app/app_lib/app_utils.c @@ -1,5 +1,6 @@ #include "app_utils.h" #include +#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; -} \ No newline at end of file +} + +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; +} diff --git a/app/app_lib/app_utils.h b/app/app_lib/app_utils.h index f339a3c..2829936 100644 --- a/app/app_lib/app_utils.h +++ b/app/app_lib/app_utils.h @@ -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 \ No newline at end of file