]> git.0d.be Git - empathy.git/blob - libempathy/empathy-time.c
Rename all filenames starting with "gossip" by "empathy", change namespace
[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., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, 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
29 #include "empathy-time.h"
30
31 /* Note: EmpathyTime is always in UTC. */
32
33 EmpathyTime
34 empathy_time_get_current (void)
35 {
36         return time (NULL);
37 }
38
39 time_t
40 empathy_time_get_local_time (struct tm *tm)
41 {
42         const gchar *timezone;
43         time_t       t;
44         
45         timezone = g_getenv ("TZ");
46         g_setenv ("TZ", "", TRUE);
47
48         tzset ();
49
50         t = mktime (tm);
51
52         if (timezone) {
53                 g_setenv ("TZ", timezone, TRUE);
54         } else {
55                 g_unsetenv ("TZ");
56         }
57
58         tzset ();
59
60         return t;
61 }
62
63 /* The format is: "20021209T23:51:30" and is in UTC. 0 is returned on
64  * failure. The alternative format "20021209" is also accepted.
65  */
66 EmpathyTime
67 empathy_time_parse (const gchar *str)
68 {
69         struct tm tm;
70         gint      year, month;
71         gint      n_parsed;
72
73         memset (&tm, 0, sizeof (struct tm));
74
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) {
79                 return 0;
80         }
81
82         tm.tm_year = year - 1900;
83         tm.tm_mon = month - 1;
84         tm.tm_isdst = -1;
85
86         return empathy_time_get_local_time (&tm);
87 }
88
89 /* Converts the UTC timestamp to a string, also in UTC. Returns NULL on failure. */
90 gchar *
91 empathy_time_to_string_utc (EmpathyTime   t,
92                            const gchar *format)
93 {
94         gchar      stamp[128];
95         struct tm *tm;
96
97         g_return_val_if_fail (format != NULL, NULL);
98
99         tm = gmtime (&t);
100         if (strftime (stamp, sizeof (stamp), format, tm) == 0) {
101                 return NULL;
102         }
103
104         return g_strdup (stamp);
105 }
106
107 /* Converts the UTC timestamp to a string, in local time. Returns NULL on failure. */
108 gchar *
109 empathy_time_to_string_local (EmpathyTime   t,
110                              const gchar *format)
111 {
112         gchar      stamp[128];
113         struct tm *tm;
114
115         g_return_val_if_fail (format != NULL, NULL);
116
117         tm = localtime (&t);
118         if (strftime (stamp, sizeof (stamp), format, tm) == 0) {
119                 return NULL;
120         }
121
122         return g_strdup (stamp);
123 }
124