c++字符串时间格式转换为时间判断有效期#include <iostream>
#include <time.h>
using namespace std;
time_t str_to_time_t(const string& ATime, const string& AFormat = "%d-%d-%d %d:%d:%d")
{
struct tm tm_Temp;
time_t time_Ret;
try
{
int i = sscanf(ATime.c_str(), AFormat.c_str(),// "%d/%d/%d %d:%d:%d" ,
&(_year),
&(_mon),
&(_mday),
&(_hour),
&(_min),
&(_sec));
_year -= 1900;
_mon--;
//如果精确到秒就把下⾯四⾏注释掉;如果精确到天就把下⾯四⾏代码放开
//_hour = 0;
//_min = 0;
//_sec = 0;
/
/_isdst = 0;
time_Ret = mktime(&tm_Temp);
return time_Ret;
}
catch (...) {
trunc函数使用时间return0;
}
}
time_t NowTime()
{
time_t t_Now = time(0);
struct tm* tm_Now = localtime(&t_Now);
//如果精确到秒就把下⾯四⾏注释掉;如果精确到天就把下⾯四⾏代码放开
//tm_Now->tm_hour = 0;
//tm_Now->tm_min = 0;
//tm_Now->tm_sec = 0;
return  mktime(tm_Now);
}
bool IsValidTime(const time_t& AEndTime, const time_t& ANowTime)
{
return (AEndTime >= ANowTime);
}
int main()
{
string sEndTime = "2019-5-11 17:38:59";
time_t t_Now = NowTime();
time_t t_End = str_to_time_t(sEndTime);
if (IsValidTime(t_End, t_Now)) {
cout<< "有效⽇期" << endl;
}
else {
cout << "时间过期" << endl;
}
return0;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。