core/date: Add date_set() to directly set a date structure
Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
422f6a389f
commit
9e0c4404fc
17
core/date.c
17
core/date.c
|
@ -6,16 +6,21 @@
|
|||
#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)
|
||||
{
|
||||
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)
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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 *);
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue