This is an uninitialized memory issue.
(gdb) p tm $1 = {tm_sec = 1, tm_min = 0, tm_hour = 4196061, tm_mday = 0, tm_mon = -5984, tm_year = 32767, tm_wday = 0, tm_yday = 0, tm_isdst = 4195984, tm_gmtoff = 4195616, tm_zone = 0x7fffffffe980 "\001"}
As you can see in the debugger, struct tm has arbitrary memory. Create garbage offset time_zone.
After strptime is executed:
(gdb) p tm $3 = {tm_sec = 1, tm_min = 0, tm_hour = 4196061, tm_mday = 11, tm_mon = 3, tm_year = 98, tm_wday = 6, tm_yday = 100, tm_isdst = 4195984, tm_gmtoff = 4195616, tm_zone = 0x7fffffffe980 "\001"}
Besides:
tm.tm_mon = tm.tm_mon -1;
Not necessary. Corrected Code:
#include <time.h> #include <stdio.h> #include <string.h> int main(int argc, char **argv) { struct tm tm; time_t ts = 0; memset(&tm, 0, sizeof(tm)); strptime("1998-04-11", "%Y-%m-%d", &tm); ts = mktime(&tm); printf("%d \n", (int)ts); //unix time-stamp printf("%s \n", ctime(&ts)); //human readable date }
Output:
892252800 Sat Apr 11 00:00:00 1998