1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2003-2007 Imendio AB
4 * Copyright (C) 2007-2010 Collabora Ltd.
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.
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.
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
21 * Authors: Richard Hult <richard@imendio.com>
29 #include <glib/gi18n.h>
31 #include "empathy-time.h"
33 /* Note: EmpathyTime is always in UTC. */
36 empathy_time_get_current (void)
42 empathy_time_get_local_time (struct tm *tm)
48 g_setenv ("TZ", "", TRUE);
55 g_setenv ("TZ", tz, TRUE);
65 /* The format is: "20021209T23:51:30" and is in UTC. 0 is returned on
66 * failure. The alternative format "20021209" is also accepted.
69 empathy_time_parse (const gchar *str)
75 memset (&tm, 0, sizeof (struct tm));
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) {
84 tm.tm_year = year - 1900;
85 tm.tm_mon = month - 1;
88 return empathy_time_get_local_time (&tm);
91 /* Converts the UTC timestamp to a string, also in UTC. Returns NULL on failure. */
93 empathy_time_to_string_utc (time_t t,
99 g_return_val_if_fail (format != NULL, NULL);
102 if (strftime (stamp, sizeof (stamp), format, tm) == 0) {
106 return g_strdup (stamp);
109 /* Converts the UTC timestamp to a string, in local time. Returns NULL on failure. */
111 empathy_time_to_string_local (time_t t,
117 g_return_val_if_fail (format != NULL, NULL);
120 if (strftime (stamp, sizeof (stamp), format, tm) == 0) {
124 return g_strdup (stamp);
128 empathy_time_to_string_relative (time_t then)
134 seconds = now - then;
138 return g_strdup_printf (ngettext ("%d second ago",
139 "%d seconds ago", seconds), seconds);
141 else if (seconds < (60 * 60)) {
143 return g_strdup_printf (ngettext ("%d minute ago",
144 "%d minutes ago", seconds), seconds);
146 else if (seconds < (60 * 60 * 24)) {
148 return g_strdup_printf (ngettext ("%d hour ago",
149 "%d hours ago", seconds), seconds);
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);
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);
162 seconds /= 60 * 60 * 24 * 30;
163 return g_strdup_printf (ngettext ("%d month ago",
164 "%d months ago", seconds), seconds);
168 return g_strdup (_("in the future"));