计算时间差

time_t tr_timediff(const tm * ptm1, const tm * ptm2)
{
    uint16_t year;
    uint32_t days = 0;
    uint8_t n;
    uint64_t time = 0;
    tm * pendtm = NULL;

    uint8_t nptm1monthday = 0;

    assert(ptm1 != NULL);

    if (NULL == ptm2)
    {
        //计算本地时间
        tr_time(pendtm);assert(pendtm != NULL);
    }
    else
    {
        pendtm = ptm2;
    }
    
    if

    if (pendtm->tm_year != ptm1->tm_year)
    {
        //不是同一年
        year = pendtm->tm_year - 1;
        while (year > ptm1->tm_year)
        {
            //计算整年天数
            if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
            {
                days += 366;
            }
            else
            {
                days += 365;
            }
            year -= 1;
        }

        for (n = 12; n >= ptm1->tm_month; --n)
        {
            //计算开始时间月数
            switch (n)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                {
                    if (n == ptm1->tm_month)
                    {
                        nptm1monthday = 31;
                    }
                    else
                    {
                        days += 31;
                    }

                }
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                {
                    if (n == ptm1->tm_month)
                    {
                        nptm1monthday = 30;
                    }
                    else
                    {
                        days += 30;
                    }

                }
                    break;
                case 2:
                {
                    if (n == ptm1->tm_month)
                    {
                        if (((ptm1->tm_year) % 4 == 0 && (ptm1->tm_year) % 100
                                != 0) || (ptm1->tm_year) % 400 == 0)
                        {
                            nptm1monthday = 29;
                        }
                        else
                        {
                            nptm1monthday = 28;
                        }
                    }
                    else
                    {
                        if (((ptm1->tm_year) % 4 == 0 && (ptm1->tm_year) % 100
                                != 0) || (ptm1->tm_year) % 400 == 0)
                        {
                            days += 29;
                        }
                        else
                        {
                            days += 28;
                        }
                    }

                }
            }
        }

        for (n = 1; n < pendtm->tm_month; ++n)
        {
            //计算结束时间月数
            switch (n)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                {

                    days += 31;
                }
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                {
                    days += 30;
                }
                    break;
                case 2:
                {
                    if (((pendtm->tm_year) % 4 == 0 && (pendtm->tm_year) % 100 != 0)
                            || (pendtm->tm_year) % 400 == 0)
                    {
                        days += 29;
                    }
                    else
                    {
                        days += 28;
                    }
                }
            }
        }

        days += pendtm->tm_day - 1;//累加结束时间天数
        days += nptm1monthday - ptm1->tm_day;//累加开始时间天数

        time = (days * 24 * 60 * 60) + (pendtm->tm_hour * 3600 + pendtm->tm_minute
                * 60 + pendtm->tm_second) + ((24 - ptm1->tm_hour - 1) * 3600
                + ((60 - ptm1->tm_minute - 1) * 60) + 60 - ptm1->tm_second);
    }
    else
    {
        //开始时间和结束时间是同一年
        for (n = ptm1->tm_month; n < pendtm->tm_month; ++n)
        {
            //计算开始和结束月份
            switch (n)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                {
                    if (n == ptm1->tm_month)
                    {
                        nptm1monthday = 31;
                    }
                    else
                    {
                        days += 31;
                    }
                }
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                {
                    if (n == ptm1->tm_month)
                    {
                        nptm1monthday = 30;
                    }
                    else
                    {
                        days += 30;
                    }
                }
                    break;
                case 2:
                {
                    if (n == ptm1->tm_month)
                    {
                        if (((ptm1->tm_year) % 4 == 0 && (ptm1->tm_year) % 100
                                != 0) || (ptm1->tm_year) % 400 == 0)
                        {
                            nptm1monthday = 29;
                        }
                        else
                        {
                            nptm1monthday = 28;
                        }
                    }
                    else
                    {
                        if (((ptm1->tm_year) % 4 == 0 && (ptm1->tm_year) % 100
                                != 0) || (ptm1->tm_year) % 400 == 0)
                        {
                            days += 29;
                        }
                        else
                        {
                            days += 28;
                        }
                    }
                }
            }
        }

        if (ptm1->tm_month != pendtm->tm_month)
        {
            //如果开始时间和结束时间不是同一月
            days += pendtm->tm_day;//累加结束时间天数
            days += nptm1monthday - ptm1->tm_day;//累加开始时间天数
        }
        else
        {
            //同年同月中
            days += pendtm->tm_day - ptm1->tm_day;
        }

        time = (days * 24 * 60 * 60) + ((pendtm->tm_hour - ptm1->tm_hour) * 3600
                + (pendtm->tm_minute - ptm1->tm_minute) * 60 + pendtm->tm_second
                - ptm1->tm_second);
    }

    return time;
}

你可能感兴趣的:(计算时间差)