]> git.0d.be Git - empathy.git/blob - libempathy/empathy-time.c
account-settings: allow to change the service
[empathy.git] / libempathy / empathy-time.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2003-2007 Imendio AB
4  * Copyright (C) 2007-2010 Collabora Ltd.
5  *
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.
10  *
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.
15  *
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
19  *
20  * Authors: Richard Hult <richard@imendio.com>
21  */
22
23 #include "config.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <glib/gi18n.h>
29
30 #include "empathy-time.h"
31
32 /* Note: EmpathyTime is always in UTC. */
33
34 gint64
35 empathy_time_get_current (void)
36 {
37         GDateTime *now;
38         gint64 result;
39
40         now = g_date_time_new_now_utc ();
41         result = g_date_time_to_unix (now);
42         g_date_time_unref (now);
43
44         return result;
45 }
46
47 /* Converts the UTC timestamp to a string, also in UTC. 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. Returns NULL on failure. */
65 gchar *
66 empathy_time_to_string_local (gint64 t,
67                               const gchar *format)
68 {
69         GDateTime *d, *local;
70         gchar *result;
71
72         g_return_val_if_fail (format != NULL, NULL);
73
74         d = g_date_time_new_from_unix_utc (t);
75         local = g_date_time_to_local (d);
76         g_date_time_unref (d);
77
78         result = g_date_time_format (local, format);
79         g_date_time_unref (local);
80
81         return result;
82 }
83
84 gchar *
85 empathy_duration_to_string (guint seconds)
86 {
87         if (seconds < 60) {
88                 return g_strdup_printf (ngettext ("%d second ago",
89                         "%d seconds ago", seconds), seconds);
90         }
91         else if (seconds < (60 * 60)) {
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                 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                 seconds /= 60 * 60 * 24;
103                 return g_strdup_printf (ngettext ("%d day ago",
104                         "%d days ago", seconds), seconds);
105         }
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);
110         }
111         else {
112                 seconds /= 60 * 60 * 24 * 30;
113                 return g_strdup_printf (ngettext ("%d month ago",
114                         "%d months ago", seconds), seconds);
115         }
116 }
117
118 gchar  *
119 empathy_time_to_string_relative (gint64 t)
120 {
121         GDateTime *now, *then;
122         gint   seconds;
123         GTimeSpan delta;
124         gchar *result;
125
126         now = g_date_time_new_now_utc ();
127         then = g_date_time_new_from_unix_utc (t);
128
129         delta = g_date_time_difference (now, then);
130         seconds = delta / G_TIME_SPAN_SECOND;
131
132         if (seconds > 0) {
133                 result = empathy_duration_to_string (seconds);
134         }
135         else {
136                 result = g_strdup (_("in the future"));
137         }
138
139         g_date_time_unref (now);
140         g_date_time_unref (then);
141
142         return result;
143 }