I'm doing the following to convert and check a date, however, I'm not sure why the following date keeps validating as true.
Wouldn't %d
check only for [01,31] + leading zeros
? Is there a better and more accurate way of doing this?
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main () {
struct tm tm;
char buffer [80];
char *str = "29/Jan/2012";
if (strptime (str, "%Y/%b/%d", &tm) == NULL)
exit(EXIT_FAILURE);
if (strftime (buffer,80,"%Y-%m-%d",&tm) == 0)
exit(EXIT_FAILURE);
printf("%s\n", buffer); // prints 29-01-20
return 0;
}