1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2003-2007 Imendio AB
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.
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.
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
20 * Authors: Richard Hult <richard@imendio.com>
28 #include <glib/gi18n.h>
30 #include "empathy-time.h"
32 /* Note: EmpathyTime is always in UTC. */
35 empathy_time_get_current (void)
41 empathy_time_get_local_time (struct tm *tm)
43 const gchar *timezone;
46 timezone = g_getenv ("TZ");
47 g_setenv ("TZ", "", TRUE);
54 g_setenv ("TZ", timezone, TRUE);
64 /* The format is: "20021209T23:51:30" and is in UTC. 0 is returned on
65 * failure. The alternative format "20021209" is also accepted.
68 empathy_time_parse (const gchar *str)
74 memset (&tm, 0, sizeof (struct tm));
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) {
83 tm.tm_year = year - 1900;
84 tm.tm_mon = month - 1;
87 return empathy_time_get_local_time (&tm);
90 /* Converts the UTC timestamp to a string, also in UTC. Returns NULL on failure. */
92 empathy_time_to_string_utc (time_t t,
98 g_return_val_if_fail (format != NULL, NULL);
101 if (strftime (stamp, sizeof (stamp), format, tm) == 0) {
105 return g_strdup (stamp);
108 /* Converts the UTC timestamp to a string, in local time. Returns NULL on failure. */
110 empathy_time_to_string_local (time_t t,
116 g_return_val_if_fail (format != NULL, NULL);
119 if (strftime (stamp, sizeof (stamp), format, tm) == 0) {
123 return g_strdup (stamp);
127 empathy_time_to_string_relative (time_t then)
133 seconds = now - then;
137 return g_strdup_printf (ngettext ("%d second ago",
138 "%d seconds ago", seconds), seconds);
140 else if (seconds < (60 * 60)) {
142 return g_strdup_printf (ngettext ("%d minute ago",
143 "%d minutes ago", seconds), seconds);
145 else if (seconds < (60 * 60 * 24)) {
147 return g_strdup_printf (ngettext ("%d hour ago",
148 "%d hours ago", seconds), seconds);
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);
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);
161 seconds /= 60 * 60 * 24 * 30;
162 return g_strdup_printf (ngettext ("%d month ago",
163 "%d months ago", seconds), seconds);
167 return g_strdup (_("in the future"));