当前位置:网站首页>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;
}边栏推荐
- 2022pta平时训练题(1~10题字符串处理问题)
- MLX90640 红外热成像仪测温传感器模块开发笔记(六)
- 404页面和路由钩子
- 并行、并发及对于高并发优化的几个方向
- Cause: could't make a guess for solution
- Asynctask < T> decoration and await are not used in synchronous methods to obtain asynchronous return values (asynchronous methods are called in synchronous methods)
- MLX90640 红外热成像仪测温传感器模块开发笔记(六)红外图像伪彩色编码
- Interview questions and answers for the second company (2)
- The software cannot be opened
- 关于函数模板描述错误的是(链接格式错误怎么解决)
猜你喜欢

Redis Docker实例与数据结构

Introduction to data analysis | kaggle Titanic mission (I) - > data loading and preliminary observation

Review of database -- 1. Overview

js 获得当前时间,时间与时间戳的转换

.NET 开源框架在工业生产中的应用

【Halcon视觉】图像灰度变化

【Halcon视觉】算子的结构

【机器学习小记】【搭建循环神经网络及其应用】deeplearning.ai course5 1st week programming(keras)
![[leetcode每日一题2021/5/8]1723. 完成所有工作的最短时间](/img/e7/a48bb5b8a86cbc4cd5b37bb16661a8.png)
[leetcode每日一题2021/5/8]1723. 完成所有工作的最短时间

Navicat15 MySQL (centos7) connected to local virtual machine
随机推荐
Review of database -- 1. Overview
STM32 阿里云MQTT esp8266 AT命令
Centos8 (liunx) deploying WTM (asp.net 5) using PgSQL
[Halcon vision] array
Some web APIs you don't know
Cause: could't make a guess for solution
.NET操作Redis Hash对象
【Halcon视觉】数组
Introduction to data analysis | kaggle Titanic mission
Application of.Net open source framework in industrial production
上传图片获取宽高
Using native JS to realize custom scroll bar (click to reach, drag to reach)
Uniapp uses the simple method signalr (only for web debugging, cannot package apps)
【Halcon视觉】编程逻辑
Introduction to data analysis | kaggle Titanic mission (I) - > data loading and preliminary observation
Interview questions and answers of the first company (I)
【C#语言】具名类型和匿名类型
Mlx90640 infrared thermal imager temperature sensor module development notes (6)
Review of database -- 3. SQL language
[Halcon vision] affine transformation