通过unix时间戳获得两个时间相差多少自然日

由两个unix时间戳得到之间相差自然日数

这两个函数模块是我在参加一个新项目的编写时偶然发现并且特别依赖的部分,为实现当时手上累计登陆完成任务有很大的帮助。该模块用C++编写,在linux环境上编译测试通过

#include <time.h>
int GetTimeZone()
{
    time_t tm1, tm2;
    tm1 = time( NULL );
    struct tm stTm;
    tm2 = mktime( gmtime_r( &tm1, &stTm ) );
    return static_cast<int>( tm1 - tm2  );
}

int GetDiffDay( unsigned int  nTime1,  unsigned int  nTime2 )
{
    static int nDaySec = 3600 * 24;
    static int nZone = GetTimeZone();

    int nTempTimeSec = 0;
    nTempTimeSec = (nZone >= 0)?nZone:(nZone + nDaySec);

    int nDay1 = ( nTime1 + nTempTimeSec ) /  nDaySec;
    int nDay2 = ( nTime2 + nTempTimeSec ) / nDaySec;

    return nDay2 - nDay1;
}

下次有空把整个项目用到的时间处理的模块整理一下

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值