Files
basic1/fatfs_time.c
2026-01-28 20:12:41 -05:00

49 lines
1.9 KiB
C

/*-----------------------------------------------------------------------*/
/* Get current time for FatFS */
/*-----------------------------------------------------------------------*/
#include "ff.h"
#include "pico/stdlib.h"
#include <string.h>
// Parse compilation date/time macros
static int get_month_from_string(const char* month_str) {
const char* months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
for (int i = 0; i < 12; i++) {
if (strncmp(month_str, months[i], 3) == 0) {
return i + 1;
}
}
return 1; // Default to January
}
/* Get current time */
DWORD get_fattime (void)
{
// Use compilation date/time from __DATE__ and __TIME__ macros
// __DATE__ format: "Jan 28 2026"
// __TIME__ format: "HH:MM:SS"
// Format: bit31:25=Year(0-127 +1980), bit24:21=Month(1-12), bit20:16=Day(1-31)
// bit15:11=Hour(0-23), bit10:5=Minute(0-59), bit4:0=Second/2(0-29)
// Parse __DATE__ (e.g., "Jan 28 2026")
char month_str[4] = {__DATE__[0], __DATE__[1], __DATE__[2], 0};
int day = (__DATE__[4] == ' ' ? 0 : (__DATE__[4] - '0') * 10) + (__DATE__[5] - '0');
int year = ((__DATE__[7] - '0') * 1000) + ((__DATE__[8] - '0') * 100) +
((__DATE__[9] - '0') * 10) + (__DATE__[10] - '0');
int month = get_month_from_string(month_str);
// Parse __TIME__ (e.g., "12:34:56")
int hour = ((__TIME__[0] - '0') * 10) + (__TIME__[1] - '0');
int minute = ((__TIME__[3] - '0') * 10) + (__TIME__[4] - '0');
int second = ((__TIME__[6] - '0') * 10) + (__TIME__[7] - '0');
return ((DWORD)(year - 1980) << 25)
| ((DWORD)month << 21)
| ((DWORD)day << 16)
| ((DWORD)hour << 11)
| ((DWORD)minute << 5)
| ((DWORD)(second / 2));
}