⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pub_datetime.c

📁 自已结累的Unix下C语言开发函数库
💻 C
字号:
/* *日期和时间的一些函数 *zyq - shilyu - cff 0901 *functionnum 006 */ #include "./../inc/pub.h"#if !defined( DEBUG )#define DEBUG#endiftypedef struct Date { char   YYYY   [ 5 ]; char   MM     [ 3 ]; char   DD     [ 3 ];}DATE ;/* 001 * FUNCTION NAME :int  f_Is_Nleapyear ( unsigned short YYYY ) * Usage: YYYY 是年份转换成的整数            * Discription: 检查YYYY是否闰年 * OUTPUT: 0 - No, 1  - Yes ,  */int  Is_RunYear ( unsigned short YYYY ){   unsigned short           nian    ;   unsigned short           N1      ;   unsigned short           N2      ;   unsigned short           N3      ;           nian =  YYYY ;    N1 = nian % 4 ;    N2 = nian % 100 ;    N3 = nian % 400 ;    if ((( N1 == 0 ) && ( N2 != 0 )) || ( ( N2 == 0 ) && ( N3 == 0 ) ) )        return 1 ;         return 0 ;}/* 002 * FUNCTION NAME : IntervalDate ( char *pInDate , char *pOutDate , long n ) * Usage:  pInDate  char [9],  pOutDate char [9] , n  long *         起始日期            输出日期            间隔天数 * Disc :  计算起始日期的n天后的日期 * OUTPUT:THE OUTPUT IS pOutDate is the date after  n days of InDate */void IntervalDate  (  char *pInDate , char *pOutDate , long n ){   unsigned short     Year       ;   unsigned short     Mon        ;   unsigned short     Day        ;   DATE               StartDate  ;   DATE               EndDate    ;   short              rc         ;   unsigned long      i          ;   char               TmpStr [4] ;      i = 0 ;   #ifdef  DEBUG      printf ( "%s\n", pInDate  );   #endif         memset ( StartDate.YYYY , 0x00,  5  );   memset ( StartDate.MM ,   0x00,  3  );   memset ( StartDate.DD ,   0x00,  3  );      memcpy ( StartDate.YYYY , pInDate     , 4 );   memcpy ( StartDate.MM   , pInDate + 4 , 2 );   memcpy ( StartDate.DD   , pInDate + 6 , 2 );   #ifdef DEBUG      printf ( "%s+++%s+++%s\n",  StartDate.YYYY ,  StartDate.MM ,                 StartDate.DD  );   #endif      Year = atoi ( StartDate.YYYY  );   Mon  = atoi ( StartDate.MM    );   Day  = atoi ( StartDate.DD    );   #ifdef DEBUG      printf ( "%d---%d---%d\n", Year , Mon , Day  );   #endif    if ( n == 0 )   {      strcpy (  pOutDate , pInDate );      return ;	   }	      if ( n > 0 )   {      for ( ;; )      {         if ( i < n )         {            i ++ ;       	   Day = Day + 1 ;   	        	      rc = Is_RunYear ( Year );   	      if ( rc == 0 )   	      {   	         if ( ( Day == 29 ) && ( Mon == 2 ) )   	         {   	            Day = 1 ;   	            Mon = Mon + 1 ;   	         }   	      }   	      else   	      {   	         if ( ( Day == 30 ) && ( Mon == 2 ) )   	         {   	            Day = 1 ;   	            Mon = Mon + 1 ;   	         }   	      }   	          	      if ( ( Day == 31 ) && ( Mon == 4 ||  Mon == 6 || Mon == 9 ||   	                              Mon == 11 ) )   	      {   	         Day = 1 ;   	         Mon = Mon + 1 ;   	      }   	          	      if ( ( Day == 32 ) && ( Mon == 1 || Mon == 3 ||  Mon == 5 ||   	                              Mon == 7 || Mon == 8 || Mon == 10 ) )   	      {   	         Day = 1 ;   	         Mon = Mon + 1 ;   	      }   	          	      if (  Day == 32 &&  Mon == 12 )   	      {   	         Day = 1 ;   	         Mon = 1 ;   	         Year = Year + 1 ;   	      }   	   }   	   else         {   	     break ;         }      }          }	   if ( n < 0 )   {      n = 0 - n  ;      for ( ;; )      {         if ( i < n )   	   {            i ++ ;       	   Day = Day - 1 ;   	        	    rc = Is_RunYear ( Year );   	    if ( rc == 0 )   	    {   	       if ( ( Day == 0 ) && ( Mon == 3 ) )   	       {   	          Day = 28 ;   	          Mon = Mon - 1 ;   	       }   	    }   	    else   	    {   	       if ( ( Day == 0 ) && ( Mon == 3 ) )   	       {   	          Day = 29 ;   	          Mon = Mon - 1 ;   	       }   	    }   	        	    if ( ( Day == 0 ) && ( Mon == 2 || Mon == 4 ||  Mon == 6 ||    	                           Mon == 9 || Mon == 8  || Mon == 11 ) )   	    {   	       Day = 31 ;   	       Mon = Mon - 1 ;   	    }   	        	        	    if ( ( Day == 0 ) && ( Mon == 12 || Mon == 5 || Mon == 7 ||   	                           Mon == 10 ) )   	    {   	       Day = 30 ;   	       Mon = Mon - 1 ;   	    }   	        	    if (  Day == 0 &&  Mon == 1 )   	    {   	       Day = 31 ;   	       Mon = 12 ;   	       Year = Year - 1 ;   	    }         }   	 else   	    break ;      }   }   #ifdef  DEBUG      printf ( "%d---%d---%d\n", Year , Mon , Day  );   #endif   memset ( pOutDate , 0x00 , sizeof ( pOutDate ) ) ;   memset ( TmpStr ,   0x00 , sizeof ( TmpStr   ) ) ;      sprintf( TmpStr, "%04d%02d%02d", Year ,  Mon  , Day );            strcpy ( pOutDate ,  TmpStr ) ;             return ;   }/* 003 *将日期格式化输出 yyyy年mm月dd日  */void OutChinaDate ( InputStr, OutputStr )char     *InputStr  ;char     *OutputStr ;{   char  TmpDate[ 15 ] ;   char  TmpStr [ 15 ] ;   memset( TmpStr , 0x00, sizeof( TmpStr ) ) ;   memcpy( TmpStr, InputStr, 4 ) ;   strcpy( TmpDate, TmpStr ) ;   strcat( TmpDate, "年" ) ;   memset( TmpStr , 0x00, sizeof( TmpStr ) ) ;   memcpy( TmpStr, InputStr+4, 2 ) ;   strcat( TmpDate, TmpStr ) ;   strcat( TmpDate, "月" ) ;   memset( TmpStr , 0x00, sizeof( TmpStr ) ) ;   memcpy( TmpStr, InputStr+6, 2 ) ;   strcat( TmpDate, TmpStr ) ;   strcat( TmpDate, "日" ) ;   strcpy ( OutputStr , TmpDate ) ;}   /* 004 *获得当前次准确时间 */int GetCurtTimeb( struct timeb *ptimeb ){	ftime( ptimeb ) ;	return OK ;} /* 005  *计算得到两个时间之间有多长返回为毫秒  */int GetBetweenTimeb1ToTimeb2( timeb1, timeb2, pStrTime )struct timeb timeb1, timeb2 ;char   *pStrTime ;{   sprintf( pStrTime, "%06d", (timeb2.time -timeb1.time ) * 1000+                            timeb2.millitm - timeb1.millitm);                            	return OK ;}/* 006 *获得本地日期和时间 */void getlocaldatetime( char *pStrDateTime ){   char TmpBuf[100];   struct tm   *time_p;   time_t timeval;   time(&timeval);   time_p = localtime ( &timeval) ;   memset( TmpBuf, 0x00, sizeof( TmpBuf ));   sprintf ( TmpBuf, "20%02d%02d%02d%02d%02d%02d", time_p->tm_year - 100, time_p->tm_mon + 1, time_p->tm_mday,   time_p->tm_hour, time_p->tm_min, time_p->tm_sec) ;   memcpy( pStrDateTime, TmpBuf, strlen( TmpBuf ) );}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -