藍森林首頁 | 返回主頁 | 本站地圖 | 站內搜索 | 聯繫信箱 |
 您目前的位置:首頁 > 自由軟件 > 技術交流 > 應用編程


    

藍森林 http://www.lslnet.com 2006年6月6日 10:18


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

我希望做一個時間差計算函數。現在(hp-ux)提供一個函數difftime可以進行兩個時間的差值計算,但我做的函數不能得到預先的值,請問為什麼??
程序為:
[code]
#include <stdio.h>;
#include <time.h>;
main()
{
        time_t lt,lt1;
        double result=0.0;
        lt=time(NULL);
        sleep(10);
        lt1=time(NULL);
        result=difftime(lt1,lt);
        printf("time = %ld\n",lt);
        printf("time1 = %ld\n",lt1);
        printf(ctime(&lt));
        printf(ctime(&lt1));
        printf(ctime(&result));
        printf("dff_time = %ld\n",result);
}
[/code]

輸出結果如下:
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這樣的數據字符串轉換成可以比對的數據好計算其中的差別??
大家看一下,謝謝。
:o  :o  :o  :o  :o

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

自己解決原來是顯示錯誤,printf()中應用lf做為輸出參數。

如何使用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型格式進行自由轉換



Copyright © 1999-2000 LSLNET.COM. All rights reserved. 藍森林網站 版權所有。 E-mail : webmaster@lslnet.com