c++时间间隔运算



#include <string>
#include <cstring>
#include <iostream>
#include "time.h"
#include <ctime>
using namespace std;
//strTime格式为 20160212,size表示需减去的天数 20160212-1=20160211
string reduceTime(const std::string& strTime, int size)
{
tm tm_;
time_t t_;
std::string formatTime = strTime + " 00:00:00";
strptime(formatTime.c_str(), "%Y%m%d %H:%M:%S", &tm_);
t_  = mktime(&tm_);
t_ -= 86400 * size;
tm_ = *localtime(&t_);
char buf[128] = {0};
strftime(buf, 64, "%Y%m%d", &tm_);
return string(buf, strlen(buf));
}
//strTime格式为 20160212,size表示需加的天数 20160212+1=20160213
string addTime(const std::string& strTime, int size)
{
tm tm_;
time_t t_;
std::string formatTime = strTime + " 00:00:00";
strptime(formatTime.c_str(), "%Y%m%d %H:%M:%S", &tm_);
t_  = mktime(&tm_);
t_ += 86400 * size;
tm_ = *localtime(&t_);
char buf[128] = {0};
strftime(buf, 64, "%Y%m%d", &tm_);
return string(buf, strlen(buf));
}
/*
@settleType = 2,表示加一天
@settleType = 3,表示加到下个星期一
@settleType = 4,表示加到下个月1号
*/
std::string calcuteSettleTime(const std::string& payTime, int settleType)
{
//日结
if (settleType == 2)
{
return addTime(payTime, 1);
}
tm tstruct;
time_t t_;
std::string formatTime = payTime + " 00:00:00";
strptime(formatTime.c_str(), "%Y%m%d %H:%M:%S", &tstruct);
t_  = mktime(&tstruct);
tstruct = *localtime(&t_);
//周结
if (settleType == 3)
{
if (tstruct.tm_wday == 0)
{
tstruct.tm_wday = 7;
}
return addTime(payTime, 8 - tstruct.tm_wday);
}
//月结
if (settleType == 4)
{
tstruct.tm_mon = tstruct.tm_mon + 1;
if (tstruct.tm_mon > 11)
{
tstruct.tm_mon = 0;
tstruct.tm_year += 1;
}
tstruct.tm_mday = 1;
char buf[128] = {0};
strftime(buf, 64, "%Y%m%d", &tstruct);
return std::string(buf, strlen(buf));
}
//实时
return payTime;
}
std::string formatTime(const std::string& strTime)
{
tm tm_;
strptime(strTime.c_str(), "%Y-%m-%d %H:%M:%S", &tm_);
char buf[64] = {0};
strftime(buf, 64, "%Y%m%d", &tm_);
return std::string(buf, strlen(buf));
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值