Skip to content

[Bug] RTC convert_timestamp offset issue for month #5

@adaminato42

Description

@adaminato42

Describe the Bug

The string offset for checking the month in convert_timestamp is looking at the 2nd character of the month, instead of the 3rd, for both A and M:

qmk_modules/rtc/rtc.c

Lines 160 to 174 in 218388e

rtc_time_t convert_timestamp(const char *timestamp) {
rtc_time_t t;
// Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
switch (timestamp[4]) {
case 'J':
t.month = (timestamp[5] == 'a') ? 1 : ((timestamp[6] == 'n') ? 6 : 7);
break;
case 'F':
t.month = 2;
break;
case 'A':
t.month = timestamp[5] == 'r' ? 4 : 8;
break;
case 'M':
t.month = timestamp[5] == 'r' ? 3 : 5;

This results in incorrect values being returned for Mar or Apr

Can the offset be changed to address this? Alternatively, perhaps helper to parse the month?

// Helper function to parse month abbreviation (e.g. "Jan", "Feb", ...)
static uint8_t parse_month(const char *str) {
    static const char *months[] = {
        "Jan", "Feb", "Mar", "Apr", "May", "Jun",
        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    };
    for (uint8_t i = 0; i < 12; ++i) {
        if (strncmp(str, months[i], 3) == 0) {
            return i + 1;
        }
    }
    return 0; // Invalid month
}

// Replace the switch block with:
t.month = parse_month(&timestamp[4]);

Additional Context?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions