From 93cf7856d170d4520a468479d392eef7cd756427 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sun, 7 Nov 2010 18:24:48 +0200 Subject: [PATCH] * mu-msg-str.[ch]: add mu_msg_str_display_date_s, displays time for time with 24h, date otherwise --- src/mu-msg-str.c | 21 ++++++++++++++++++++- src/mu-msg-str.h | 17 +++++++++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/mu-msg-str.c b/src/mu-msg-str.c index f5a05d26..4441d63b 100644 --- a/src/mu-msg-str.c +++ b/src/mu-msg-str.c @@ -65,12 +65,31 @@ mu_msg_str_date_s (const char* frm, time_t t) } char* -mu_msg_str_date (const char* frm, time_t t) +mu_msg_str_date (const char *frm, time_t t) { return g_strdup (mu_msg_str_date_s(frm, t)); } + +const char* +mu_msg_str_display_date_s (time_t t) +{ + time_t now; + static const guint SECS_IN_DAY = 24 * 60 * 60; + + now = time (NULL); + + if (ABS(now - t) > SECS_IN_DAY) + return mu_msg_str_date_s ("%x", t); + else + return mu_msg_str_date_s ("%X", t); +} + + + + + const char* mu_msg_str_size_s (size_t s) { diff --git a/src/mu-msg-str.h b/src/mu-msg-str.h index fe2d41ad..b79fca12 100644 --- a/src/mu-msg-str.h +++ b/src/mu-msg-str.h @@ -29,8 +29,7 @@ G_BEGIN_DECLS /** - * get a display string for a given time_t; - * use the preferred date/time for the current locale + * get a string for a given time_t * * mu_msg_str_date_s returns a ptr to a static buffer, * while mu_msg_str_date returns dynamically allocated @@ -48,6 +47,20 @@ const char* mu_msg_str_date_s (const char* frm, time_t t) G_GNUC_CONST; char* mu_msg_str_date (const char* frm, time_t t) G_GNUC_WARN_UNUSED_RESULT; +/** + * get a display string for a given time_t; if the given is less than + * 24h from the current time, we display the time, otherwise the date, + * using the preferred date/time for the current locale + * + * mu_msg_str_display_date_s returns a ptr to a static buffer, + * + * @param t the time as time_t + * + * @return a string representation of the time/date + */ +const char* mu_msg_str_display_date_s (time_t t); + + /** * get a display size for a given size_t; uses M for sizes > * 1000*1000, k for smaller sizes. Note: this function use the