The problem is very simple, I have a date column in my database postgres, it returns the value as string in the format yyyy-mm-dd, in order to store it in my code, I need to parse the value. The function is also very simple:
int parse_date(const char * s, tbDate*res)
{
int yyyy, mm, dd;
if (sscanf(s, "%d %d %d", &yyyy, &mm, &dd) == 3
|| sscanf(s, "%d.%d.%d", &yyyy, &mm, &dd) == 3
|| sscanf(s, "%d/%d/%d", &yyyy, &mm, &dd) == 3
|| sscanf(s, "%d-%d-%d", &yyyy, &mm, &dd) == 3)
{
if (mm > 0 && mm < 13 && dd>0 && dd < 32)
{
res->day = dd;
res->month = mm;
res->year = yyyy;
return TB_PARSE_SUCCESS;
}
}
return TB_PARSE_FAILURE;
}
The first three versions of sscanf() work very well, but the follow sscanf(s, "%d-%d-%d", &yyyy, &mm, &dd) , the format that the database returns, are read as negative value, it means that if I pass a date like '2022-12-03', yyyy is correctly 2022, but mm and dd, are -12 and -3. I would avoid to put a date format function in the query of the database, and I would know if there is a way to escape the - charachter inside the sscanf().