If you only need to check if the two lines have the same date or not, and if it is guaranteed that the lines are in the same format, then there is no need to convert them to a date. You just need to compare the substrings after the first space character. If they are the same, then the dates are the same. Here is a sample code:
using namespace std;
string getCurrentDate()
{
const char* months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
SYSTEMTIME st;
GetSystemTime(&st);
ostringstream ss;
ss<<st.wHour<<":"<<st.wMinute<<
" "<<months[st.wMonth-1]<<
" "<<st.wDay<<", "<<st.wYear%1000;
return ss.str();
}
string getDateString(const string& s)
{
string date;
size_t indx = s.find_first_of(' ');
if(indx != string::npos)
{
date = s.substr(indx + 1);
}
return date;
}
bool isCurrentDate(const string& s1)
{
string d1 = getDateString(s1);
string d2 = getDateString(getCurrentDate());
return ! d1.empty() && ! d2.empty() && d1 == d2;
}
int main( void )
{
bool s = isCurrentDate("21:5 Jan 23, 11");
bool s1 = isCurrentDate("21:5 Jan 25, 11");
return 0;
}
source
share