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
committerGuillaume Desmottes <guillaume.desmottes@collabora.co.uk>2011-04-19 18:16:11 +0800
commit92af7e9f7c5f114963299fcf53b48b4ed86a2f7b (patch)
tree24fc4607cd492d777c2e1ee4a5f77a6381059fd8 /libempathy/empathy-time.c
parent2d1ca4e5b9f7fdff7b1ea2d4fc31828d7dbbbae8 (diff)
downloadgsoc2013-empathy-92af7e9f7c5f114963299fcf53b48b4ed86a2f7b.tar
gsoc2013-empathy-92af7e9f7c5f114963299fcf53b48b4ed86a2f7b.tar.gz
gsoc2013-empathy-92af7e9f7c5f114963299fcf53b48b4ed86a2f7b.tar.bz2
gsoc2013-empathy-92af7e9f7c5f114963299fcf53b48b4ed86a2f7b.tar.lz
gsoc2013-empathy-92af7e9f7c5f114963299fcf53b48b4ed86a2f7b.tar.xz
gsoc2013-empathy-92af7e9f7c5f114963299fcf53b48b4ed86a2f7b.tar.zst
gsoc2013-empathy-92af7e9f7c5f114963299fcf53b48b4ed86a2f7b.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;
}