aboutsummaryrefslogtreecommitdiffstats
path: root/libempathy/empathy-time.c
diff options
context:
space:
mode:
authorGuillaume Desmottes <guillaume.desmottes@collabora.co.uk>2011-04-19 17:06:52 +0800
committerDanielle Madeley <danielle.madeley@collabora.co.uk>2011-04-21 10:40:06 +0800
commit845c7de7a4caee49f6cea3b9f10f0df3db9d0971 (patch)
treec2fa2f7f28fc5fa8099e22cd635106b2ddc9aaca /libempathy/empathy-time.c
parent3aa72e42ffe1ce5068dfc5c351d05598f2884bea (diff)
downloadgsoc2013-empathy-845c7de7a4caee49f6cea3b9f10f0df3db9d0971.tar
gsoc2013-empathy-845c7de7a4caee49f6cea3b9f10f0df3db9d0971.tar.gz
gsoc2013-empathy-845c7de7a4caee49f6cea3b9f10f0df3db9d0971.tar.bz2
gsoc2013-empathy-845c7de7a4caee49f6cea3b9f10f0df3db9d0971.tar.lz
gsoc2013-empathy-845c7de7a4caee49f6cea3b9f10f0df3db9d0971.tar.xz
gsoc2013-empathy-845c7de7a4caee49f6cea3b9f10f0df3db9d0971.tar.zst
gsoc2013-empathy-845c7de7a4caee49f6cea3b9f10f0df3db9d0971.zip
Port all timestamps from time_t to gint64 (#648188)
Diffstat (limited to 'libempathy/empathy-time.c')
-rw-r--r--libempathy/empathy-time.c76
1 files changed, 47 insertions, 29 deletions
diff --git a/libempathy/empathy-time.c b/libempathy/empathy-time.c
index b0ad01888..f33152d97 100644
--- a/libempathy/empathy-time.c
+++ b/libempathy/empathy-time.c
@@ -31,89 +31,107 @@
/* Note: EmpathyTime is always in UTC. */
-time_t
+gint64
empathy_time_get_current (void)
{
- return time (NULL);
+ GDateTime *now;
+ gint64 result;
+
+ now = g_date_time_new_now_utc ();
+ result = g_date_time_to_unix (now);
+ g_date_time_unref (now);
+
+ return result;
}
/* Converts the UTC timestamp to a string, also in UTC. Returns NULL on failure. */
gchar *
-empathy_time_to_string_utc (time_t t,
+empathy_time_to_string_utc (gint64 t,
const gchar *format)
{
- gchar stamp[128];
- struct tm *tm;
+ GDateTime *d;
+ char *result;
g_return_val_if_fail (format != NULL, NULL);
- tm = gmtime (&t);
- if (strftime (stamp, sizeof (stamp), format, tm) == 0) {
- return NULL;
- }
+ d = g_date_time_new_from_unix_utc (t);
+ result = g_date_time_format (d, format);
+ g_date_time_unref (d);
- return g_strdup (stamp);
+ return result;
}
/* Converts the UTC timestamp to a string, in local time. Returns NULL on failure. */
gchar *
-empathy_time_to_string_local (time_t t,
+empathy_time_to_string_local (gint64 t,
const gchar *format)
{
- gchar stamp[128];
- struct tm *tm;
+ GDateTime *d, *local;
+ gchar *result;
g_return_val_if_fail (format != NULL, NULL);
- tm = localtime (&t);
- if (strftime (stamp, sizeof (stamp), format, tm) == 0) {
- return NULL;
- }
+ d = g_date_time_new_from_unix_utc (t);
+ local = g_date_time_to_local (d);
+ g_date_time_unref (d);
- return g_strdup (stamp);
+ result = g_date_time_format (local, format);
+ g_date_time_unref (local);
+
+ return result;
}
gchar *
-empathy_time_to_string_relative (time_t then)
+empathy_time_to_string_relative (gint64 t)
{
- time_t now;
+ GDateTime *now, *then;
gint seconds;
+ GTimeSpan delta;
+ gchar *result;
+
+ now = g_date_time_new_now_utc ();
+ then = g_date_time_new_from_unix_utc (t);
- now = time (NULL);
- seconds = now - then;
+ delta = g_date_time_difference (now, then);
+ seconds = delta / G_TIME_SPAN_SECOND;
if (seconds > 0) {
if (seconds < 60) {
- return g_strdup_printf (ngettext ("%d second ago",
+ result = 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",
+ result = 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",
+ result = 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",
+ result = g_strdup_printf (ngettext ("%d day ago",
"%d days ago", seconds), seconds);
}
else if (seconds < (60 * 60 * 24 * 30)) {
seconds /= 60 * 60 * 24 * 7;
- return g_strdup_printf (ngettext ("%d week ago",
+ result = g_strdup_printf (ngettext ("%d week ago",
"%d weeks ago", seconds), seconds);
}
else {
seconds /= 60 * 60 * 24 * 30;
- return g_strdup_printf (ngettext ("%d month ago",
+ result = g_strdup_printf (ngettext ("%d month ago",
"%d months ago", seconds), seconds);
}
}
else {
- return g_strdup (_("in the future"));
+ result = g_strdup (_("in the future"));
}
+
+ g_date_time_unref (now);
+ g_date_time_unref (then);
+
+ return result;
}