core/date: Add date_string() to convert a date to a string
Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
0a2abb2b31
commit
ea35d81c22
11
core/date.c
11
core/date.c
|
@ -2,6 +2,7 @@
|
|||
* Copyright 2015 (c) Anna Schumaker.
|
||||
*/
|
||||
#include <core/date.h>
|
||||
#include <core/string.h>
|
||||
#include <time.h>
|
||||
|
||||
|
||||
|
@ -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;
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
#include <core/tags/track.h>
|
||||
|
||||
#include <glib.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
static Database<Track> 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;
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
#include <core/date.h>
|
||||
#include <tests/test.h>
|
||||
#include <locale.h>
|
||||
#include <time.h>
|
||||
|
||||
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()
|
||||
|
|
Loading…
Reference in New Issue