Question

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)
Was it helpful?

Solution

Take a look at the documentation. The %y specifier is for two-digit years, without the century, whereas %Y is for the full year, including the century. When you use %y with your input string, it parses the year as 20, 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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top