strptime的使用,判断日期是否合法

使用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


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值