strptime() function in C fails to detect invalid dates. Ex: 2011-02-31 , 2011-04-31. Is there any other function or workaround to this problem
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
You can use
mktime
to normalize your structure after usingstrptime
.The example above will produce the output below:
If the strings before and after
mktime
do not match, then you know the input string was not a valid date.If you need to know which field was invalid, you can save a copy of the
ltm
structure before callingmktime
. Then, you can examine if the day (tm_mday
), month (tm_mon
), or year (tm_year
) was the one in the invalid format. Fortm_mon
,0
is January, and11
is December. Fortm_year
, it is the number of years since1900
. Remember to account for leap year when validating the day of the month for February.