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., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Richard Hult <richard@imendio.com>
29 #include "empathy-time.h"
31 /* Note: EmpathyTime is always in UTC. */
34 empathy_time_get_current (void)
40 empathy_time_get_local_time (struct tm *tm)
42 const gchar *timezone;
45 timezone = g_getenv ("TZ");
46 g_setenv ("TZ", "", TRUE);
53 g_setenv ("TZ", timezone, TRUE);
63 /* The format is: "20021209T23:51:30" and is in UTC. 0 is returned on
64 * failure. The alternative format "20021209" is also accepted.
67 empathy_time_parse (const gchar *str)
73 memset (&tm, 0, sizeof (struct tm));
75 n_parsed = sscanf (str, "%4d%2d%2dT%2d:%2d:%2d",
76 &year, &month, &tm.tm_mday, &tm.tm_hour,
77 &tm.tm_min, &tm.tm_sec);
78 if (n_parsed != 3 && n_parsed != 6) {
82 tm.tm_year = year - 1900;
83 tm.tm_mon = month - 1;
86 return empathy_time_get_local_time (&tm);
89 /* Converts the UTC timestamp to a string, also in UTC. Returns NULL on failure. */
91 empathy_time_to_string_utc (time_t t,
97 g_return_val_if_fail (format != NULL, NULL);
100 if (strftime (stamp, sizeof (stamp), format, tm) == 0) {
104 return g_strdup (stamp);
107 /* Converts the UTC timestamp to a string, in local time. Returns NULL on failure. */
109 empathy_time_to_string_local (time_t t,
115 g_return_val_if_fail (format != NULL, NULL);
118 if (strftime (stamp, sizeof (stamp), format, tm) == 0) {
122 return g_strdup (stamp);