日期:2014-05-16  浏览次数:20840 次

mac os下编译linux项目程序时发现的clang兼容性问题

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, const char *argv[])
{
    char szDateNow[20];
    struct tm curtime;
    time_t t = time( NULL );  //get systime
    localtime_r( &t, &curtime);  //转换时间格式

    strftime( szDateNow, sizeof(szDateNow), "%Y-%m", &curtime);
    printf("%%Y-%%m is %s.\n", szDateNow);
    strftime( szDateNow, sizeof(szDateNow), "%04Y-%02m", &curtime);
    printf("%%04Y-%%02m is %s.\n", szDateNow);
    exit(0); 
}

如上所示代码,在linux下使用gcc编译,输出结果为:
%Y-%m is 2014-01.
%04Y-%02m is 2014-01.
在mac os 10.9.1下使用clang编译,输出结果为:
%Y-%m is 2014-01.
%04Y-%02m is 4Y-2m.

请问大家一个问题,如果我要输出02Y-02m即两位年两位月改怎么做,输出四位自己截取这个不要提了
------解决方案--------------------
这个字段宽度的设置是一个GNU扩展,不过我看了苹果的strftime的代码,似乎已经加上了这个GNU扩展的支持。不过其实你加不加都一样。因为%Y就是4位的,%m也是补0的2位