使用strptime函数
char *pret = strptime(data,"%Y%m%d%H%M%S", &timeinfo);
ubuntu和AIX上,调用strptime函数,日期不合法直接返回NULL值,只需要根据pret != NULL就断定日期合法
</pre><pre code_snippet_id="629839" snippet_file_name="blog_20150328_6_1952433" name="code" class="cpp">SUSE10有些不太一样,在某些日期非法情况下,SUSE10的pret依然不等于NULL,所以还需要加一个条件(*pret != '\0')
</pre><pre code_snippet_id="629839" snippet_file_name="blog_20150328_8_2404853" name="code" class="cpp">总的来说,不管是SUSE,AIX,Ubuntu判断日期是否合法最好用
if ((pret != NULL) && (*pret) != '\0' )
</pre><pre code_snippet_id="629839" snippet_file_name="blog_20150328_11_6397751" name="code" class="cpp">系统自定义的结构tm,
struct tm {
int tm_sec; /* seconds */
int tm_min; /* minutes */
int tm_hour; /* hours */
int tm_mday; /* day of the month */
int tm_mon; /* month */
int tm_year; /* year */
int tm_wday; /* day of the week */
int tm_yday; /* day in the year */
int tm_isdst; /* daylight saving time */
};
int tm_sec; [0 , 60] 等于60,操作系统认为是合法的
int tm_min; [0 , 59]
int tm_hour; [0 , 23]
int tm_mday; [1 , 31] 唯一一个从1开始计数的成员
int tm_mon; [0 , 11]
int tm_year; [1900 , +oo) 从1900年开始技术,而不是1970年
int tm_wday; [0 , 6] 0对应的星期天而不是星期一
下面是ubuntu上简单测试
有个编译告警:strptime.c:13:7: warning: assignment makes pointer from integer without a cast [enabled by default]
给了一个非法日期,运行结果是:Date format is invalid
同样这段代码在SUSE10上测试结果是:Date format is OK
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char data[] = "20150332015600";
struct tm timeinfo;
char *pret = NULL;
pret = strptime(data,"%Y%m%d%H%M%S", &timeinfo);
if( pret == NULL)
{
printf("Date format is invalid\n");
}
else
{
printf("Date format is OK\n");
}
return 0;
}
AIX的编译器是xlR_c
AIX查看CPU型号的命令是
uname -Mu