core/date: Add date_today() function for finding today's date

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-10-27 19:13:08 -04:00
parent d230396cb2
commit cef39bc0fc
4 changed files with 34 additions and 10 deletions

18
core/date.c Normal file
View File

@ -0,0 +1,18 @@
/*
* Copyright 2015 (c) Anna Schumaker.
*/
#include <core/date.h>
#include <time.h>
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;
}
}

View File

@ -97,14 +97,8 @@ const std::string Track :: primary_key() const
void Track :: played()
{
time_t rawtime = time(NULL);
struct tm *now = localtime(&rawtime);
_count++;
_date.d_day = now->tm_mday;
_date.d_month = now->tm_mon + 1;
_date.d_year = now->tm_year + 1900;
date_today(&_date);
tags :: commit_track_db();
}

View File

@ -10,4 +10,8 @@ struct date {
unsigned int d_day;
};
/* Set the provided date structure to today's date. */
void date_today(struct date *);
#endif /* OCARINA_CORE_DATE_H */

View File

@ -1,20 +1,28 @@
/*
* Copyright 2015 (c) Anna Schumaker.
*/
#define _GNU_SOURCE
#include <core/date.h>
#include <tests/test.h>
#include <time.h>
void test_date()
{
time_t rawtime = time(NULL);
struct tm *today = localtime(&rawtime);
struct date date = {
.d_year = 0,
.d_month = 0,
.d_day = 0,
};
test_equal(date.d_year, 0);
test_equal(date.d_month, 0);
test_equal(date.d_day, 0);
date_today(NULL);
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);
}
DECLARE_UNIT_TESTS(