免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7825 | 回复: 3
打印 上一主题 下一主题

[函数] 谁用过strftime和strptime函数,给一个例子。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-12-16 10:22 |只看该作者 |倒序浏览
我实验这两各函数不知道为什么没有输出,请指教。
代码如下:
  1. #include <time.h>;
  2. #include <stdio.h>;
  3. #include <string.h>;
  4. #include <strings.h>;
  5. #include <locale.h>;
  6. int main(void)
  7. {
  8.         struct tm tm1;
  9.         time_t t;
  10.         char *sp;
  11.         char buf[1024];
  12.         tm1.tm_sec=10;
  13.         tm1.tm_min=20;
  14.         tm1.tm_hour=10;
  15.         tm1.tm_mon=9;
  16.         tm1.tm_mday=21;
  17.         tm1.tm_year=2001-1900;
  18.         memset(buf,0,1024);
  19.         t=mktime(&tm1);
  20.         memcpy(&tm1,localtime(&t),sizeof(struct tm));
  21.         /*strcpy(buf,"1999/06/08010101");*/
  22.         sp=strptime(buf,"%m/%d/%y",&tm1);
  23.         printf("tm1.tm_year = [%d]\n",tm1.tm_year);
  24.         printf("tm1.tm_mon = [%d]\n",tm1.tm_mon);
  25.         printf("tm1.tm_mday= [%d]\n",tm1.tm_mday);
  26.         /*sp=strptime(buf,"[%a] [%A] [%b] [%B] [%c] [%C] \n [%d] [%D] [%e] [%h]
  27. [%H] \n [%I] [%j] [%m] [%M] [%n] [%p] \n [%r] [%R] [%S] [%t] [%T] [%U] \n [%w] [
  28. %W] [%x] [%X] [%y] [%Y] \n [%%] [%Ec] [%EC] [%Ex] [%EX] [%Ey] [%EY] [%Od] \n [%O
  29. e] [%OH] [%OI] [%Om] [%OM] \n [%OS] [%OU] [%Ow] [%Oy]",&tm1);*/   /* 原先的格式 */
  30.         printf("buf = { [%s] }\n",buf);
  31.         printf("sp = { [%s] }\n",sp);
  32. }

复制代码

结果:
tm1.tm_year = [101]
tm1.tm_mon = [9]
tm1.tm_mday= [21]
buf = { [] }
sp = { [] }

buf和sp中什么也没有,tm中数据也没有变化,请问这两各函数是如何使用的????

论坛徽章:
0
2 [报告]
发表于 2003-12-16 10:45 |只看该作者

谁用过strftime和strptime函数,给一个例子。

没人用过????

论坛徽章:
0
3 [报告]
发表于 2003-12-16 11:07 |只看该作者

谁用过strftime和strptime函数,给一个例子。

我知道了,原来strftime函数是tm到字符串,strptime函数是字符串到tm结构一对函数,斑竹请删除这个帖子吧,我已经明白了。

论坛徽章:
0
4 [报告]
发表于 2003-12-16 11:37 |只看该作者

谁用过strftime和strptime函数,给一个例子。

偶写过的一个比较傻的函数:

typedef struct mytime
{
        char        ymdhms[20];
        char        ymdhms2[16];
        char        ymd[9];
} MYTIME;


/* -------------------------------------
** get system datetime in string format
*/
int timenow(MYTIME *mytime)
{
        time_t        tt_time;
        struct tm mytm;
       
        tt_time = time(NULL);
        memset(mytime, 0, sizeof(MYTIME));
       
        localtime_r(&tt_time, &mytm);

        if(strftime(mytime->;ymdhms, 20, "%Y/%m/%d %X", &mytm)==19)
                if(strftime(mytime->;ymdhms2, 16, "%Y%m%d.%H%M%S", &mytm)==15)
                        if(strftime(mytime->;ymd, 9 , "%Y%m%d", &mytm)==
                                return 0;
       
        memset(mytime, 0, sizeof(MYTIME));       
        return -1;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP