Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare two char* like dates

Status
Not open for further replies.

bobisto

Programmer
Feb 13, 2002
111
MK
I have a function that accepts two variables CString
In those variable i recieve strings like "20030910" and "20051231" and I should compare them like dates.
What is the best way to do it? Currently my code is :
BOOL VerifyExpirationDate(CString sExpirationDateFile, CString sExpirationDateGiven)
{ //mora stringovite za datum da bidat 8 karakteri
if (sExpirationDateFile.GetLength() != 8) // YYYYMMDD
return FALSE;
if (sExpirationDateFile.GetLength() != 8) // YYYYMMDD
return FALSE;

COleDateTime tExpirationDateFile(
atoi(sExpirationDateFile.Left(4)),
atoi(sExpirationDateFile.Mid(4, 2)),
atoi(sExpirationDateFile.Mid(6, 2)), 0, 0, 0);

COleDateTime tExpirationDateGiven(
atoi(sExpirationDateGiven.Left(4)),
atoi(sExpirationDateGiven.Mid(4, 2)),
atoi(sExpirationDateGiven.Mid(6, 2)), 0, 0, 0);

if (tExpirationDateFile >= tExpirationDateGiven)
return TRUE;
else
return FALSE;
}
where sExpirationDateFile and sExpirationDateGiven are variables mentioned earlyer. I dont like it because for me is to complicated, to slow and eats too much memory
 
Since the dates are formated as "YYYYMMDD", you should just be able to do a simple string comparison and get the desired results.

Dennis
 
I've tried but not work like in VB.I can't find a rule how C++ compare strings
 
Well, as you use CString, and CString has a bunch of comparison operators, you can simply do a

Code:
CString sSomeDate("20011023"); 
CString sSomeOtherDate("20030412"); 

if (sSomeDate < sSomeOtherDate)
{
  .
  .
}


/Per

if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
 
I think you have to use strcmp function (part of Cstring library).
i'm i remember correctly the syntax:
strcmp(string1 , string2)
the function return 0 if equales , and 1 or -1 , if not equles.
hope it'll help u.
Asi
 
hey,

If all else fails, why don't you just type cast the strings as integers or doubles?? Its been a while since I've used C++, but I think it would look like this:

string date1
string date2

if (int(date1) < int(date2))
{
...
}

or

if (double(date1) < double(date2))
{
...
}

Good luck with it

Kevin
 
Hi,
Generraly when you deal with dates (another example is Currency) you should use COle classes.
In this case you should have your own class derived from COleDateTime and have implemented operators ==, <, > != etc... This is not trivial when the dates contain date (ymd), time h,m,s and miliseconds.
I post for you the CAppDate class which is working very well. You should take out or replace care about LogError() function that was required due to the specific requirements.
For your answer see the < and > operators.
//
// -obislavu- 1999-07-01
//

class CAppDate : public COleDateTime
{
public:
CAppDate() : COleDateTime() {}
CAppDate(LPCTSTR pszDate);
BOOL ParseDate(LPCTSTR pszDate);
BOOL IsValid() const { return GetStatus() == valid; }
BOOL IsEmpty() const;
void SetEmpty();
static CAppDate GetLowValueDate() { return m_dtLowValue; }
static CAppDate GetHighValueDate() { return m_dtHighValue; }
static void SetDefaultFormat(const CString& sFormat) { m_sDefaultFormat=sFormat; }
static const CString& GetDefaultFormat() { return m_sDefaultFormat; }
CString Format(LPCTSTR pszFormat=NULL) const;
CAppDate& operator=(LPCTSTR pszDate);
BOOL operator==(const CAppDate& dt) const;
BOOL operator!=(const CAppDate& dt) const;
BOOL operator>(const CAppDate& dt) const;
BOOL operator>=(const CAppDate& dt) const;
BOOL operator<(const CAppDate& dt) const;
BOOL operator<=(const CAppDate& dt) const;
private:
double DoubleFromDate(DATE dt) const;
static CString m_sDefaultFormat;
static CAppDate m_dtLowValue;
static CAppDate m_dtHighValue;
};



CString CAppDate::m_sDefaultFormat = &quot;%Y%m%d&quot;;
CAppDate CAppDate::m_dtLowValue = &quot;0100-01-01&quot;;
CAppDate CAppDate::m_dtHighValue = &quot;9999-12-31&quot;;

/////////////////////////////////////////////////////////////////////////////
// CAppDate

CAppDate::CAppDate(LPCTSTR pszDate) : COleDateTime()
{
*this = pszDate;
}

BOOL CAppDate::IsEmpty() const
{
return m_dt == 0.0;
}

void CAppDate::SetEmpty()
{
SetDate(0,0,0);
m_dt = 0.0;
// Calling SetDate function with parameters (0,0,0) will NOT set m_dt value to 0.
// It will only turn the m_status flag to invalid.
//
}

BOOL CAppDate::parseDate(LPCTSTR pszDate)
{
CString sDate = pszDate;

if ( sDate.FindOneOf(&quot; -/&quot;) == -1 && sDate.GetLength() == 8 )
{
sDate = sDate.Left(4) + &quot;-&quot; + sDate.Mid(4,2) + &quot;-&quot; + sDate.Right(2);
}
return ParseDateTime( sDate, VAR_DATEVALUEONLY);
}


CString CAppDate::Format(LPCTSTR pszFormat/*=NULL*/) const
{
if ( IsEmpty() )
return &quot;&quot;;
else
if ( pszFormat == NULL )
return COleDateTime::Format(m_sDefaultFormat);
else
return COleDateTime::Format(pszFormat);
}

CAppDate& CAppDate::eek:perator=(LPCTSTR pszDate)
{
BOOL bValid = ParseDate(pszDate);
CString sDate(pszDate);
sDate.TrimLeft();
sDate.TrimRight();
if ( !sDate.IsEmpty() && !bValid )
m_dt = 1.0;

return *this;
}

BOOL CAppDate::eek:perator==(const CAppDate& dt) const
{
if ( !IsEmpty() && !IsValid() )
{
CString msg;
msg.Format(LOG_IDS_INVALIDCONDITION, _T(&quot;IsEmpty() || IsValid()&quot;));
//LogError
}

return DoubleFromDate(m_dt) == DoubleFromDate(dt.m_dt);
}

BOOL CAppDate::eek:perator!=(const CAppDate& dt) const
{
if ( !IsEmpty() && !IsValid() )
{
CString msg;
msg.Format(LOG_IDS_INVALIDCONDITION, _T(&quot;IsEmpty() || IsValid()&quot;));
//LogError
}

return DoubleFromDate(m_dt) != DoubleFromDate(dt.m_dt);
}

BOOL CAppDate::eek:perator>(const CAppDate& dt) const
{
if ( !IsEmpty() && !IsValid() )
{
CString msg;
msg.Format(LOG_IDS_INVALIDCONDITION, _T(&quot;IsEmpty() || IsValid()&quot;));
//LogError
}

return DoubleFromDate(m_dt) > DoubleFromDate(dt.m_dt);
}

BOOL CAppDate::eek:perator>=(const CAppDate& dt) const
{
if ( !IsEmpty() && !IsValid() )
{
CString msg;
msg.Format(LOG_IDS_INVALIDCONDITION, _T(&quot;IsEmpty() || IsValid()&quot;));
//LogError
}

return DoubleFromDate(m_dt) >= DoubleFromDate(dt.m_dt);
}

BOOL CAppDate::eek:perator<(const CAppDate& dt) const
{
if ( !IsEmpty() && !IsValid() )
{
CString msg;
msg.Format(LOG_IDS_INVALIDCONDITION, _T(&quot;IsEmpty() || IsValid()&quot;));
//LogError
}

return DoubleFromDate(m_dt) < DoubleFromDate(dt.m_dt);
}

BOOL CAppDate::eek:perator<=(const CAppDate& dt) const
{
if ( !IsEmpty() && !IsValid() )
{
CString msg;
msg.Format(LOG_IDS_INVALIDCONDITION, _T(&quot;IsEmpty() || IsValid()&quot;));
//LogError
}

return DoubleFromDate(m_dt) <= DoubleFromDate(dt.m_dt);
}

double CAppDate::DoubleFromDate(DATE dt) const
{
// No problem if positive
if (dt >= 0)
return dt;

// If negative, must convert since negative dates not continuous
// (examples: -1.25 to -.75, -1.50 to -.50, -1.75 to -.25)
double temp = ceil(dt);
return temp - (dt - temp);
}

-obislavu-

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top