2012年11月1日 星期四
比較兩個任意日期的時間差
int cmptime(char *s_time,char *e_time){
int delta_sec;
int s_time_array[6],e_time_array[6];//[Y][M][D][h][m][s]
long long delta_s;
int delta_m,delta_h,delta_D;
char str_buf1[5],str_buf2[5];
int i=0,j=0;
//retreive data
while(1){
str_buf1[i]=*s_time;
str_buf2[i]=*e_time;
s_time++;
e_time++;
i++;
if(((*s_time == '/')&&(*e_time == '/'))||((*s_time == ':')&&(*e_time == ':'))){
str_buf1[i]='\0'; //string end
str_buf2[i]='\0';
s_time_array[j]=atof(str_buf1);
e_time_array[j]=atof(str_buf2);
memset(str_buf1,0,5);//clean buf.
memset(str_buf2,0,5);
i=0;
s_time++;//next char
j++;
}
else if(*s_time !== '\0'){
str_buf1[i]='\0'; //string end
str_buf2[i]='\0';
s_time_array[j]=atof(str_buf1);
e_time_array[j]=atof(str_buf2);
memset(str_buf1,0,5);//clean buf.
memset(str_buf2,0,5);
break;
}
}
//compare time
s_time_array[2]=julian_cnt(s_time_array[0],s_time_array[1],s_time_array[2]);//caculate julian days
e_time_array[2]=julian_cnt(e_time_array[0],e_time_array[1],e_time_array[2]);
while(s_time_array[0] < e_time_array[0]){ //** plus days if over the year**
if((s_time_array[0]%4==0 && s_time_array[0]%100!=0) || s_time_array[0]%400==0){ //leap year
e_time_array[2]=e_time_array[2]+366;
}
else{
e_time_array[2]=e_time_array[2]+365;
}
s_time_array[0]++;
}
if((e_time_array[5]-s_time_array[5])<0 delta_sec="delta_sec" nbsp="nbsp" p="p"> delta_s=e_time_array[5]-s_time_array[5]+60;
e_time_array[4]--;
}
else{
delta_s=e_time_array[5]-s_time_array[5];
}
if((e_time_array[4]-s_time_array[4])<0 delta_min="delta_min" nbsp="nbsp" p="p"> delta_m=e_time_array[4]-s_time_array[4]+60;
e_time_array[3]--;
}
else{
delta_m=e_time_array[4]-s_time_array[4];
}
if((e_time_array[3]-s_time_array[3)<0 delta_hour="delta_hour" nbsp="nbsp" p="p"> delta_h=e_time_array[3]-s_time_array[3]+24;
e_time_array[2]--;
}
else{
delta_h=e_time_array[3]-s_time_array[3];
}
if((e_time_array[2]-s_time_array[2)<0 error="error" nbsp="nbsp" p="p"> printf("input error");
exit(0);
}
else{
delta_D=e_time_array[2]-s_time_array[2]; //delta_day
}
delta_s=delta_s+60*delta_m+3600*delta_h+86400*delta_D; //finally convert to numbers in sec
return delta_s;
}
int julian_cnt(int *yr,int *mon,int *day){
int jday=0;
int year_day[12];
int i=0;
if((*yr%4==0 && *yr%100!=0) || *yr%400==0){ //leap year
day_yr[12]={31 29 31 30 31 30 31 31 30 31 30 31};
}
else{ //common year
day_yr[12]={31 28 31 30 31 30 31 31 30 31 30 31};
}
while(i+1 != *mon){
jday=jday+day_yr[i];
}
jday=jday+*day;
return jday;
}
0>0>0>0>
標籤:
programming
訂閱:
文章 (Atom)