I’m trying to convert the character string “2011-12-12” into a time value using strptime. Can someone explain why this doesn’t work?
According to the docs:
The format is composed of zero or more directives. Each directive is composed of one of the following: one or more white-space characters (as specified by isspace()); an ordinary character (neither ‘%’ nor a white-space character); or a conversion specification.
if( strptime(date, "%y-%m-%d", &tm) != NULL)
Take a look at the documentation. The
%yspecifier is for two-digit years, without the century, whereas%Yis for the full year, including the century. When you use%ywith your input string, it parses the year as20, and then since the rest of the string (11-12-12) doesn’t match-%m-%d, parsing fails.Either change your format string to use
%Y(recommended), or change your input to use two-digit dates (strongly not recommended).