📄 calculationday.c
字号:
/************************************************************************/
/* */
/* 僾儘僙僗柤丗 巜掕擔晅偺N擔屻偺擔晅傪庢摼偡傞 丂丂丂 丂*/
/* 娭 悢 柤丗 calculationDay 丂丂丂丂 */
/* 婡擻奣梫 丗 巜掕擔晅偺N擔屻偺擔晅傪庢摼偡傞 */
/* 曉媝抣 丗 惓忢廔椆 0 */
/* 丗 堎忢廔椆 -1 */
/* 拲堄帠崁 丗 摿偵側偟 */
/* */
/* 僷儔儊乕僞丗IN 丗char* pc_date 寁嶼慜擔晅 */
/* int i_addDay 捛壛擔悢 */
/* OUT丗char* pc_resultDate 寁嶼屻擔晅 */
/* */
/* 怴婯嶌惉幰丗妿巙暉 */
/* 怴婯嶌惉擔丗2005擭06寧24擔 */
/* 僶乕僕儑儞丗Ver1.1.0 */
/* 廋惓幰丗 妿巙暉 */
/* 廋惓擔丗 2005擭07寧07擔 */
/* 僶乕僕儑儞:Ver1.2.0 */
/* */
/************************************************************************/
/*----------------------------------------------------------------------*/
/* 僀儞僋儖乕僪 */
/*----------------------------------------------------------------------*/
#include<string.h>
#include<stdio.h>
#include<time.h>
#define DATE_SIZE 8
#define YEAR_SIZE 4
#define MONTH_SIZE 2
#define DAY_SIZE 2
int calculationDay(const char *pc_date,int i_addDay,char *pc_resultDate){
char pc_year[YEAR_SIZE + 1];
char pc_month[MONTH_SIZE + 1];
char pc_day[DAY_SIZE + 1];
int i_year;
int i_month;
int i_day;
struct tm st_creTime;
int i_getYear;
int i_getMonth;
int i_getDay;
printf("擔晅寁嶼張棟奐巒\n");
/* 弶婜壔 */
memset(pc_year,0,sizeof(pc_year));
memset(pc_month,0,sizeof(pc_month));
memset(pc_day,0,sizeof(pc_day));
/* 僷儔儊乕僞偐傜擭丒寧丒擔傪庢摼 */
strncpy(pc_year,pc_date,YEAR_SIZE);
strncpy(pc_month,pc_date+YEAR_SIZE,MONTH_SIZE);
strncpy(pc_day,pc_date+YEAR_SIZE+MONTH_SIZE,DAY_SIZE);
i_year=atoi(pc_year);
i_month=atoi(pc_month);
i_day=atoi(pc_day);
/* 僒僀僘僠僃僢僋 */
printf("僒僀僘僠僃僢僋\n");
if ( strlen(pc_year) > YEAR_SIZE ) {
printf("擭僒僀僘僄儔乕");
printf("擔晅寁嶼張棟堎忢廔椆");
exit(2);
}
if ( strlen(pc_month) > MONTH_SIZE ) {
printf("寧僒僀僘僄儔乕");
printf("擔晅寁嶼張棟堎忢廔椆");
exit(2);
}
if ( strlen(pc_day) > DAY_SIZE ) {
printf("擔僒僀僘僄儔乕");
printf("擔晅寁嶼張棟堎忢廔椆");
exit(2);
}
/* 擭丒寧丒擔傪struct tm峔憿懱偵戙擖 */
st_creTime.tm_year = i_year-1900;
st_creTime.tm_mon = i_month-1;
st_creTime.tm_mday = i_day+i_addDay;
st_creTime.tm_hour = 0;
st_creTime.tm_min = 0;
st_creTime.tm_sec = 0;
st_creTime.tm_isdst = -1;
/* 寁嶼屻偺擔晅傪媮傔傞 */
printf("寁嶼屻偺擔晅傪媮傔傞\n");
if ( mktime(&st_creTime) == -1 ) {
printf("擔晅寁嶼張棟堎忢廔椆");
return 0;
}
i_getYear = st_creTime.tm_year+1900;
i_getMonth = st_creTime.tm_mon+1;
i_getDay = st_creTime.tm_mday;
/* 擭悢偺僠僃僢僋 */
if ( i_getYear < 1 || i_getYear > 9999 ) {
printf("擔晅寁嶼張棟堎忢廔椆");
exit(2);
}
/* 寁嶼屻擔晅傪堷悢偵愝掕 */
sprintf(pc_resultDate,"%04d%02d%02d",i_getYear,i_getMonth,i_getDay);
return 0;
}
int main(void)
{
char pc_ymd[]="20040301";
int addDay=-1;
char pc_resultYmd[DATE_SIZE+1];
memset(pc_resultYmd,0,sizeof(pc_resultYmd));
int i_return = calculationDay(pc_ymd,addDay,pc_resultYmd);
if(i_return!=0){
printf("Error!");
exit(2);
}
sleep(1000);
printf("Result date is:%s\n",pc_resultYmd);
sleep(1000);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -