strptime的时间格式问题
如下代码我是求两个时间的秒数,如果定义char fmt[] = "%Y-%m-%d-%H:%M:%S";,最后的结果是正确的,但是如果定义char fmt[] = "%Y-%m-%d %H:%M:%S";结果就是错误的。但是我想要%Y-%m-%d %H:%M:%S这种格式,这是为什么?
#include <stdio.h>
#include <time.h>
main(int argc, char *argv[])
{
char fmt[] = "%Y-%m-%d %H:%M:%S";
time_t ggtime,curtime;
struct tm *tb = (struct tm*)malloc(sizeof(struct tm));
strptime(argv[1], fmt, tb);
ggtime = mktime(tb);
curtime = time(NULL);
printf("The pause used %f seconds.\n",difftime(curtime,ggtime));
}
结果:
<LIS>:/orahome>./hello 2013-12-20-7:20:00
The pause used 1267.000000 seconds.
<LIS>:/orahome>./hello 2013-12-20 07:20:00
The pause used 27859.000000 seconds.
[解决办法]
参数带空格要加引号吧...