时间差計算

2019-12-12 18:21发布

条件如下
//输入开始 年 月 日 时 分 秒,
//输入结束 年 月 日 时 分 秒
// 计算两个时间的时间差,结果转成秒数
用 C语言,方便提供例程吗
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
18条回答
jxn98310
2019-12-14 05:37
本帖最后由 jxn98310 于 2019-3-12 18:38 编辑

转成UTC时间后,做减法
/*
** ===================================================================
**     Method      :  m2m_time_ToUtc (component ENV)
**
**     Description :
**         This method converts a time in m2mTime_t to UTC time.
**     Parameters  : const m2mTimePtr_t fptTime:
**     Returns     : UINT32: UTC time in seconds
** ===================================================================
*/
UINT32 m2m_time_ToUtc(const m2mTimePtr_t fptTime)
{
    m2mTime_t tm;

    (void)memcpy(&tm, fptTime, sizeof(m2mTime_t));

    if (0 >= (INT16) (tm.Month -= 2))                          /* 1..12 -> 11,12,1..10 */
    {
        tm.Month += 12;                                        /* Puts Feb last since it has leap day */
        tm.Year -= 1;
    }

    return (((
                 (UINT32) (tm.Year / 4 - tm.Year / 100U + tm.Year / 400 + 367 * tm.Month / 12 + tm.Day) +
                 (UINT32)tm.Year * 365 - 719499
             ) * 24 + tm.Hour                                  /* now have hours */
            ) * 60 + tm.Minute                                 /* now have minutes */
           ) * 60 + tm.Second - tm.TimeZone * 3600;            /* finally seconds */

}

一周热门 更多>