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