1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
5 * Copyright (C) 2002-2006 Jody Goldberg (jody@gnome.org)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of version 2.1 of the GNU Lesser General Public
9 * License as published by the Free Software Foundation.
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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
22 #include <gsf-config.h>
23 #include <gsf/gsf-timestamp.h>
32 timestamp_to_string (GValue const *src_value, GValue *dest_value)
34 char *str = gsf_timestamp_as_string (g_value_get_boxed (src_value));
35 g_value_set_string (dest_value, str);
40 gsf_timestamp_get_type (void)
42 static GType our_type = 0;
45 our_type = g_boxed_type_register_static ("GsfTimestamp",
46 (GBoxedCopyFunc)gsf_timestamp_copy,
47 (GBoxedFreeFunc)gsf_timestamp_free);
48 g_value_register_transform_func (our_type, G_TYPE_STRING,
49 ×tamp_to_string);
55 gsf_timestamp_new (void)
57 GsfTimestamp *res = g_new0 (GsfTimestamp, 1);
64 * @stamp: timestamp to be copied
68 * Returns: a separate copy of @stamp.
71 gsf_timestamp_copy (GsfTimestamp const *stamp)
73 GsfTimestamp *res = gsf_timestamp_new ();
74 res->timet = stamp->timet;
79 * gsf_timestamp_free :
80 * @stamp : timestamp to be freed
82 * Releases the memory in use for @stamp (if any).
85 gsf_timestamp_free (GsfTimestamp *stamp)
90 #if defined(HAVE_STRUCT_TM_TM_GMTOFF)
91 #define GMTOFF(t) ((t).tm_gmtoff)
92 #elif defined(HAVE_STRUCT_TM___TM_GMTOFF)
93 #define GMTOFF(t) ((t).__tm_gmtoff)
94 #elif defined(G_OS_WIN32)
95 #define GMTOFF(t) (gmt_to_local_win32())
97 /* FIXME: work out the offset anyway. */
102 static time_t gmt_to_local_win32(void)
104 TIME_ZONE_INFORMATION tzinfo;
105 DWORD dwStandardDaylight;
108 dwStandardDaylight = GetTimeZoneInformation(&tzinfo);
111 if (dwStandardDaylight == TIME_ZONE_ID_STANDARD)
112 bias += tzinfo.StandardBias;
114 if (dwStandardDaylight == TIME_ZONE_ID_DAYLIGHT)
115 bias += tzinfo.DaylightBias;
117 return (- bias * 60);
122 * gsf_timestamp_from_string :
123 * @spec : The string to parse
124 * @stamp : #GsfTimestamp
126 * Very simple parser for time stamps. Currently requires a format of
127 * 'YYYY-MM-DDThh:mm:ss'
128 * and does no bounds checking.
132 * Returns: %TRUE on success
135 gsf_timestamp_from_string (char const *spec, GsfTimestamp *stamp)
139 memset (&tm, 0, sizeof (struct tm));
141 /* 'YYYY-MM-DDThh:mm:ss' */
142 if (6 == sscanf (spec, "%d-%d-%dT%d:%d:%d",
143 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
144 &tm.tm_hour, &tm.tm_min, &tm.tm_sec)) {
147 tm.tm_mon--; /* 0..11 */
149 /* err on the side of avoiding negatives */
150 if (tm.tm_year >= 1900)
157 stamp->timet = t + GMTOFF(tm);
164 * gsf_timestamp_parse :
165 * @spec : The string to parse
166 * @stamp : #GsfTimestamp
168 * Very simple parser for time stamps. Currently requires a format of
169 * 'YYYY-MM-DDThh:mm:ss'
170 * and does no bounds checking.
172 * Deprecated : Use gsf_timestamp_from_string
174 * Returns: %TRUE on success
177 gsf_timestamp_parse (char const *spec, GsfTimestamp *stamp)
179 return gsf_timestamp_from_string (spec, stamp);
183 * gsf_timestamp_as_string :
184 * @stamp: timestamp to be converted.
186 * Produce a string representation (ISO 8601 format) of @stamp.
188 * Returns: a string representation of @stamp. When @stamp is %NULL, the
189 * representation is "<invalid>".
192 gsf_timestamp_as_string (GsfTimestamp const *stamp)
197 g_return_val_if_fail (stamp != NULL, g_strdup ("<invalid>"));
199 t = stamp->timet; /* Use an honest time_t for gmtime_r. */
203 /* -NOT- thread-safe */
208 /* using 'YYYY-MM-DDThh:mm:ss' */
209 return g_strdup_printf ("%4d-%02d-%02dT%02d:%02d:%02dZ",
210 tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
211 tm.tm_hour, tm.tm_min, tm.tm_sec);
215 gsf_timestamp_hash (GsfTimestamp const *stamp)
221 * gsf_timestamp_equal :
223 * @b: another timestamp
225 * Compare timestamps @a and @b.
227 * Returns: true if @a and @b represent the same point in time; false otherwise.
231 gsf_timestamp_equal (GsfTimestamp const *a, GsfTimestamp const *b)
233 return a->timet == b->timet;
237 gsf_value_set_timestamp (GValue *value, GsfTimestamp const *stamp)
239 g_value_set_boxed (value, stamp);
243 gsf_timestamp_set_time (GsfTimestamp *stamp, guint64 t)