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));
}
(1) 测试日期类成员函数,在主函数中列出菜单选项,可以完成日期的加减比较等测试功能。 (2) 完善程序功能,在日期相加的菜单选项中增加日期加天数,结果为新日期;日期家月份,结果为新日期,要考虑闰年情况。 (3) 完善程序功能,在日期相减的菜单选项中增加日期减天数,结果为新日期;日期减月份,结果为新日期,要考虑闰年情况。 (4) 显示日期时增加显示星期及英文形式的月份的功能。 (5) 增加输入的甄别功能,即输入非法数据(如负数、日期超过31天、时间超过24小时等情况)的识别显示功能。 (1) 仿照日期类编写时间类CTime_t,可以完成时间的设置、运算、比较等功能。 (2) 增加时间的输入功能,既可以选择输入格式,可以输入hh:mm:ss格式的信息。 (3) 增加时间的输出格式,可以输出12小时的时间格式。 (4) 编写时间和日期的派生类CDati,完成日期与时间的联合设置、运算、比较等功能,要求该派生类可以完成:日期时间加天数或时间等于新的日期时间,日期时间减天数或等于新的日期时间,两个日期时间相减等于天数或时间等工作,在程序中考虑闰年等具体情况,并重载各种运算符。 (5) 增加输入的甄别功能,即输入非法数据,即输入非法数据(如负数、日期超过31天、时间超过24小时等情况)的识别显示功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值