打印
[经验分享]

关于单片机C语言实现计算任意两个时间点相隔分钟数的问题

[复制链接]
351|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
saservice|  楼主 | 2023-9-28 21:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include <stdio.h>

typedef struct Time{
int Y,M,D,h,m,s;
}Time;

Time time1 = {20,7,28,5,30,10};
Time time2 = {20,7,29,6,10,10};

/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
*函数名称:Day_Diff
*函数功能:计算任意日期的相隔天数
*输入参数:time1 结构体指针 , time2结构体指针
*返回参数:天数
* <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
int Day_Diff(Time *time1 , Time *time2)
{
int year2 , month2 , day2;
int year1 , month1 , day1;
month1 = (time1->M + 9)%12;
year1 = time1->Y - month1/10;
day1 = 365 * year1 + year1 / 4 - year1 / 100 + year1 / 400 + (month1 *306 + 5) / 10 + (time1->D - 1);

month2 = (time2->M + 9) % 12;
year2 = time2->Y - month2 / 10;
day2 = 365 * year2 + year2 / 4 - year2 / 100 + year2 / 400 + (month2 *306 + 5) / 10 + (time2->D - 1);
return (day2 - day1);
}

/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
*函数名称:TimeSub
*函数功能:计算任意两个时间点相隔分钟数
*输入参数:Day 天数 , time1 结构体指针 , time2结构体指针
*返回参数:
* <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
int TimeSub(int Day , Time *time1,Time *time2)
{
long t1,t2,t;
t1 = (time1->h * 3600) + (time1->m * 60) + (time1 -> s);
t2 = (Day * 24 *3600) + (time2->h * 3600) + (time2->m *60) + (time2 -> s);

t = (t2 - t1) /60;
return t;
}


int main()
{
int num = 0;
int day = 0;
day = Day_Diff(&time1 , &time2);
printf("day = %d\r\n",day);
num = TimeSub(day,&time1 , &time2);
printf("%d\r\n" , num);
return 0;
}


使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

43

主题

1299

帖子

2

粉丝