免费注册 查看新帖 |

Chinaunix

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

[函数] 如何使用difftime函数???(HP-UX) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-11-25 09:37 |只看该作者 |倒序浏览
我希望做一个时间差计算函数。现在(hp-ux)提供一个函数difftime可以进行两个时间的差值计算,但我做的函数不能得到预先的值,请问为什么??
程序为:

  1. #include <stdio.h>;
  2. #include <time.h>;
  3. main()
  4. {
  5.         time_t lt,lt1;
  6.         double result=0.0;
  7.         lt=time(NULL);
  8.         sleep(10);
  9.         lt1=time(NULL);
  10.         result=difftime(lt1,lt);
  11.         printf("time = %ld\n",lt);
  12.         printf("time1 = %ld\n",lt1);
  13.         printf(ctime(&lt));
  14.         printf(ctime(&lt1));
  15.         printf(ctime(&result));
  16.         printf("dff_time = %ld\n",result);
  17. }
复制代码


输出结果如下:
time = 1069723995
time1 = 1069724005
Tue Nov 25 01:33:15 2003
Tue Nov 25 01:33:25 2003
Fri Feb  6 20:58:40 2004
dff_time = 2063681936

result的返回值预想为10,但现在的值明显不正确。
另外系统man说明如下:
difftime()        Return the difference in seconds between two
                        calendar times: time1 - time0.
说明应当可以的,不知道为什么不行???


还有各位知道如何将输入的2003/04/06这样的数据字符串转换成可以比对的数据好计算其中的差别??
大家看一下,谢谢。
        

论坛徽章:
0
2 [报告]
发表于 2003-11-25 09:58 |只看该作者

如何使用difftime函数???(HP-UX)

自己解决原来是显示错误,printf()中应用lf做为输出参数。

论坛徽章:
0
3 [报告]
发表于 2003-11-25 10:13 |只看该作者

如何使用difftime函数???(HP-UX)

系统提供的时间格式转换函数:
      #include <time.h>;
      char *asctime(const struct tm *timeptr);
      int asctime_r(const struct tm *timeptr, char *buffer, int buflen);
      char *ctime(const time_t *timer);
      int ctime_r(const time_t *timer, char *buffer, int buflen);
      double difftime(time_t time1, time_t time0);
      struct tm *gmtime(const time_t *timer);
      int gmtime_r(const time_t *timer, struct tm *result);
      struct tm *localtime(const time_t *timer);
      int localtime_r(const time_t *timer, struct tm *result);
      time_t mktime(struct tm *timeptr);
可以将tm与long型格式进行自由转换
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP