c語言算時間差
A. c語言中如何計算時間差
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main()
{
unsigned char time1[] = {10, 8, 31, 9, 26 };
unsigned char time2[] = { 10, 8, 31, 9, 50 };
struct tm t1 = {0};
struct tm t2 = {0};
time_t _t1;
time_t _t2;
double diff;
t1.tm_year = time1[0] + 100;
t1.tm_mon = time1[1];
t1.tm_mday = time1[2];
t1.tm_hour = time1[3];
t1.tm_min = time1[4];
t2.tm_year = time2[0] + 100;
t2.tm_mon = time2[1];
t2.tm_mday = time2[2];
t2.tm_hour = time2[3];
t2.tm_min = time2[4];
_t1 = _mkgmtime( &t1 );
_t2 = _mkgmtime( &t2 );
diff = difftime(_t2, _t1 );
printf( "相差 %.0f 分鍾
", diff / 60 );
}
(1)c語言算時間差擴展閱讀:
C語言中有兩個相關的函數用來計算時間差,分別是:
time_t time( time_t *t) 與 clock_t clock(void)
頭文件: time.h
計算的時間單位分別為: s , ms
time_t 和 clock_t 是函數庫time.h 中定義的用來保存時間的數據結構
返回值:
1、time : 返回從公元1970年1月1號的UTC時間從0時0分0秒算起到現在所經過的秒數。如果參數 t 非空指針的話,返回的時間會保存在 t 所指向的內存。
2、clock:返回從「開啟這個程序進程」到「程序中調用clock()函數」時之間的CPU時鍾計時單元(clock tick)數。 1單元 = 1 ms。
所以我們可以根據具體情況需求,判斷採用哪一個函數。
具體用法如下例子:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
time_t c_start, t_start, c_end, t_end;
c_start = clock(); //!< 單位為ms
t_start = time(NULL); //!< 單位為s
system("pause");
c_end = clock();
t_end = time(NULL);
//!<difftime(time_t, time_t)返回兩個time_t變數間的時間間隔,即時間差
printf("The pause used %f ms by clock() ",difftime(c_end,c_start));
printf("The pause used %f s by time() ",difftime(t_end,t_start));
system("pause");
return 0;
}
因此,要計算某一函數塊的佔用時間時,只需要在執行該函數塊之前和執行完該函數塊之後調用同一個時間計算函數。再調用函數difftime()計算兩者的差,即可得到耗費時間。
B. 【急求】c語言 求兩個時間的差值
/*可以處理空格!!!*/
#include<stdio.h>
#include<string.h>
structTTime
{
inth,m,s;
longGetSec(){return3600L*h+60*m+s;}
voidStrToTime(char_str[])
{
inti,j,len=strlen(_str);
/*去空格*/
for(i=0;i<len;++i)
if(_str[i]=='')
{
for(j=i;j<len-1;++j)
_str[j]=_str[j+1];
--len;
i=-1;
continue;
}
/*讀小時*/
j=0;
for(i=0;i<len;++i)
if(_str[i]==':')
break;
else
j=j*10+_str[i]-'0';
h=j;
/*讀分鍾*/
j=0;
for(++i;i<len;++i)
if(_str[i]==':')
break;
else
j=j*10+_str[i]-'0';
m=j;
/*讀秒*/
j=0;
for(++i;i<len;++i)
j=j*10+_str[i]-'0';
s=j;
}
voidToPlan(longt)
{
inthh,mm,ss;
hh=t/3600;
t%=3600;
mm=t/60;
t%=60;
ss=t;
printf("%2.2d:%2.2d:%2.2d ",hh,mm,ss);
}
}Ta,Tb,Tc;
voidmain()
{
chara[105],b[105];
gets(a);
gets(b);
Ta.StrToTime(a);
Tb.StrToTime(b);
printf("sec:%ld,time:",Tb.GetSec()-Ta.GetSec());
Tc.ToPlan(Tb.GetSec()-Ta.GetSec());
}
C. C語言中如何計算時間差 秒
C語言中有時間函數(time函數),可以列印出系統時間,相減就行。當然,也有各類延時函數。sleep族函數。
D. 如何用c語言計算兩個時間的時間差
#include
<time.h>
clock_t
start;
clock_t
end;
start
=
clock();
...//需要計算時間的代碼片斷
end
=
clock();
printf("%ld",
(end
-
start)/clk_tck/60);
E. c語言如何計算時間差
boolcomputer(file_date_tt1,file_date_tt2)
{
intmin=t1.i_dd<t2.i_dd?t1.i_dd:t2.i_dd;
inttime1=(t1.i_dd-min)*24+t1.i_hh;
inttime2=(t2.i_dd-min)*24+t2.i_hh;
if(time1>time2)
{
if(time1-time2>12)
{
printf("時間超過12個小時! ");
returntrue;
}
printf("時間不超過12個小時! ");
returnfalse;
}
else
{
if(time2-time1>12)
{
printf("時間超過12個小時! ");
returntrue;
}
printf("時間不超過12個小時! ");
returnfalse;
}
}
F. C語言中用difftime計算兩個時間差問題,求解!
你好。
問題分析:
time_t 只能表示從1970年1月1日0時0分0秒 到此時的秒數,1970年1月1日0時0分0秒 以前的時間它是無法表示的。而struct tm 的成員變數tm_year 的值為實際年份減去 1900,你的程序中給timeptr1.tm_year 和timeptr2.tm_year 都賦值為 0,就相當於這兩個時間都是 1900 年,1900 年在 1970年1月1日0時0分0秒 以前,所以用 mktime 函數計算的結果 time_t 是無法表示的,t1 和 t2 的結果就都是 0,最終 difftime(0,0) 結果當然也是 0。
解決方案:
給struct tm 的成員變數tm_year 一個大於 70 的值如 71,即 1971年,總之保證這個時間在 1970年1月1日0時0分0秒 之後即可。
程序清單:
#include "stdio.h"
#include "time.h"
int main()
{
time_t t1,t2;
struct tm timeptr1,timeptr2;
timeptr1.tm_mday=1;
timeptr1.tm_mon=1;
timeptr1.tm_year=1971-1900;
timeptr1.tm_sec=timeptr1.tm_min=timeptr1.tm_hour=
timeptr1.tm_wday=timeptr1.tm_yday=timeptr1.tm_isdst=0;
timeptr2.tm_mday=2;
timeptr2.tm_mon=1;
timeptr2.tm_sec=5;
timeptr2.tm_year=1971-1900;
timeptr2.tm_min=timeptr2.tm_hour=timeptr2.tm_wday=
timeptr2.tm_yday=timeptr2.tm_isdst=0;
t1=mktime(&timeptr1);
t2=mktime(&timeptr2);
printf("時間差: >> %lf ",difftime(t2,t1));
system("pause");
return 0;
}
運行結果:
望採納!
G. C語言怎麼把時間1和時間2換成分鍾,再計算時間差
unsigned int time1, time2, h, m;
time1 = h1 * 60 + m1;
time2 = h2 * 60 + m2;
time1 = time1 > time2 ? time1 - time2 : time2 - time1;
h = time1 / 60;
m = time1 % 60;
printf("時間差為%u小時%u分鍾\r\n", h, m);
H. c語言中,如何比較兩個時間相差幾天
計算兩個年月日之間的天數,思路是分別算出日期的總天數然後相減。
要考慮閏年的情況,判斷閏年的口訣:4年一閏,100年不閏,400年再閏。
((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
網上找了一個(偷懶= =!),修改下如下:
#include <stdio.h>
int sum(int y,int m,int d)
{
unsigned char x[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int i,s=0;
for(i=1;i<y;i++)
if(i%4==0 && i%100!=0 || i%400==0)
s+=366;//閏年
else
s+=365;//平年
if(y%4==0 && y%100!=0 || y%400==0)
x[2]=29;
for(i=1;i<m;i++)
s+=x[i];//整月的天數
s+=d;//日的天數
return s;//返回總天數,相對公元1年
}
void main()
{
unsigned char y1,m1,d1,y2,m2,d2;
int s1,s2;
printf("輸入第一個年 月 日:");
scanf("%d %d %d",&y1,&m1,&d1);
printf("輸入第二個年 月 日:");
scanf("%d %d %d",&y2,&m2,&d2);
s1=sum(y1,m1,d1);
s2=sum(y2,m2,d2);
if (s1 > s2)
printf("相差天數:%ld ",s1-s2);
else
printf("相差天數:%ld ",s2-s1);
}
I. C語言中計算2個時間的差值的函數
#include<time.h>
#include<stdio.h>
time_t_mktime(char*slTime)/**yyyy-mm-dd**/
{
structtmtm_t;
intyear;
intmon;
intday;
sscanf(slTime,"%4d-%2d-%2d",&year,&mon,&day);
tm_t.tm_year=year-1900;
tm_t.tm_mon=mon-1;
tm_t.tm_mday=day;
tm_t.tm_hour=12;
tm_t.tm_min=00;
tm_t.tm_sec=01;
tm_t.tm_wday=0;
tm_t.tm_yday=0;
tm_t.tm_isdst=0;
returnmktime(&tm_t);
}
intdaydiff(char*date1,char*date2)/**yyyy-mm-dd**/
{
time_tt1=_mktime(date1);
time_tt2=_mktime(date2);
time_tdiff=abs(t2-t1);
return(int)(diff/(24*60*60));
}
intmain()
{
chardate1[12],date2[12];
printf("inputdate1:");
scanf("%s",date1);
fflush(stdin);
printf("inputdate2:");
scanf("%s",date2);
fflush(stdin);
printf("%s-%sis%ddays ",date1,date2,daydiff(date1,date2));
}
J. C語言怎樣提取系統時間且將其用於求時間差運算
提供兩種方法作為參考:第一種,使用系統函數GetSystemTime和結構體SYSTEMTIME#include <windows.h>SYSTEMTIME stbegin,stend在開始時間點運行 GetSystemTime(&stbegin); 在結束時間點運行 GetSystemTime(&stend); 這時獲得起始時間和結束時間,可以進行時間差運算。typedef struct _SYSTEMTIME {
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds; } SYSTEMTIME,
*PSYSTEMTIME; 第二種,如果使用MFC或者ATL庫,可以使用COleDateTime類COleDateTime stbegin,stend;開始時間:stbegin=COleDateTime.GetCurrentTime();結束時間:stend=COleDateTime.GetCurrentTime();時間差:COleDateTime stresult=stend-stend;再使用COleDateTime的format函數可以把值轉換成一個字元串