扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
最近做项目需要基于C语言实现一个获取两个日期之间共有多少天的函数,通过搜索一些资料,实现了int GetInternalDays(Date date1, Date date2)接口,该接口实现了data1和date2直间相差的天数。
10年积累的成都做网站、网站制作经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有颍州免费网站建设让你可以放心的选择与我们合作。
/*****************************************************************************************
* Copyright:Yue Workstation
*
* FileName:CountDate.c
*
* Function:count the days of two date
*
* Author:Abel Lee
*
* Created on:2011-4-18
*
* Log:
* ***************************************************************************************
* 2011-4-18 create.
*****************************************************************************************/
#include
#define TestMain main //The toggle of main function
typedef struct date
{
int year;
int month;
int day;
}Date;//Date struct
//The month of the date given have days in this year.
int daysInMonth[13] = {0,0,31,59,90,120,151,181,212,243,273,304,334};
/*****************************************************************************************
* Function Name:IsLeap
*
* Function:Whether the year is a Leap
*
* Input Parameter:year:The year need to be judged
*
* Return Value:1:Leap 0:No leap
*
* Author: Abel Lee
*
* Log:create on 2011-4-18
****************************************************************************************/
int IsLeap(const int year)
{
return (((year % 4 == 0) && year % 100 != 0) || year % 400 == 0);
}
/****************************************************************************************
* Function Name:CountDays
*
* Function:Count the days of in this year
*
* Input Parameter:CurrentDate:the day of in this year
*
* Return Value:The days of in this year
*
* Author: Abel Lee
*
* Log:create on 2011-4-18
****************************************************************************************/
int CountDays(const Date *currentDate)
{
int sumDays = daysInMonth[currentDate->month] + currentDate->day;
if(IsLeap(currentDate->year) && (currentDate->month >= 3))
{
sumDays++;
}
return sumDays;
}
/***************************************************************************************
* Function Name:SwapDate
*
* Function:Swap the two dates
*
* Input Parameter:date1:The first date
* date2:The second date
* Return Value:None
*
* Author:Li
*
* Log:create on 2011-4-19
**************************************************************************************/
void SwapDate(Date *date1,Date *date2)
{
int tmp = 0;
tmp = date1->year;
date1->year = date2->year;
date2->year = tmp;
tmp = date1->month;
date1->month = date2->month;
date2->month = tmp;
tmp = date1->day;
date1->day = date2->day;
date2->day = tmp;
return;
}
/*************************************************************************************
* Function Name:GetIntervalDays
*
* Function:Get the days between date1 and date2
*
* Input Parameter:date1:The first date
* date2:The second date
*
* Return Value:The days between date1 and date2
* -1 is the date format is error
*
* Author:Abel Lee
*
* Log:create on 2011-4-19
************************************************************************************/
int GetIntervalDays(Date *date1,Date *date2)
{
int days1 = 0;
int days2 = 0;
int sumDays = 0;
int i = 0;
int years = 0;
//Parameter range check
if(date1->year < 1900 || date1->year > 2038 || date2->year < 1900 || date2->year > 2038)
{
printf("The year range is 1900 ~ 2038/n");
return -1;
}
if(date1->month > 12 || date1->month < 1 || date2->month > 12 || date2->month < 1)
{
printf("The month range is 1 ~ 12/n");
return -1;
}
switch(date1->month)
{
case 2:
if(IsLeap(date1->year))
{
if(date1->day < 1 || date1->day > 29)
{
printf("%d-%d range is 0 ~ 29/n",date1->year,date1->month);
return -1;
}
}
else
{
if(date1->day < 1 || date1->day > 28)
{
printf("%d-%d range is 0 ~ 28/n",date1->year,date1->month);
return -1;
}
}
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if(date1->day < 1 || date1->day > 31)
{
printf("%d-%d range is 0 ~ 31/n",date1->year,date1->month);
return -1;
}
break;
case 4:
case 6:
case 9:
case 11:
if(date1->day < 1 || date1->day > 30)
{
printf("%d-%d range is 0 ~ 30/n",date1->year,date1->month);
return -1;
}
break;
default:
printf("The format of date is error!/n");
return -1;
}
switch(date2->month)
{
case 2:
if(IsLeap(date2->year))
{
if(date2->day < 1 || date2->day > 29)
{
printf("%d-%d range is 0 ~ 29/n",date2->year,date2->month);
return -1;
}
}
else
{
if(date2->day < 1 || date2->day > 28)
{
printf("%d-%d range is 0 ~ 28/n",date2->year,date2->month);
return -1;
}
}
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if(date2->day < 1 || date2->day > 31)
{
printf("%d-%d range is 0 ~ 31/n",date1->year,date1->month);
return -1;
}
break;
case 4:
case 6:
case 9:
case 11:
if(date2->day < 1 || date2->day > 30)
{
printf("%d-%d range is 0 ~ 30/n",date2->year,date2->month);
return -1;
}
break;
default:
printf("The format of date is error!/n");
return -1;
}
//date2 must be newer than date1
if(date1->year > date2->year)
{
SwapDate(date1,date2);
}
else if((date2->year == date1->year) && (date1->month > date2->month))
{
SwapDate(date1,date2);
}
else if((date2->year == date1->year) && (date1->month == date2->month) && (date1->day > date2->day))
{
SwapDate(date1,date2);
}
years = date2->year - date1->year;
days1 = CountDays(date1);
days2 = CountDays(date2);
sumDays = days2 - days1;
if(years == 1)
{
sumDays += 365;
}
else if(years > 1)
{
for(i = date1->year; i < date2->year; i++)
{
sumDays += 365;
if(IsLeap(i))
{
sumDays++;
}
}
}
return sumDays;
}
int main(int argc,char *argv[])
{
Date date1,date2;
int year,month,day;
printf("input the first date(eg. 2011 2 5):");
scanf("%d%d%d",&year,&month,&day);
date1.year = year;
date1.month = month;
date1.day = day;
printf("input the second date(eg. 2015 9 10):");
scanf("%d%d%d",&year,&month,&day);
date2.year = year;
date2.month = month;
date2.day = day;
printf("********%d********\n",GetIntervalDays(&date1,&date2));
printf("Application run successfully!\n");
return 0;
}
可见,一个如此简单的功能,C语言却需要这么多代码,相比与Java/Python等更高级语言,确实开发效率相对较低!
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流