>From 10a07bf30ee42952789a6d7951abf46affce0c57 Mon Sep 17 00:00:00 2001 From: Bastien Nocera Date: Wed, 10 Feb 2010 13:31:21 +0000 Subject: [PATCH 3/4] Use glib functions for parse_iso8601() Instead of this fragile home-grown function --- src/utils.c | 53 +++++++---------------------------------------------- 1 files changed, 7 insertions(+), 46 deletions(-) diff --git a/src/utils.c b/src/utils.c index 628b832..c75c2d6 100644 --- a/src/utils.c +++ b/src/utils.c @@ -107,61 +107,22 @@ char * make_iso8601(time_t time) { return g_time_val_to_iso8601(&tv); } -/* From Imendio's GnomeVFS OBEX module (om-utils.c) */ time_t parse_iso8601(const gchar *str, int len) { gchar *tstr; - struct tm tm; - gint nr; - gchar tz; - time_t time; - time_t tz_offset = 0; - - memset (&tm, 0, sizeof (struct tm)); + GTimeVal tv; /* According to spec the time doesn't have to be null terminated */ - if (str[len - 1] != '\0') { - tstr = g_malloc(len + 1); - strncpy(tstr, str, len); - tstr[len] = '\0'; - } + if (str[len - 1] != '\0') + tstr = g_strndup(str, len); else tstr = g_strdup(str); - nr = sscanf (tstr, "%04u%02u%02uT%02u%02u%02u%c", - &tm.tm_year, &tm.tm_mon, &tm.tm_mday, - &tm.tm_hour, &tm.tm_min, &tm.tm_sec, - &tz); - - g_free(tstr); - - /* Fixup the tm values */ - tm.tm_year -= 1900; /* Year since 1900 */ - tm.tm_mon--; /* Months since January, values 0-11 */ - tm.tm_isdst = -1; /* Daylight savings information not avail */ - - if (nr < 6) { - /* Invalid time format */ + if (g_time_val_from_iso8601(tstr, &tv) == FALSE) { + g_free (tstr); return -1; } - time = mktime (&tm); - -#if defined(HAVE_TM_GMTOFF) - tz_offset = tm.tm_gmtoff; -#elif defined(HAVE_TIMEZONE) - tz_offset = -timezone; - if (tm.tm_isdst > 0) { - tz_offset += 3600; - } -#endif - - if (nr == 7) { /* Date/Time was in localtime (to remote device) - * already. Since we don't know anything about the - * timezone on that one we won't try to apply UTC offset - */ - time += tz_offset; - } - - return time; + g_free (tstr); + return tv.tv_sec; } -- 1.6.6