Track: Add a function for comparing two dates

I don't want to expose the date structure outside of the track class, so
I'll provide a comparison function instead.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2014-11-28 10:21:35 -05:00
parent de1b5fcef3
commit b5a529795d
3 changed files with 34 additions and 9 deletions

View File

@ -99,3 +99,14 @@ void Track :: played()
//tagdb :: commit();
}
int Track :: compare_date(const Track *rhs)
{
int ret = _date.year - rhs->_date.year;
if (ret == 0) {
ret = _date.month - rhs->_date.month;
if (ret == 0)
ret = _date.day - rhs->_date.day;
}
return ret;
}

View File

@ -117,6 +117,16 @@ public:
/** Increment Track::_count and set Track::_date to today's date. */
void played();
/**
* Compare two tracks based on last played date.
*
* @param rhs The other track to compare.
* @return < 0: lhs < rhs.
* @return 0: lhs == rhs.
* @return > 0: lhs > rhs;
*/
int compare_date(const Track *);
/**
* Read track data from file

View File

@ -70,14 +70,14 @@ static void test_track_tag_functional()
time_t rawtime = time(NULL);
struct tm *now = localtime(&rawtime);
std::stringstream ss;
Track track;
Track track1, track2;
track = Track(album, artist, genre, library,
track1 = Track(album, artist, genre, library,
"/home/Zelda/Music/Hyrule Symphony/6 - Kakariko Village.mp3",
"Kakariko Village", 186, 6);
track.played();
test_equal(track.length_str(), (std::string)"3:06");
test_equal(track.count(), (unsigned)1);
track1.played();
test_equal(track1.length_str(), (std::string)"3:06");
test_equal(track1.count(), (unsigned)1);
std::setlocale(LC_TIME, "C");
@ -87,7 +87,7 @@ static void test_track_tag_functional()
if (now->tm_mday < 10)
ss << "0";
ss << now->tm_mday << "/" << (now->tm_year % 100);
test_equal(track.date(), ss.str());
test_equal(track1.date(), ss.str());
std::setlocale(LC_TIME, "en_US");
@ -98,14 +98,18 @@ static void test_track_tag_functional()
if (now->tm_mday < 10)
ss << "0";
ss << now->tm_mday << "/" << now->tm_year + 1900;
test_equal(track.date(), ss.str());
test_equal(track1.date(), ss.str());
/* Not an actual track on the album, I just needed something < 1 min. */
track = Track(album, artist, genre, library,
track2 = Track(album, artist, genre, library,
"/home/Zelda/Music/Hyrule Symphony/0 - intro.mp3",
"Intro", 56, 0);
test_equal(track.length_str(), (std::string)"0:56");
test_equal(track2.length_str(), (std::string)"0:56");
test_equal(track1.compare_date(&track1), 0);
test_equal(track1.compare_date(&track2), 2014);
test_equal(track2.compare_date(&track1), -2014);
}
int main(int argc, char **argv)