From c40737394bff5c27daea7e4ee08f240224475904 Mon Sep 17 00:00:00 2001 From: Pierre-Luc Beaudoin Date: Tue, 26 May 2009 15:38:32 -0400 Subject: Display relative time to user The time of the geolocation update is displayed relatively to current time (1 min ago, 3 hours ago). The function just prints ("in the future") for debugging purpuses for time in the future. --- libempathy/empathy-time.c | 41 +++++++++++++++++++++++++++++++++++++++++ libempathy/empathy-time.h | 1 + 2 files changed, 42 insertions(+) (limited to 'libempathy') diff --git a/libempathy/empathy-time.c b/libempathy/empathy-time.c index 15b5c30a4..a482f4dc3 100644 --- a/libempathy/empathy-time.c +++ b/libempathy/empathy-time.c @@ -25,6 +25,7 @@ #include #include #include +#include #include "empathy-time.h" @@ -122,3 +123,43 @@ empathy_time_to_string_local (time_t t, return g_strdup (stamp); } +gchar * +empathy_time_to_string_relative (time_t then) +{ + time_t now; + gint seconds; + + now = time (NULL); + seconds = now - then; + + if (seconds > 0) { + if (seconds < 60) { + seconds /= 60; + return g_strdup_printf (ngettext ("%d second ago", + "%d seconds ago", seconds), seconds); + } + else if (seconds < (60 * 60)) { + seconds /= 60; + return g_strdup_printf (ngettext ("%d minute ago", + "%d minutes ago", seconds), seconds); + } + else if (seconds < (60 * 60 * 24)) { + seconds /= 60 * 60; + return g_strdup_printf (ngettext ("%d hour ago", + "%d hours ago", seconds), seconds); + } + else if (seconds < (60 * 60 * 24 * 7)) { + seconds /= 60 * 60 * 24; + return g_strdup_printf (ngettext ("%d day ago", + "%d days ago", seconds), seconds); + } + else { + seconds /= 60 * 60 * 24 * 30; + return g_strdup_printf (ngettext ("%d month ago", + "%d months ago", seconds), seconds); + } + } + else { + return g_strdup ("in the future"); + } +} diff --git a/libempathy/empathy-time.h b/libempathy/empathy-time.h index a6737386c..2875d2f9f 100644 --- a/libempathy/empathy-time.h +++ b/libempathy/empathy-time.h @@ -40,6 +40,7 @@ gchar *empathy_time_to_string_utc (time_t t, const gchar *format); gchar *empathy_time_to_string_local (time_t t, const gchar *format); +gchar *empathy_time_to_string_relative (time_t t); G_END_DECLS -- cgit v1.2.3