From 9e0c4404fc605c6ea6be5351a1715d2a7cda048d Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Mon, 2 Nov 2015 08:35:58 -0500 Subject: [PATCH] core/date: Add date_set() to directly set a date structure Signed-off-by: Anna Schumaker --- core/date.c | 17 +++++++++++------ core/tags/track.cpp | 4 +--- include/core/date.h | 3 +++ tests/core/date.c | 15 +++++++++++---- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/core/date.c b/core/date.c index 3dc54f07..289983e9 100644 --- a/core/date.c +++ b/core/date.c @@ -6,16 +6,21 @@ #include +void date_set(struct date *date, unsigned int year, + unsigned int month, unsigned int day) +{ + if (date) { + date->d_year = year; + date->d_month = month; + date->d_day = day; + } +} + void date_today(struct date *date) { time_t rawtime = time(NULL); struct tm *now = localtime(&rawtime); - - if (date) { - date->d_year = now->tm_year + 1900; - date->d_month = now->tm_mon + 1; - date->d_day = now->tm_mday; - } + date_set(date, now->tm_year + 1900, now->tm_mon + 1, now->tm_mday); } void date_read(struct file *f, struct date *date) diff --git a/core/tags/track.cpp b/core/tags/track.cpp index 490ddcb4..7ab43aa3 100644 --- a/core/tags/track.cpp +++ b/core/tags/track.cpp @@ -24,9 +24,7 @@ Track :: Track(Album *album, Artist *artist, Genre *genre, Library *library, _album(album), _artist(artist), _genre(genre), _library(library), _count(0), _length(length), _track(track), _path(filepath) { - _date.d_day = 0; - _date.d_month = 0; - _date.d_year = 0; + date_set(&_date, 0, 0, 0); filter :: add(this->name(), index()); filter :: add(_artist->name(), index()); filter :: add(_album->name(), index()); diff --git a/include/core/date.h b/include/core/date.h index 90879527..6d7496e7 100644 --- a/include/core/date.h +++ b/include/core/date.h @@ -14,6 +14,9 @@ struct date { }; +/* Set the date structure. */ +void date_set(struct date *, unsigned int, unsigned int, unsigned int); + /* Set the provided date structure to today's date. */ void date_today(struct date *); diff --git a/tests/core/date.c b/tests/core/date.c index 801853da..4697c7bb 100644 --- a/tests/core/date.c +++ b/tests/core/date.c @@ -21,19 +21,26 @@ void test_date() gchar *str; date_today(NULL); + date_set(NULL, 0, 0, 0); setlocale(LC_TIME, "C"); + file_open(&f, OPEN_WRITE); + + date_set(&date, 1988, 6, 17); + test_equal(date.d_year, 1988); + test_equal(date.d_month, 6); + test_equal(date.d_day, 17); + date_write(&f, &date); + file_writef(&f, "\n"); date_today(&date); test_equal(date.d_year, today->tm_year + 1900); test_equal(date.d_month, today->tm_mon + 1); test_equal(date.d_day, today->tm_mday); - - file_open(&f, OPEN_WRITE); - file_writef(&f, "%u %u %u\n", 1988, 6, 17); date_write(&f, &date); - file_close(&f); + file_close(&f); file_open(&f, OPEN_READ); + date_read(&f, &date); test_equal(date.d_year, 1988); test_equal(date.d_month, 6);