From b5a529795de1f634a1fce6912ab08c461655f59b Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Fri, 28 Nov 2014 10:21:35 -0500 Subject: [PATCH] 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 --- core/tags/track.cpp | 11 +++++++++++ include/core/tags/track.h | 10 ++++++++++ tests/core/tags/track.cpp | 22 +++++++++++++--------- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/core/tags/track.cpp b/core/tags/track.cpp index fce90994..b6c3b6f5 100644 --- a/core/tags/track.cpp +++ b/core/tags/track.cpp @@ -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; +} diff --git a/include/core/tags/track.h b/include/core/tags/track.h index 26bd8023..3079a9f1 100644 --- a/include/core/tags/track.h +++ b/include/core/tags/track.h @@ -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 diff --git a/tests/core/tags/track.cpp b/tests/core/tags/track.cpp index 5fc75d61..5e852809 100644 --- a/tests/core/tags/track.cpp +++ b/tests/core/tags/track.cpp @@ -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)