Is there a way to get strptime()
to handle fixed format time strings?
I need to parse a time string that is always on the fixed width format: "yymmdd HHMMSS
",
but with the complication that leading zeros are sometimes present and sometimes not.
Reading up on the man(3p) page of strptime
I note that for all the conversion specifiers %y, %m, %d, %H, %M, %S
it is commented that "leading zeros shall be permitted but shall not be required.". Hence I try the format specifier %y%m%d %H%M%S
, naïvely hoping that strptime
will recognize that spaces in the two substrings %y%m%d
and %H%M%S
are equivalent to (missing) leading zeroes.
This seem to work for the specifier %m
, but not for %M
(well, unless the second part is less than 10) as demonstrated by the following piece of code
#include <stdio.h>
#include <time.h>
int main() {
struct tm buff;
const char ts[]="17 310 22 312";
char st[14];
strptime(ts,"%y%m%d %H%M%S", &buff);
strftime(st,14,"%y%m%d %H%M%S",&buff);
printf("%s\n",ts);
printf("%s\n",st);
return 0;
}
When compiled and run on my machine outputs
17 310 22 312
170310 223102
Any insight on how to overcome this would be appreciated, or do I need to resort to manually chopping the string 2-characters at the time using atoi
to convert to integers to populate my struct tm
instance with?