1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2003-2007 Imendio AB
4 * Copyright (C) 2007-2010 Collabora Ltd.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Authors: Richard Hult <richard@imendio.com>
28 #include <glib/gi18n.h>
30 #include "empathy-time.h"
32 /* Note: EmpathyTime is always in UTC. */
35 empathy_time_get_current (void)
40 now = g_date_time_new_now_utc ();
41 result = g_date_time_to_unix (now);
42 g_date_time_unref (now);
47 /* Converts the UTC timestamp to a string, also in UTC. Returns NULL on failure. */
49 empathy_time_to_string_utc (gint64 t,
55 g_return_val_if_fail (format != NULL, NULL);
57 d = g_date_time_new_from_unix_utc (t);
58 result = g_date_time_format (d, format);
59 g_date_time_unref (d);
64 /* Converts the UTC timestamp to a string, in local time. Returns NULL on failure. */
66 empathy_time_to_string_local (gint64 t,
72 g_return_val_if_fail (format != NULL, NULL);
74 d = g_date_time_new_from_unix_utc (t);
75 local = g_date_time_to_local (d);
76 g_date_time_unref (d);
78 result = g_date_time_format (local, format);
79 g_date_time_unref (local);
85 empathy_duration_to_string (guint seconds)
88 return g_strdup_printf (ngettext ("%d second ago",
89 "%d seconds ago", seconds), seconds);
91 else if (seconds < (60 * 60)) {
93 return g_strdup_printf (ngettext ("%d minute ago",
94 "%d minutes ago", seconds), seconds);
96 else if (seconds < (60 * 60 * 24)) {
98 return g_strdup_printf (ngettext ("%d hour ago",
99 "%d hours ago", seconds), seconds);
101 else if (seconds < (60 * 60 * 24 * 7)) {
102 seconds /= 60 * 60 * 24;
103 return g_strdup_printf (ngettext ("%d day ago",
104 "%d days ago", seconds), seconds);
106 else if (seconds < (60 * 60 * 24 * 30)) {
107 seconds /= 60 * 60 * 24 * 7;
108 return g_strdup_printf (ngettext ("%d week ago",
109 "%d weeks ago", seconds), seconds);
112 seconds /= 60 * 60 * 24 * 30;
113 return g_strdup_printf (ngettext ("%d month ago",
114 "%d months ago", seconds), seconds);
119 empathy_time_to_string_relative (gint64 t)
121 GDateTime *now, *then;
126 now = g_date_time_new_now_utc ();
127 then = g_date_time_new_from_unix_utc (t);
129 delta = g_date_time_difference (now, then);
130 seconds = delta / G_TIME_SPAN_SECOND;
133 result = empathy_duration_to_string (seconds);
136 result = g_strdup (_("in the future"));
139 g_date_time_unref (now);
140 g_date_time_unref (then);