View Bug Activity | Format For Printing
mktime should normalize time to the next month if mday is greater than the last day of the current month. CTIME(3) The mktime() function converts a broken-down time structure, expressed as local time, to calendar time representation. The function ignores the specified contents of the structure members tm_wday and tm_yday and recomputes them from the other information in the broken-down time structure. If structure members are outside their legal interval, they will be normalized (so that, e.g., 40 October is changed into 9 November). Calling mktime() also sets the external variable tzname with information about the current time zone. If the specified bro- ken-down time cannot be represented as calendar time (seconds since the epoch), mktime() returns a value of (time_t)(-1) and does not alter the tm_wday and tm_yday members of the broken-down time struc- ture. But mktime normalizes wrong, it just delete the last digits until mday is in legal range, example: $ cat mktime-test.c #include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) { char str_mday[3] = "00\0"; char date[11] = "00.00.0000\0"; int int_mday = 0; time_t t = time(NULL); struct tm *inctime = localtime(&t); strftime(str_mday, 3, "%e", inctime); int_mday = atoi(str_mday); strftime(date, 11, "%e.%m.%Y", inctime); printf("\ndate: %s\tmday= %d",date,int_mday); int_mday+=29; sprintf(str_mday, "%d",int_mday); strptime(str_mday, "%e", inctime); t = mktime(inctime); inctime = localtime(&t); strftime(date, 11, "%e.%m.%Y", inctime); printf("\ndate: %s\tmday= %d",date,int_mday); int_mday+=129; sprintf(str_mday, "%d",int_mday); strptime(str_mday, "%e", inctime); t = mktime(inctime); inctime = localtime(&t); strftime(date, 11, "%e.%m.%Y", inctime); printf("\ndate: %s\tmday= %d",date,int_mday); int_mday+=4129; sprintf(str_mday, "%d",int_mday); strptime(str_mday, "%e", inctime); t = mktime(inctime); inctime = localtime(&t); strftime(date, 11, "%e.%m.%Y", inctime); printf("\ndate: %s\tmday= %d\n\n",date,int_mday); return 0; } $ gcc -o mktime-test mktime-test.c && ./mktime-test date: 24.03.2006 mday= 24 date: 5.03.2006 mday= 53 date: 18.03.2006 mday= 182 date: 4.03.2006 mday= 4311
This has nothing to do with mktime. It is strptime which rejects your invalid data. This is the correct behavior.