]> git.0d.be Git - empathy.git/blob - libempathy/empathy-time.c
empathy-time: add Collabora copyright
[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 program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program 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  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA  02110-1301  USA
20  *
21  * Authors: Richard Hult <richard@imendio.com>
22  */
23
24 #include "config.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <glib/gi18n.h>
30
31 #include "empathy-time.h"
32
33 /* Note: EmpathyTime is always in UTC. */
34
35 time_t
36 empathy_time_get_current (void)
37 {
38         return time (NULL);
39 }
40
41 time_t
42 empathy_time_get_local_time (struct tm *tm)
43 {
44         const gchar *tz;
45         time_t       t;
46
47         tz = g_getenv ("TZ");
48         g_setenv ("TZ", "", TRUE);
49
50         tzset ();
51
52         t = mktime (tm);
53
54         if (tz) {
55                 g_setenv ("TZ", tz, TRUE);
56         } else {
57                 g_unsetenv ("TZ");
58         }
59
60         tzset ();
61
62         return t;
63 }
64
65 /* The format is: "20021209T23:51:30" and is in UTC. 0 is returned on
66  * failure. The alternative format "20021209" is also accepted.
67  */
68 time_t
69 empathy_time_parse (const gchar *str)
70 {
71         struct tm tm;
72         gint      year, month;
73         gint      n_parsed;
74
75         memset (&tm, 0, sizeof (struct tm));
76
77         n_parsed = sscanf (str, "%4d%2d%2dT%2d:%2d:%2d",
78                     &year, &month, &tm.tm_mday, &tm.tm_hour,
79                            &tm.tm_min, &tm.tm_sec);
80         if (n_parsed != 3 && n_parsed != 6) {
81                 return 0;
82         }
83
84         tm.tm_year = year - 1900;
85         tm.tm_mon = month - 1;
86         tm.tm_isdst = -1;
87
88         return empathy_time_get_local_time (&tm);
89 }
90
91 /* Converts the UTC timestamp to a string, also in UTC. Returns NULL on failure. */
92 gchar *
93 empathy_time_to_string_utc (time_t       t,
94                             const gchar *format)
95 {
96         gchar      stamp[128];
97         struct tm *tm;
98
99         g_return_val_if_fail (format != NULL, NULL);
100
101         tm = gmtime (&t);
102         if (strftime (stamp, sizeof (stamp), format, tm) == 0) {
103                 return NULL;
104         }
105
106         return g_strdup (stamp);
107 }
108
109 /* Converts the UTC timestamp to a string, in local time. Returns NULL on failure. */
110 gchar *
111 empathy_time_to_string_local (time_t       t,
112                               const gchar *format)
113 {
114         gchar      stamp[128];
115         struct tm *tm;
116
117         g_return_val_if_fail (format != NULL, NULL);
118
119         tm = localtime (&t);
120         if (strftime (stamp, sizeof (stamp), format, tm) == 0) {
121                 return NULL;
122         }
123
124         return g_strdup (stamp);
125 }
126
127 gchar  *
128 empathy_time_to_string_relative (time_t then)
129 {
130         time_t now;
131         gint   seconds;
132
133         now = time (NULL);
134         seconds = now - then;
135
136         if (seconds > 0) {
137                 if (seconds < 60) {
138                         return g_strdup_printf (ngettext ("%d second ago",
139                                 "%d seconds ago", seconds), seconds);
140                 }
141                 else if (seconds < (60 * 60)) {
142                         seconds /= 60;
143                         return g_strdup_printf (ngettext ("%d minute ago",
144                                 "%d minutes ago", seconds), seconds);
145                 }
146                 else if (seconds < (60 * 60 * 24)) {
147                         seconds /= 60 * 60;
148                         return g_strdup_printf (ngettext ("%d hour ago",
149                                 "%d hours ago", seconds), seconds);
150                 }
151                 else if (seconds < (60 * 60 * 24 * 7)) {
152                         seconds /= 60 * 60 * 24;
153                         return g_strdup_printf (ngettext ("%d day ago",
154                                 "%d days ago", seconds), seconds);
155                 }
156                 else if (seconds < (60 * 60 * 24 * 30)) {
157                         seconds /= 60 * 60 * 24 * 7;
158                         return g_strdup_printf (ngettext ("%d week ago",
159                                 "%d weeks ago", seconds), seconds);
160                 }
161                 else {
162                         seconds /= 60 * 60 * 24 * 30;
163                         return g_strdup_printf (ngettext ("%d month ago",
164                                 "%d months ago", seconds), seconds);
165                 }
166         }
167         else {
168                 return g_strdup (_("in the future"));
169         }
170 }