Linux下实现给定一个日期求后面一天的日期

来源:互联网 发布:网络营销策划书内容 编辑:程序博客网 时间:2024/05/19 23:01

 

        有的时候在程序中往往需要用到一个日期后面的一天日期,为了方便以后工作中还会用到,写个模板来方便以后调用,下面为实现的demo程序

 

//tomorrow.c

/*************************************************************************************************    FileName : tomorrow.c     FileFunc : 给出一个日期(YYYYMMDD),返回这个日期后面的一天(给出的和返回的日期都是字符串格式)    Version  : V0.1      Author   : Sunrier      Date     : 2012-07-04 13:38:03     Descp    : Linux下实现给定一个日期求出后面的一天*************************************************************************************************/#include <stdio.h>#include <string.h>#include <time.h>typedef unsigned long ulYear;typedef unsigned long ulMonth;typedef unsigned long ulDay;void GetLocalTime( char *pOutTime )    {        time_t t;        struct tm tm1;        t = time(NULL);        memcpy(&tm1,localtime(&t),sizeof(struct tm));        sprintf(pOutTime,"%04d%02d%02d",tm1.tm_year+1900,tm1.tm_mon+1,tm1.tm_mday);    /*sprintf(pOutTime,"%04d-%02d-%02d %02d:%02d:%02d",tm1.tm_year+1900,tm1.tm_mon+1,tm1.tm_mday,tm1.tm_hour,tm1.tm_min,tm1.tm_sec);*/        /*YYYYMMDDHHMMSS(年月日时分秒)   */  }    unsigned long asc_long(unsigned char *pucSrc,unsigned int uiLen){unsigned int uiI;unsigned long ulTmp1,ulTmp2; ulTmp1 = 0 ; ulTmp2 = 1 ; pucSrc += uiLen ;   for (uiI = 0; uiI< uiLen ; uiI++) {  ulTmp1 += (ulTmp2 * (*--pucSrc & 0x0F) ) ;ulTmp2 *= 10 ; }  return (ulTmp1) ;}unsigned char *long_asc( unsigned char *pucDest,unsigned int uiLen,unsigned long ulSrc ){unsigned int uiI;unsigned char ucCh;unsigned char aucTab[10];unsigned char *pucPtr;unsigned long ulTmp1,ulTmp2;  ulTmp1 = ulSrc;  ulTmp2 = 100000000L ;  for (uiI = 0; uiI< 5; uiI++)  {ucCh = (unsigned char)(ulTmp1 / ulTmp2) ;aucTab[2*uiI] = ucCh / 10 + 0x30 ;aucTab[2*uiI+1] = ucCh % 10 + 0x30;ulTmp1 = ulTmp1 % ulTmp2;ulTmp2 = ulTmp2 / 100;  }memset( pucDest, 0x30, uiLen ) ;pucDest += uiLen ;  pucPtr = pucDest ;if ( uiLen > 10 )uiLen = 10 ;for( uiI=0; uiI < uiLen;uiI++)*--pucDest = aucTab[9-uiI] ;return( (unsigned char*)pucPtr );}int dateAdd(char *pcDate,char *pcDateAdd){unsigned char ucLeapYearFlag = 0;ulYear year = 0;ulMonth month = 0;ulDay day = 0;year = asc_long((unsigned char*)pcDate, 4);month = asc_long((unsigned char*)pcDate+4, 2);day = asc_long((unsigned char*)pcDate+6, 2);if((year%4==0 && year%100!=0) || (year%400==0))ucLeapYearFlag = 1;day += 1;switch( month ){case 1:if(day > 31){day = 1;month += 1;}break;case 2:if( ucLeapYearFlag ){if(day > 29){day = 1;month += 1;}}else{if(day > 28){day = 1;month += 1;}}break;case 3:if(day > 31){day = 1;month += 1;}break;case 4:if(day > 30){day = 1;month += 1;}break;case 5:if(day > 31){day = 1;month += 1;}break;case 6:if(day > 30){day = 1;month += 1;}break;case 7:if(day > 31){day = 1;month += 1;}break;case 8:if(day > 31){day = 1;month += 1;}break;case 9:if(day > 30){day = 1;month += 1;}break;case 10:if(day > 31){day = 1;month += 1;}break;case 11:if(day > 30){day = 1;month += 1;}break;case 12:if(day > 31){day = 1;month = 1;year += 1;}break;default :return 1;}long_asc((unsigned char*)pcDateAdd, 4, year);long_asc((unsigned char*)pcDateAdd+4, 2, month);long_asc((unsigned char*)pcDateAdd+6, 2, day);return 0;}


 

 

//demo.c

/*******************************************************************************************    FileName : demo.c     FileFunc : 定义测试文件      Version  : V0.1      Author   : Sunrier      Date     : 2012-07-04 13:37:00     Descp    : Linux下实现给定一个日期求出后面的一天(给出的和返回的日期都是字符串格式)*******************************************************************************************/#include <stdio.h>#include <string.h>int main(int argc,char *argv[]){unsigned char aucDate[30],aucDateAdd[30];memset(aucDate,0,sizeof(aucDate));GetLocalTime(aucDate);memset(aucDateAdd,0,sizeof(aucDateAdd));dateAdd(aucDate,aucDateAdd);printf("%s\n",aucDate);printf("%s\n",aucDateAdd);return 0;}


 

//makefile

#makefile  OBJS = demo    all:$(OBJS)    CFLAGS = -O -w -ansi       #CFLAGS = -O -Wall -ansi       CC = gcc $(CFLAGS)  demo:demo.c tomorrow.c@$(CC) -o $@ $? clean:@ls | grep -v ^makefile$$ | grep -v [.]c$$ | grep -v [.]h$$ | grep -v [.]sql$$ | grep -v [.]sh$$ | xargs rm -rf#makefile


 

[Sunrier@localhost Date]$ ls
demo.c  makefile  tomorrow.c
[Sunrier@localhost Date]$ make
[Sunrier@localhost Date]$ ls
demo  demo.c  makefile  tomorrow.c
[Sunrier@localhost Date]$ ./demo
20120704
20120705
[Sunrier@localhost Date]$

 

 

 

原创粉丝点击