• C语言实现时间差、星期、天数算日期(转)


    原文地址:http://blog.sina.com.cn/s/blog_7c59079701011a9j.html

    最近因为一个项目需要,默认一个时间值,即当天的前一天日期,顺手就将其他的几个功能一并实现了,主要为

    1、两个日期之间的时间差

    2、某个日期之后,绝对值差值之后的某个日期

    3、某年日期,算出星期几

    下面见源码:

    #ifndef CALCULATE_H

    #define CALCULATE_H

    typedef struct

    {

             int year;

             int month;

             int day;

    }date;

    int Leap_Year(int year);

    int Calculate(date a,date b);

    date Date_String_To_Format(char *string_date);

    int Distance_Date(char *string_begin, char *string_end);

    int Week(char *string_date);

    int Judge(date d1);

    date Now_to_Later(date now, int gap);

    date Now_To_Fronter(date now, int gap);

    #endif

    #include<stdio.h>

    #include<stdlib.h>

    #include <string.h>

    #include "calculate.h"

    int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};

      

    功能: 将年转化为天

    参数:

    返回:

    sum---转化的天数

    修改:

    备注:

    fast_sky@sina.com

     int Year_To_Day(int year_begin, int year_end)

    {

             int sum = 0;

             int number = year_begin;

             while(number != year_end)

             {

                       sum = sum + 365 + Leap_Year(year_begin);

                       number++;

             }

             return sum;

    }

    功能:将月转化为天数

    参数:

    返回:

    -----转化完成的天数

    修改:

    备注:

    fast_sky@sina.com

     int Month_To_Day(date now)

    {

             int i, day = 0;

             for (i = 0; i < now.month - 1; ++i)

             {

                       day = day + month[i];

             }

             return (now.month >= 3) ? day + now.day + Leap_Year(now.year) :  day + now.day;

    }

    功能:计算两个日期之间的差值

    参数:

    begin---起始日期

    end----结束日期

    返回:

    ---算出的时间差值

    修改:

    备注:

    fast_sky@sina.com

    且begin <= end

     int Calculate(date begin,date end)

    {

             return Year_To_Day(begin.year, end.year) - Month_To_Day(begin) + Month_To_Day(end);

    }

    功能:是否为闰年

    参数:

    返回:

    1----成功

    0--失败

    修改:

    备注:

    fast_sky@sina.com

     int Leap_Year(int year)

    {

             return  ((year % 400 == 0) || ((year % 4 == 0)&& (year 0 != 0))) ? 1 : 0;

    }

      

    功能:字符日期转化为结构体格式

    参数:

    string_date----传入日前格式(20110101)只取前八位,后面不处理

    返回:

    ---转化完成的格式

    修改:

    备注:

    fast_sky@sina.com

     date Date_String_To_Format(char *string_date)

    {

             char year[5], month[3], day[3];

             date d1;

             memset(year, 0x0, sizeof(year));

             memset(month, 0x0, sizeof(month));

             memset(day, 0x0, sizeof(day));

             memcpy(year, string_date, 4);

             memcpy(month, string_date+4, 2);

             memcpy(day, string_date+6, 2);

             d1.year = atoi(year);

             d1.month = atoi(month);

             d1.day = atoi(day);

             return d1;

    }

    功能:计算两者之间的差值

    参数:

    返回:

    sum---计算所得两个日期之间的差值

    修改:

    备注:

    fast_sky@sina.com

    int Distance_Date(char *string_begin, char *string_end)

    {

             date d1,d2;

             int    sum;

             d1=Date_String_To_Format(string_begin);

             d2=Date_String_To_Format(string_end);

             if(Judge(d1)&&Judge(d2))

             {

                       if(d1.year<=d2.year)

                       {

                                sum=Calculate(d1,d2);

                       }

                       else

                       {

                                sum=Calculate(d2,d1);

                       }

             }

             return sum;

    }

    功能:某年的星期几

    参数:

    返回:

    1--7----传出的星期数

    修改:

    备注:

    fast_sky@sina.com

    蔡勒公式

     int Week(char *string_date)

    {

             date d1;

             int  century = 0;

             int year = 0;

             int weekday = 0;

             int month = 0;

             char ch;

             d1=Date_String_To_Format(string_date);

             if (Judge(d1))

             {

                       if (d1.month < 3)

                       {

                                month = d1.month + 12;

                                d1.year--;

                       }

                       else

                                month = d1.month;

                       century = d1.year/100;

                       year = d1.year % 100;

                       weekday = year + (year/4)+(century/4)-(2*century)+(26*(month + 1)/10) + d1.day - 1;

                       weekday = (weekday < 0) ? weekday + 7: weekday;

             }       

             return (weekday % 7 == 0) ? 7 : weekday % 7;

    }

    功能:从现在日期往后计算多少天之后的日期

    参数:

    返回:

    ---多少天之后的日期

    修改:

    备注:

    fast_sky@sina.com

     date Now_to_Later(date now, int gap)

    {

             date d1;

             int sum, sum_bak;

             int temp;

             d1 = now;

             sum = gap;

            

             if(Judge(d1)&&(sum>0) && (sum < 3649270))

             {

                       while(sum>365)

                       {

                                if(d1.month>=3)

                                {

                                         d1.year++;

                                         sum=sum-365-Leap_Year(d1.year);

                                }

                                else

                                {

                                         sum=sum-365-Leap_Year(d1.year);

                                         d1.year++;

                                }

                       }

                       while(sum > 0)

                       {

                                if (d1.month != 2)

                                {

                                         temp = month[d1.month - 1] -d1.day + 1;

                                }

                                else

                                {

                                         temp = month[d1.month - 1] +Leap_Year(d1.year)- d1.day + 1;

                                }

                                sum_bak = sum;

                                sum = sum - temp;

                                if (sum  >= 0)

                                {

                                         d1.month++;

                                         d1.day = 1;

                                         if (d1.month > 12)

                                         {

                                                   d1.month = 1;

                                                   d1.year++;

                                         }

                                         sum_bak = sum;

                                }

                       }

                       d1.day += sum_bak;

             }

             return d1;

    }

    功能:从当前日期往前计算多少天前的日期

    参数:

    返回:

    ------计算完成的日期

    修改:

    备注:

    fast_sky@sina.com

     date Now_To_Fronter(date now, int gap)

    {

             date d1;

             int sum, sum_bak;

             int temp;

             d1 = now;

             sum = gap;

            

             if(Judge(d1)&&(sum < 0) && (sum > -3649270))

             {

                       while(sum < -365)

                       {

                                if(d1.month>=3)

                                {

                                         sum=sum+365+Leap_Year(d1.year);

                                         d1.year--;

                                }

                                else

                                {

                                         d1.year--;

                                         sum=sum + 365 + Leap_Year(d1.year);

                                }

                       }

                       sum_bak = sum;

                       while(sum < 0)

                       {

                                temp =-d1.day;

                                sum = sum - temp;

                                if (sum  <= 0)

                                {

                                         d1.month--;

                                         if (d1.month < 1)

                                         {

                                                   d1.month = 12;

                                                   d1.year--;

                                         }

                                         if (d1.month == 2)

                                                   d1.day  = month[d1.month - 1] + Leap_Year(d1.year);

                                         else

                                                   d1.day = month[d1.month - 1];

                                         sum_bak = -sum;

                                }

                       }

                       if (sum_bak < 0)

                       {

                                d1.day = d1.day + sum_bak;

                       }else

                       {

                                if (d1.month == 2)

                                         d1.day = month[d1.month - 1] + Leap_Year(d1.year) - sum_bak;

                                else

                                         d1.day = month[d1.month - 1] - sum_bak;

                       }

             }

             return d1;

    }

    功能:判断输入日期是否合法

    参数:

    返回:

    1----成功

    0--失败

    修改:

    备注:

    fast_sky@sina.com

     int udge(date d1)

    {

             return     ((d1.year > 0 && d1.year <= 9999) &&

                                (d1.month > 0 && d1.month <= 12) &&

             (d1.day > 0 &&  (

             ((d1.month == 2) && (d1.day < month[d1.month - 1] + Leap_Year(d1.year)))||

             ((d1.month != 2) && (d1.day < month[d1.month - 1]))

                                                   ))) ? 1 : 0;

    }

        

  • 相关阅读:
    1.1 Recruitment 1.1.4 Sample Test(II)
    1.1 Recruitment 1.1.4 Sample Test(I)
    微信登录接口
    谷歌浏览器保留页面跳转前的请求
    积分墙项目接口文档
    @SneakyThrows
    security中使用单元测试
    Prometheus监控系统
    大数据-shell-脚本入门-开头格式、运行方式、多命令处理
    大数据-shell-概述
  • 原文地址:https://www.cnblogs.com/15157737693zsp/p/4847548.html
Copyright © 2020-2023  润新知