日期的偏移获取情况

//关于时间的偏移可以在这上面扩展
#include <stdio.h>
#include <time.h>
#include <iostream>
#include <boost/date_time/gregorian/gregorian.hpp>   
using namespace std;

//boost 库
std::string GetDayNumOffsetStr(const int offset_day)
{
    return ConvertToString(GetDayNumOffsetInt(offset_day));
}

int GetDayNumOffsetInt(const int offset_day)
{
    std::string strTime = boost::gregorian::to_iso_string(boost::gregorian::day_clock::local_day() + boost::gregorian::days(offset_day));
    return atoi(strTime.c_str());
}


//标准库
int GetDayNumOffset(const int offset_day)
{
    time_t tnow;

    struct tm *tmnow = NULL;
    struct tm *tmDayoffset = NULL;

    time(&tnow);
    tmnow = localtime(&tnow);//当前时间
    
    tnow += 24*60*60*offset_day;
    tmDayoffset = localtime(&tnow);
    
    /*
            tm24h->tm_year+1900,tm24h->tm_mon,tm24h->tm_mday,
            tm24h->tm_hour,tm24h->tm_min,tm24h->tm_sec);
    */
    return ((tmDayoffset->tm_year+1900)*10000 + tmDayoffset->tm_mon*100 + tmDayoffset->tm_mday);
}

int main(int argc, char **argv)
{
    cout <<"current:"<<GetDayNumOffset(0)<<endl;
    cout <<"-1:"<<GetDayNumOffset(-1)<<endl;
    cout <<"-2:"<<GetDayNumOffset(-2)<<endl;
    cout <<"+3:"<<GetDayNumOffset(3)<<endl;

    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值