core/date: Add date_today() function for finding today's date
Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
d230396cb2
commit
cef39bc0fc
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -97,14 +97,8 @@ const std::string Track :: primary_key() const
|
||||||
|
|
||||||
void Track :: played()
|
void Track :: played()
|
||||||
{
|
{
|
||||||
time_t rawtime = time(NULL);
|
|
||||||
struct tm *now = localtime(&rawtime);
|
|
||||||
|
|
||||||
_count++;
|
_count++;
|
||||||
_date.d_day = now->tm_mday;
|
date_today(&_date);
|
||||||
_date.d_month = now->tm_mon + 1;
|
|
||||||
_date.d_year = now->tm_year + 1900;
|
|
||||||
|
|
||||||
tags :: commit_track_db();
|
tags :: commit_track_db();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,4 +10,8 @@ struct date {
|
||||||
unsigned int d_day;
|
unsigned int d_day;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/* Set the provided date structure to today's date. */
|
||||||
|
void date_today(struct date *);
|
||||||
|
|
||||||
#endif /* OCARINA_CORE_DATE_H */
|
#endif /* OCARINA_CORE_DATE_H */
|
||||||
|
|
|
@ -1,20 +1,28 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2015 (c) Anna Schumaker.
|
* Copyright 2015 (c) Anna Schumaker.
|
||||||
*/
|
*/
|
||||||
|
#define _GNU_SOURCE
|
||||||
|
|
||||||
#include <core/date.h>
|
#include <core/date.h>
|
||||||
#include <tests/test.h>
|
#include <tests/test.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
void test_date()
|
void test_date()
|
||||||
{
|
{
|
||||||
|
time_t rawtime = time(NULL);
|
||||||
|
struct tm *today = localtime(&rawtime);
|
||||||
struct date date = {
|
struct date date = {
|
||||||
.d_year = 0,
|
.d_year = 0,
|
||||||
.d_month = 0,
|
.d_month = 0,
|
||||||
.d_day = 0,
|
.d_day = 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
test_equal(date.d_year, 0);
|
date_today(NULL);
|
||||||
test_equal(date.d_month, 0);
|
date_today(&date);
|
||||||
test_equal(date.d_day, 0);
|
|
||||||
|
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(
|
DECLARE_UNIT_TESTS(
|
||||||
|
|
Loading…
Reference in New Issue