From cef39bc0fcb938eef0e67561dca8cf4c0f33676f Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 27 Oct 2015 19:13:08 -0400 Subject: [PATCH] core/date: Add date_today() function for finding today's date Signed-off-by: Anna Schumaker --- core/date.c | 18 ++++++++++++++++++ core/tags/track.cpp | 8 +------- include/core/date.h | 4 ++++ tests/core/date.c | 14 +++++++++++--- 4 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 core/date.c diff --git a/core/date.c b/core/date.c new file mode 100644 index 00000000..dde33e6a --- /dev/null +++ b/core/date.c @@ -0,0 +1,18 @@ +/* + * Copyright 2015 (c) Anna Schumaker. + */ +#include +#include + + +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; + } +} diff --git a/core/tags/track.cpp b/core/tags/track.cpp index f5f471b1..b7e2489e 100644 --- a/core/tags/track.cpp +++ b/core/tags/track.cpp @@ -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(); } diff --git a/include/core/date.h b/include/core/date.h index d5009b02..1afec771 100644 --- a/include/core/date.h +++ b/include/core/date.h @@ -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 */ diff --git a/tests/core/date.c b/tests/core/date.c index ab77e431..076b0966 100644 --- a/tests/core/date.c +++ b/tests/core/date.c @@ -1,20 +1,28 @@ /* * Copyright 2015 (c) Anna Schumaker. */ +#define _GNU_SOURCE + #include #include +#include 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(