您好,登錄后才能下訂單哦!
最近做項目需要基于C語言實現一個獲取兩個日期之間共有多少天的函數,通過搜索一些資料,實現了int GetInternalDays(Date date1, Date date2)接口,該接口實現了data1和date2直間相差的天數。
/*****************************************************************************************
* 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 <stdio.h>
#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等更高級語言,確實開發效率相對較低!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。