C 时间格式化

-----------------------------.h-----------------------------
#ifndef formatTime_h
#define formatTime_h

#include  <stdio.h>

int  LeapYear( int  year);  // 判断闰年
struct  tm  * DayAdd( int  dayNUm);  //dayNum  单位:天  0 代表获取当前时间,整整数代表获取以后的时间,负数代表向前的时间
struct  tm  * MonthAdd( int  monthNum, struct  tm  * inTm); //inTm null 表示当前时间
struct  tm  * LastDayOfMonth( int  monthNum, struct  tm  * inTm);

#endif  /* formatTime_h */

-----------------------------.c-----------------------------
#include  "formatTime.h"
#include
  <time.h>
#include
  <stdio.h>
#include
  <stdlib.h>
#include
<math.h>

int  LeapYear( int  year)   // 判断闰年
{
   
  int  ret =  0 ;
   
  if  ((year% 4 == 0  && year% 100 != 0 ) || year% 400  ==  0 ) {
        ret =
  0 ;
       
  return  ret;
    }
   
  else {
        ret =
  1 ;
       
  return  ret;
    }
}


struct  tm  * DayAdd( int  dayNUm)  //dayNum  单位:天  0 代表获取当前时间,整整数代表获取以后的时间,负数代表向前的时间
{
   
  time_t  tt,timetmp;
   
  struct  tm  * p =  NULL ;
   
  time (&tt);
   
  if  (tt == - 1 ) {
       
  perror ( "time" );
       
  return  p;
    }
    timetmp =
  24 * 60 * 60 *dayNUm;
   
  if  (dayNUm >  0 ) {
        tt = tt + timetmp;
    }
else  if  (dayNUm <  0 )
    {
        tt = tt + timetmp;
    }
   
    p =
  localtime (&tt);
   
  //    strcpy(p, localtime(&tt));
   
  if  (p ==  NULL ) {
       
  return  p;
    }
   
  return  p;
}


struct  tm  * MonthAdd( int  monthNum, struct  tm  * inTm)  //inTm null 表示当前时间
{

   
  time_t  tt;
   
  struct  tm  * tmTmp;
   
  int  yearCount =  0 ;
   
   
  if  (inTm ==  NULL ) {  // 判断时候获取当前时间戳
       
  time (&tt);
        tmTmp =
  localtime (&tt);
    }
else
    {
        tmTmp = inTm;
    }
   
   
  if  (monthNum <  0 ) {  // 负数代表
       
  while  ( 1 ) {
           
  if  ( abs (monthNum) >=  12 ) {
                monthNum+=
12 ;
                yearCount--;
               
  continue ;
            }
else {
               
  break ;
            }
        }
       
  if  ( abs (monthNum) >= (tmTmp-> tm_mon + 1 )) {  // 判断传入的月数是否大于当前的月份,所以 tmTmp->tm_mon+1, 因为 tm_mon 0 开始算
            tmTmp->
tm_mon  =  12  - ( abs (monthNum) - (tmTmp-> tm_mon ));
            yearCount--;
//            if (monthNum + (tmTmp->tm_mon+1) <= 0) { // 判断是否向前数一年,如果向前一年必须 year 减一
//                tmTmp->tm_mon = 12 - (abs(monthNum) - (tmTmp->tm_mon));
//                yearCount--;
//            }else
//            {
//                tmTmp->tm_mon += monthNum+1;
//            }
           
        }
else {
            tmTmp->
tm_mon  += monthNum;
        }
    }
else {
       
  while  ( 1 ) {  // 判断传入的参数是否大于 12 ,把大于 12 的数换算成年, 12 个月为一年
           
  if  (monthNum >=  12 ) {
                monthNum -=
  12 ;
                yearCount++;
               
  continue ;
            }
else {
               
  break ;
            }
        }
       
  if  (monthNum + (tmTmp-> tm_mon + 1 ) >  12 ) {  // 判断是否需要跨越当前年,如果跨越当前年则年计数器加 1
            tmTmp->
tm_mon  =  abs ((monthNum + (tmTmp-> tm_mon ))- 12 );
            yearCount++;
        }
else {
            tmTmp->
tm_mon  += monthNum;
        }
    }
    tmTmp->
tm_year  += yearCount;
   
   
  return  tmTmp;
}

struct  tm  * LastDayOfMonth( int  monthNum, struct  tm  * inTm)  //inTm null 表示当前时间
{
   
  int  month12[ 12 ] = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 };
   
  time_t  tt;
   
  struct  tm  * tmTmp;
   
  int  yearCount =  0 ;
   
   
  if  (inTm ==  NULL ) {  // 判断时候获取当前时间戳
       
  time (&tt);
        tmTmp =
  localtime (&tt);
    }
else
    {
        tmTmp = inTm;
    }
   
   
  if  (monthNum <  0 ) {  // 负数代表
       
  while  ( 1 ) {
           
  if  ( abs (monthNum) >=  12 ) {
                monthNum+=
12 ;
                yearCount--;
               
  continue ;
            }
else {
               
  break ;
            }
        }
       
  if  ( abs (monthNum) >= (tmTmp-> tm_mon + 1 )) {  // 判断传入的月数是否大于当前的月份,所以 tmTmp->tm_mon+1, 因为 tm_mon 0 开始算
            tmTmp->
tm_mon  =  12  - ( abs (monthNum) - (tmTmp-> tm_mon ));
            yearCount--;
           
  //            if (monthNum + (tmTmp->tm_mon+1) <= 0) { // 判断是否向前数一年,如果向前一年必须 year 减一
           
  //                tmTmp->tm_mon = 12 - (abs(monthNum) - (tmTmp->tm_mon));
           
  //                yearCount--;
           
  //            }else
           
  //            {
           
  //                tmTmp->tm_mon += monthNum+1;
           
  //            }
           
        }
else {
            tmTmp->
tm_mon  += monthNum;
        }
    }
else {
       
  while  ( 1 ) {  // 判断传入的参数是否大于 12 ,把大于 12 的数换算成年, 12 个月为一年
           
  if  (monthNum >=  12 ) {
                monthNum -=
  12 ;
                yearCount++;
               
  continue ;
            }
else {
               
  break ;
            }
        }
       
  if  (monthNum + (tmTmp-> tm_mon + 1 ) >  12 ) {  // 判断是否需要跨越当前年,如果跨越当前年则年计数器加 1
            tmTmp->
tm_mon  =  abs ((monthNum + (tmTmp-> tm_mon ))- 12 );
            yearCount++;
        }
else {
            tmTmp->
tm_mon  += monthNum;
        }
    }
    tmTmp->
tm_year  += yearCount;
   
   
  if ( LeapYear (tmTmp-> tm_year + 1900 ) ==  1 )  // 不是闰年
    {
        tmTmp->
tm_mday  = month12[tmTmp-> tm_mon ];
    }
else
    {
       
  if  (tmTmp-> tm_mon  ==  1 ) {
            tmTmp->
tm_mday  = month12[tmTmp-> tm_mon ]+ 1 ;
        }
else {
            tmTmp->
tm_mday  = month12[tmTmp-> tm_mon ];
        }
    }
   
   
  return  tmTmp;
}



来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28572479/viewspace-2138949/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/28572479/viewspace-2138949/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值