//关于时间的偏移可以在这上面扩展
#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;
}
日期的偏移获取情况
最新推荐文章于 2023-08-22 19:02:41 发布