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