diff --git a/core/date.c b/core/date.c index 625d7bde..3dc54f07 100644 --- a/core/date.c +++ b/core/date.c @@ -2,6 +2,7 @@ * Copyright 2015 (c) Anna Schumaker. */ #include +#include #include @@ -27,6 +28,16 @@ void date_write(struct file *f, struct date *date) file_writef(f, "%u %u %u", date->d_year, date->d_month, date->d_day); } +gchar *date_string(const struct date *date) +{ + struct tm tm = { + .tm_year = date->d_year - 1900, + .tm_mon = date->d_month - 1, + .tm_mday = date->d_day, + }; + return string_tm2str(&tm); +} + int date_compare(const struct date *lhs, const struct date *rhs) { int ret = lhs->d_year - rhs->d_year; diff --git a/core/tags/track.cpp b/core/tags/track.cpp index 4e3781cd..490ddcb4 100644 --- a/core/tags/track.cpp +++ b/core/tags/track.cpp @@ -6,7 +6,6 @@ #include #include -#include static Database track_db("track.db", false); @@ -60,16 +59,13 @@ unsigned int Track :: track() { return _track; } const std::string Track :: date() const { - struct tm tm; - char buffer[20]; + char *buf; std::string res = "Never"; if (_count > 0) { - tm.tm_mday = _date.d_day; - tm.tm_mon = _date.d_month - 1; - tm.tm_year = _date.d_year - 1900; - strftime(buffer, 20, "%Ex", &tm); - res = buffer; + buf = date_string(&_date); + res = buf; + g_free(buf); } return res; } diff --git a/include/core/date.h b/include/core/date.h index 9f4bcf89..90879527 100644 --- a/include/core/date.h +++ b/include/core/date.h @@ -23,6 +23,12 @@ void date_read(struct file *, struct date *); /* Write the date to file. */ void date_write(struct file *, struct date *); +/* + * Convert the date into a string. + * This function allocates a new string that MUST be freed with g_free(). + */ +gchar *date_string(const struct date *); + /* * Compare two dates. * diff --git a/tests/core/date.c b/tests/core/date.c index dc5e51bd..801853da 100644 --- a/tests/core/date.c +++ b/tests/core/date.c @@ -5,6 +5,7 @@ #include #include +#include #include void test_date() @@ -17,8 +18,11 @@ void test_date() .d_day = 0, }; struct file f = FILE_INIT("date", 0); + gchar *str; date_today(NULL); + setlocale(LC_TIME, "C"); + date_today(&date); test_equal(date.d_year, today->tm_year + 1900); test_equal(date.d_month, today->tm_mon + 1); @@ -35,10 +39,15 @@ void test_date() test_equal(date.d_month, 6); test_equal(date.d_day, 17); + str = date_string(&date); + test_equal(str, "06/17/88"); + g_free(str); + date_read(&f, &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_close(&f); } void test_compare()