core/string: Add string_tm2str() function to get a date stringg

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-10-28 03:55:40 -04:00
parent f5d744a75c
commit 0a2abb2b31
3 changed files with 33 additions and 0 deletions

View File

@ -39,6 +39,13 @@ gchar *string_sec2str_long(unsigned int sec)
return res;
}
gchar *string_tm2str(struct tm *tm)
{
gchar *buf = g_malloc(20 * sizeof(gchar));
strftime(buf, 20, "%Ex", tm);
return buf;
}
static gunichar __string_get_char(const gchar *str, const gchar *cur,
const gchar *res)
{

View File

@ -17,6 +17,9 @@ gchar *string_sec2str(unsigned int);
/* Convert number of seconds into a long-form time string. */
gchar *string_sec2str_long(unsigned int);
/* Convert a struct tm to a locale-dependant date string. */
gchar *string_tm2str(struct tm *);
/* Convert the input string to lowercase, dropping special characters. */
gchar *string_lowercase(const gchar *);

View File

@ -4,6 +4,7 @@
*/
#include <core/string.h>
#include <tests/test.h>
#include <locale.h>
#define str_test_equal(lhs, rhs) \
test_strings_equal(lhs, g_strdup(rhs), __LINE__)
@ -53,6 +54,27 @@ void test_sec2str_long()
"22 days, 4 hours, 12 minutes, 32 seconds");
}
void test_date()
{
struct tm tm = {
.tm_year = 92,
.tm_mon = 9,
.tm_mday = 5,
};
gchar *str;
setlocale(LC_TIME, "C");
str = string_tm2str(&tm);
test_equal(str, "10/05/92");
g_free(str);
setlocale(LC_TIME, "en_US");
str = string_tm2str(&tm);
test_equal(str, "10/05/1992");
g_free(str);
}
void test_lowercase()
{
str_test_equal(string_lowercase(""), "");
@ -82,6 +104,7 @@ void test_compare()
DECLARE_UNIT_TESTS(
UNIT_TEST("Seconds to String", test_sec2str),
UNIT_TEST("Seconds to String (Long)", test_sec2str_long),
UNIT_TEST("Date to String", test_date),
UNIT_TEST("String Lowercase", test_lowercase),
UNIT_TEST("String Comparison", test_compare),
);