- 论坛徽章:
- 0
|
不知道是否有漏洞, 楼主自己测试看看。如果早于1970 则调整一下 for(id=1970;id<day/10000;id++) 这一句。
- #include <stdio.h>
- int get_day_off(int day)
- {
- int id,tot=0;
- for(id=1970;id<day/10000;id++)
- {
- if((id%400==0)||((id%4==0)&&(id%100!=0)))
- tot+=366;
- else
- tot+=365;
- };
- for(id=1;id<day/100%100;id++)
- {
- switch(id)
- {
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- tot+=31;
- break;
- case 4:
- case 6:
- case 9:
- case 11:
- tot+=30;
- break;
- case 2:
- if((day/10000%400==0)||((day/10000%4==0)&&(day/10000%100!=0)))
- tot+=29;
- else
- tot+=28;
- };
- };
- tot+=day%100;
- return tot;
- };
- int main(int argc, char* argv[])
- {
- char a[20],b[20];
- int ia,ib,off;
- strcpy(a,"19840308");
- strcpy(b,"19990526");
- sscanf(a,"%d",&ia);
- sscanf(b,"%d",&ib);
- off=get_day_off(ib)-get_day_off(ia);
- printf("off is %d\n",off);
- return 0;
- }
复制代码
SQL> select to_date('19990526','yyyymmdd') - to_date('19840308','yyyymmdd') from
dual;
TO_DATE('19990526','YYYYMMDD')-TO_DATE('19840308','YYYYMMDD')
-------------------------------------------------------------
5557
这个是对的。 |
|