C++ mktime returning random dates

前端 未结 3 902
走了就别回头了
走了就别回头了 2020-12-21 05:55

I\'m trying to convert a date string to a time_t, but mktime() is returning seemingly random dates:

string datetime = \"2014-12-10 10:30\";
stru         


        
3条回答
  •  攒了一身酷
    2020-12-21 06:45

    You need to properly zero-initialize all of the other fields of the struct tm instance before calling strptime(), since it doesn't necessarily initialize every field. From the strptime() POSIX specification:

    It is unspecified whether multiple calls to strptime() using the same tm structure will update the current contents of the structure or overwrite all contents of the structure. Conforming applications should make a single call to strptime() with a format and all data needed to completely specify the date and time being converted.

    For example, this should suffice:

    struct tm tmInfo = {0};
    

提交回复
热议问题