]> git.0d.be Git - empathy.git/blob - libempathy/empathy-time.c
UOA: Do not segfault when "Done" or "Cancel" button clicked but widget is not ready yet
[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 <glib/gi18n-lib.h>
25
26 #include "empathy-time.h"
27
28 /* Note: EmpathyTime is always in UTC. */
29
30 gint64
31 empathy_time_get_current (void)
32 {
33   GDateTime *now;
34   gint64 result;
35
36   now = g_date_time_new_now_utc ();
37   result = g_date_time_to_unix (now);
38   g_date_time_unref (now);
39
40   return result;
41 }
42
43 /* Converts the UTC timestamp to a string, also in UTC.
44  * Returns NULL on failure. */
45 gchar *
46 empathy_time_to_string_utc (gint64 t,
47     const gchar *format)
48 {
49   GDateTime *d;
50   char *result;
51
52   g_return_val_if_fail (format != NULL, NULL);
53
54   d = g_date_time_new_from_unix_utc (t);
55   result = g_date_time_format (d, format);
56   g_date_time_unref (d);
57
58   return result;
59 }
60
61 /* Converts the UTC timestamp to a string, in local time.
62  * Returns NULL on failure. */
63 gchar *
64 empathy_time_to_string_local (gint64 t,
65     const gchar *format)
66 {
67   GDateTime *d, *local;
68   gchar *result;
69
70   g_return_val_if_fail (format != NULL, NULL);
71
72   d = g_date_time_new_from_unix_utc (t);
73   local = g_date_time_to_local (d);
74   g_date_time_unref (d);
75
76   result = g_date_time_format (local, format);
77   g_date_time_unref (local);
78
79   return result;
80 }
81
82 gchar *
83 empathy_duration_to_string (guint seconds)
84 {
85   if (seconds < 60)
86     {
87       return g_strdup_printf (ngettext ("%d second ago",
88         "%d seconds ago", seconds), seconds);
89     }
90   else if (seconds < (60 * 60))
91     {
92       seconds /= 60;
93       return g_strdup_printf (ngettext ("%d minute ago",
94         "%d minutes ago", seconds), seconds);
95     }
96   else if (seconds < (60 * 60 * 24))
97     {
98       seconds /= 60 * 60;
99       return g_strdup_printf (ngettext ("%d hour ago",
100         "%d hours ago", seconds), seconds);
101     }
102   else if (seconds < (60 * 60 * 24 * 7))
103     {
104       seconds /= 60 * 60 * 24;
105       return g_strdup_printf (ngettext ("%d day ago",
106         "%d days ago", seconds), seconds);
107     }
108   else if (seconds < (60 * 60 * 24 * 30))
109     {
110       seconds /= 60 * 60 * 24 * 7;
111       return g_strdup_printf (ngettext ("%d week ago",
112         "%d weeks ago", seconds), seconds);
113     }
114   else
115     {
116       seconds /= 60 * 60 * 24 * 30;
117       return g_strdup_printf (ngettext ("%d month ago",
118         "%d months ago", seconds), seconds);
119     }
120 }
121
122 gchar  *
123 empathy_time_to_string_relative (gint64 t)
124 {
125   GDateTime *now, *then;
126   gint   seconds;
127   GTimeSpan delta;
128   gchar *result;
129
130   now = g_date_time_new_now_utc ();
131   then = g_date_time_new_from_unix_utc (t);
132
133   delta = g_date_time_difference (now, then);
134   seconds = delta / G_TIME_SPAN_SECOND;
135
136   if (seconds > 0)
137     result = empathy_duration_to_string (seconds);
138   else
139     result = g_strdup (_("in the future"));
140
141   g_date_time_unref (now);
142   g_date_time_unref (then);
143
144   return result;
145 }