1 /*
2 * Copyright (C) 2006, Jamie McCracken <jamiemcc@gnome.org>
3 * Copyright (C) 2008-2010, Nokia <ivan.frade@nokia.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library 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 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 #include "config.h"
22
23 #include <glib.h>
24
25 #include "tracker-date-time.h"
26
tracker_date_error_quark(void)27 GQuark tracker_date_error_quark (void) {
28 return g_quark_from_static_string ("tracker_date_error-quark");
29 }
30
31 GDateTime *
tracker_date_new_from_iso8601(const gchar * string,GError ** error)32 tracker_date_new_from_iso8601 (const gchar *string,
33 GError **error)
34 {
35 GDateTime *datetime;
36 GTimeZone *tz;
37
38 tz = g_time_zone_new_local ();
39 datetime = g_date_time_new_from_iso8601 (string, tz);
40 g_time_zone_unref (tz);
41
42 if (!datetime) {
43 g_set_error (error,
44 TRACKER_DATE_ERROR,
45 TRACKER_DATE_ERROR_INVALID_ISO8601,
46 "'%s' is not a ISO 8601 date string. "
47 "Allowed form is CCYY-MM-DDThh:mm:ss[.ssssss][Z|(+|-)hh:mm]",
48 string);
49 }
50
51 return datetime;
52 }
53
54 gchar *
tracker_date_format_iso8601(GDateTime * datetime)55 tracker_date_format_iso8601 (GDateTime *datetime)
56 {
57 if (g_date_time_get_utc_offset (datetime) == 0)
58 return g_date_time_format (datetime, "%C%y-%m-%dT%TZ");
59 else
60 return g_date_time_format (datetime, "%C%y-%m-%dT%T%:z");
61 }
62