core/date: Add date_set() to directly set a date structure

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-11-02 08:35:58 -05:00
parent 422f6a389f
commit 9e0c4404fc
4 changed files with 26 additions and 13 deletions

View File

@ -6,16 +6,21 @@
#include <time.h> #include <time.h>
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) void date_today(struct date *date)
{ {
time_t rawtime = time(NULL); time_t rawtime = time(NULL);
struct tm *now = localtime(&rawtime); struct tm *now = localtime(&rawtime);
date_set(date, now->tm_year + 1900, now->tm_mon + 1, now->tm_mday);
if (date) {
date->d_year = now->tm_year + 1900;
date->d_month = now->tm_mon + 1;
date->d_day = now->tm_mday;
}
} }
void date_read(struct file *f, struct date *date) void date_read(struct file *f, struct date *date)

View File

@ -24,9 +24,7 @@ Track :: Track(Album *album, Artist *artist, Genre *genre, Library *library,
_album(album), _artist(artist), _genre(genre), _library(library), _album(album), _artist(artist), _genre(genre), _library(library),
_count(0), _length(length), _track(track), _path(filepath) _count(0), _length(length), _track(track), _path(filepath)
{ {
_date.d_day = 0; date_set(&_date, 0, 0, 0);
_date.d_month = 0;
_date.d_year = 0;
filter :: add(this->name(), index()); filter :: add(this->name(), index());
filter :: add(_artist->name(), index()); filter :: add(_artist->name(), index());
filter :: add(_album->name(), index()); filter :: add(_album->name(), index());

View File

@ -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. */ /* Set the provided date structure to today's date. */
void date_today(struct date *); void date_today(struct date *);

View File

@ -21,19 +21,26 @@ void test_date()
gchar *str; gchar *str;
date_today(NULL); date_today(NULL);
date_set(NULL, 0, 0, 0);
setlocale(LC_TIME, "C"); 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); date_today(&date);
test_equal(date.d_year, today->tm_year + 1900); test_equal(date.d_year, today->tm_year + 1900);
test_equal(date.d_month, today->tm_mon + 1); test_equal(date.d_month, today->tm_mon + 1);
test_equal(date.d_day, today->tm_mday); 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); date_write(&f, &date);
file_close(&f);
file_close(&f);
file_open(&f, OPEN_READ); file_open(&f, OPEN_READ);
date_read(&f, &date); date_read(&f, &date);
test_equal(date.d_year, 1988); test_equal(date.d_year, 1988);
test_equal(date.d_month, 6); test_equal(date.d_month, 6);