Saturday, 15 June 2013

c++ - Comparing two timestamp -



c++ - Comparing two timestamp -

i coded next function simple. seems not work when compare 2 ctime. don't know wrong, may help me please?

bool operator<(ctime second) { if (year<second.getyear()) homecoming true; if (year>second.getyear()) homecoming false; if (year<second.getmonth()) homecoming true; if (year>second.getmonth()) homecoming false; if (year<second.getday()) homecoming true; if (year>second.getday()) homecoming false; if (year<second.gethour()) homecoming true; if (year>second.gethour()) homecoming false; if (year<second.getmin()) homecoming true; if (year>second.getmin()) homecoming false; if (year<second.getsec()) homecoming true; if (year>second.getsec()) homecoming false; homecoming false; }

you comparing year fields.

instead of

if (year<second.getmonth()) homecoming true;

you need

if (this->getmonth()<second.getmonth()) homecoming true;

similarly other fields.

update

you can create code bit less verbose replacing lines:

if (year<second.getyear()) homecoming true; if (year>second.getyear()) homecoming false;

with

if (year != second.getyear()) homecoming (year < second.getyear());

similarly other fields.

c++

No comments:

Post a Comment