目次
時間比較機能:
入力は文字列 2023-7-28 で、文字列は年、月、日の情報に分解されます。
システム時刻 2023-7-24 を取得し、入力時刻とシステム時刻を比較し、比較結果を出力します。
1コード
#include <ctime>
#include<iostream>
#include<vector>
using namespace std;
void StrSplit(string str_in, const char split_c, vector<string>& str_results)
{
if (str_in == ""){
std::cout << "error : str is null" << std::endl;
return;
}
size_t pos = str_in.find(split_c);
while (pos != str_in.npos)
{
string temp = str_in.substr(0, pos);
str_results.push_back(temp);
str_in = str_in.substr(pos + 1, str_in.size());
pos = str_in.find(split_c);
}
}
string TimeCompare(string databaseTime){
vector<string> tmp_str;
StrSplit(databaseTime + "-", '-', tmp_str);
unsigned int dayin, daysys, year=0,mon=0,day=0;
if(tmp_str.size() == 3){
year = stoi(tmp_str[0]);
mon = stoi(tmp_str[1]);
day = stoi(tmp_str[2]);
}else{
std::cout<< "Time format error !!!"<<std::endl;
return "error";
}
dayin = year*10000+mon*100+day;
time_t now = time(NULL);
tm *tm_t = localtime(&now);
daysys =(tm_t->tm_year + 1900)*10000 + (tm_t->tm_mon+1)*100 + tm_t->tm_mday;
cout<<"SystemTime: "<<tm_t->tm_year + 1900<<"-"<<tm_t->tm_mon+1<<"-"<<tm_t->tm_mday<<endl;
if(dayin > daysys) {
return "big";
}else if(dayin < daysys) {
return "small";
}else{
return "equal";
}
}
int main()
{
cout<<"Input1: "<<"2023-7-28"<<endl;
cout<<TimeCompare("2023-7-28")<<endl;
cout<<"Input2: "<<"2022-6-3"<<endl;
cout<<TimeCompare("2022-6-3")<<endl;
}
2 走行結果