ITPub博客

首页 > 应用开发 > IT综合 > C 时间格式化

C 时间格式化

原创 IT综合 作者:raysuen 时间:2017-05-12 10:10:15 0 删除 编辑
-----------------------------.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博客 ” ,链接:https://blog.itpub.net/28572479/viewspace-2138949/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: mysql 主从复制
下一篇: DG RAC - 单点搭建
请登录后发表评论 登录
全部评论
擅长oracle,Linux。精通shell,python。

注册时间:2016-08-23

  • 博文量
    145
  • 访问量
    227108