当前位置:网站首页>C language calculation date interval days
C language calculation date interval days
2022-07-26 10:34:00 【Neo Wordsworth】
How many days are the two dates related ,
I saw one on the Internet “C Analysis of the classical algorithm for calculating the number of days between dates in language ” article , The code is as follows :
int day_diff(int year_start, int month_start, int day_start, int year_end, int month_end, int day_end)
{
int y2, m2, d2;
int y1, m1, d1;
m1 = (month_start + 9) % 12;
y1 = year_start - m1/10;
d1 = 365*y1 + y1/4 - y1/100 + y1/400 + (m1*306 + 5)/10 + (day_start - 1);
m2 = (month_end + 9) % 12;
y2 = year_end - m2/10;
d2 = 365*y2 + y2/4 - y2/100 + y2/400 + (m2*306 + 5)/10 + (day_end - 1);
return (d2 - d1);
}I use java Randomly generate some data to verify this c Code , Some data are incorrect , such as
4645-8-25
5074-4-28
java The calculation is as follows :
(int) ((c2.getTime().getTime() - c1.getTime().getTime()) / (1000 * 3600 * 24));Then I wrote a code myself ,
u8 IsLeapYear(u16 year){
if ((year & 0x03) == 0) {
if ((year >> 2) % 25 == 0) {
if ((year & 0x0f) == 0)
return 1;
} else {
return 1;
}
}
return 0;
}
const u8 offset[]= {0,0,1,5,6,8,9,11,12,13,15,16,18};
u32 CalDayDiff(MyDate* s,MyDate* e){
u32 yDiff,i;
u32 sum=0,sum2;
MyDate d;
yDiff=e->year-s->year;
if(yDiff>1){
for(i=s->year+1;i<e->year;i++){
sum+=IsLeapYear(i)?366:365;
}
yDiff=1;
}
switch(yDiff){
case 0:
sum=((e->month-1)<<5)-offset[e->month]+e->day;
if(e->month>2 && IsLeapYear(e->year))
sum+=1;
sum2=((s->month-1)<<5)-offset[s->month]+s->day;
if(s->month>2 && IsLeapYear(s->year))
sum2+=1;
sum-=sum2;
break;
case 1:
d.year=s->year;
d.month=12;
d.day=31;
sum+=CalDayDiff(s,&d);
d.year=e->year;
d.month=1;
d.day=1;
sum+=CalDayDiff(&d,e)+1;
break;
}
return sum;
}边栏推荐
- 【dectectron2】跟着官方demo一起做
- [gossip] error loading psychopg2 module: no module named psychopg2
- 原生JS-获取transform值 x y z及rotate旋转角度
- QRcode二维码(C语言)遇到的问题
- [C language] named type and anonymous type
- 【Halcon视觉】编程逻辑
- [Halcon vision] morphological corrosion
- canvas上传图片base64-有裁剪功能-Jcrop.js
- Kaptcha image verification code integration
- C语言回调函数
猜你喜欢

Application of.Net open source framework in industrial production

js翻页、kkpager.js翻页

Okaleido ecological core equity Oka, all in fusion mining mode
![[Halcon vision] image filtering](/img/4b/e73a8d589b49276d96621f0ef02449.png)
[Halcon vision] image filtering

centos8(liunx)部署WTM(ASP.NET 5)使用pgsql

equals与==的区别

我们的Web3创业项目,黄了

数据分析入门 | kaggle泰坦尼克任务(一)—>数据加载和初步观察
![[leetcode每日一题2021/8/30]528. 按权重随机选择【中等】](/img/13/c6cb176d7065035f60d55ad20ed1bf.png)
[leetcode每日一题2021/8/30]528. 按权重随机选择【中等】

Our Web3 entrepreneurship project is yellow
随机推荐
[leetcode每日一题2021/2/14]765. 情侣牵手
图片随手机水平移动-陀螺仪。360度设置条件
卸载魅族应用商店
【dectectron2】跟着官方demo一起做
.net5wtm (asp.net core) PgSQL unpacking operation
Using native JS to realize custom scroll bar (click to reach, drag to reach)
algorithm
.NET5WTM(ASP.NET Core) PGSql开箱操作
Nacos custom service change subscription
【Halcon视觉】编程逻辑
10 令 operator= 返回一个 reference to *this
Deduct daily question 838 of a certain day
.NET操作Redis Hash对象
Uniapp uses the simple method signalr (only for web debugging, cannot package apps)
Tradingview 使用教程
[leetcode每日一题2021/8/30]528. 按权重随机选择【中等】
微信公众号发布提醒(微信公众号模板消息接口)
equals与==的区别
Listening freely, the next stop of online text traffic competition?
[Halcon vision] Fourier transform of image