]> git.0d.be Git - empathy.git/blob - libempathy/empathy-time.c
Merge remote-tracking branch 'glassrose/add-All-service-selection-in-debug-window'
[empathy.git] / libempathy / empathy-time.c
1 /*
2  * Copyright (C) 2003-2007 Imendio AB
3  * Copyright (C) 2007-2012 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * Authors: Richard Hult <richard@imendio.com>
20  */
21
22 #include "config.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <glib/gi18n.h>
28
29 #include "empathy-time.h"
30
31 /* Note: EmpathyTime is always in UTC. */
32
33 gint64
34 empathy_time_get_current (void)
35 {
36   GDateTime *now;
37   gint64 result;
38
39   now = g_date_time_new_now_utc ();
40   result = g_date_time_to_unix (now);
41   g_date_time_unref (now);
42
43   return result;
44 }
45
46 /* Converts the UTC timestamp to a string, also in UTC.
47  * Returns NULL on failure. */
48 gchar *
49 empathy_time_to_string_utc (gint64 t,
50     const gchar *format)
51 {
52   GDateTime *d;
53   char *result;
54
55   g_return_val_if_fail (format != NULL, NULL);
56
57   d = g_date_time_new_from_unix_utc (t);
58   result = g_date_time_format (d, format);
59   g_date_time_unref (d);
60
61   return result;
62 }
63
64 /* Converts the UTC timestamp to a string, in local time.
65  * Returns NULL on failure. */
66 gchar *
67 empathy_time_to_string_local (gint64 t,
68     const gchar *format)
69 {
70   GDateTime *d, *local;
71   gchar *result;
72
73   g_return_val_if_fail (format != NULL, NULL);
74
75   d = g_date_time_new_from_unix_utc (t);
76   local = g_date_time_to_local (d);
77   g_date_time_unref (d);
78
79   result = g_date_time_format (local, format);
80   g_date_time_unref (local);
81
82   return result;
83 }
84
85 gchar *
86 empathy_duration_to_string (guint seconds)
87 {
88   if (seconds < 60)
89     {
90       return g_strdup_printf (ngettext ("%d second ago",
91         "%d seconds ago", seconds), seconds);
92     }
93   else if (seconds < (60 * 60))
94     {
95       seconds /= 60;
96       return g_strdup_printf (ngettext ("%d minute ago",
97         "%d minutes ago", seconds), seconds);
98     }
99   else if (seconds < (60 * 60 * 24))
100     {
101       seconds /= 60 * 60;
102       return g_strdup_printf (ngettext ("%d hour ago",
103         "%d hours ago", seconds), seconds);
104     }
105   else if (seconds < (60 * 60 * 24 * 7))
106     {
107       seconds /= 60 * 60 * 24;
108       return g_strdup_printf (ngettext ("%d day ago",
109         "%d days ago", seconds), seconds);
110     }
111   else if (seconds < (60 * 60 * 24 * 30))
112     {
113       seconds /= 60 * 60 * 24 * 7;
114       return g_strdup_printf (ngettext ("%d week ago",
115         "%d weeks ago", seconds), seconds);
116     }
117   else
118     {
119       seconds /= 60 * 60 * 24 * 30;
120       return g_strdup_printf (ngettext ("%d month ago",
121         "%d months ago", seconds), seconds);
122     }
123 }
124
125 gchar  *
126 empathy_time_to_string_relative (gint64 t)
127 {
128   GDateTime *now, *then;
129   gint   seconds;
130   GTimeSpan delta;
131   gchar *result;
132
133   now = g_date_time_new_now_utc ();
134   then = g_date_time_new_from_unix_utc (t);
135
136   delta = g_date_time_difference (now, then);
137   seconds = delta / G_TIME_SPAN_SECOND;
138
139   if (seconds > 0)
140     result = empathy_duration_to_string (seconds);
141   else
142     result = g_strdup (_("in the future"));
143
144   g_date_time_unref (now);
145   g_date_time_unref (then);
146
147   return result;
148 }