+ time_->tv_usec += microseconds % G_USEC_PER_SEC;
+ time_->tv_sec += microseconds / G_USEC_PER_SEC;
+ if (time_->tv_usec >= G_USEC_PER_SEC)
+ {
+ time_->tv_usec -= G_USEC_PER_SEC;
+ time_->tv_sec++;
+ }
+ }
+ else
+ {
+ microseconds *= -1;
+ time_->tv_usec -= microseconds % G_USEC_PER_SEC;
+ time_->tv_sec -= microseconds / G_USEC_PER_SEC;
+ if (time_->tv_usec < 0)
+ {
+ time_->tv_usec += G_USEC_PER_SEC;
+ time_->tv_sec--;
+ }
+ }
+}
+
+/* converts a broken down date representation, relative to UTC, to
+ * a timestamp; it uses timegm() if it's available.
+ */
+static time_t
+mktime_utc (struct tm *tm)
+{
+ time_t retval;
+
+#ifndef HAVE_TIMEGM
+ static const gint days_before[] =
+ {
+ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
+ };
+#endif
+
+#ifndef HAVE_TIMEGM
+ if (tm->tm_mon < 0 || tm->tm_mon > 11)
+ return (time_t) -1;
+
+ retval = (tm->tm_year - 70) * 365;
+ retval += (tm->tm_year - 68) / 4;
+ retval += days_before[tm->tm_mon] + tm->tm_mday - 1;
+
+ if (tm->tm_year % 4 == 0 && tm->tm_mon < 2)
+ retval -= 1;
+
+ retval = ((((retval * 24) + tm->tm_hour) * 60) + tm->tm_min) * 60 + tm->tm_sec;
+#else
+ retval = timegm (tm);
+#endif /* !HAVE_TIMEGM */
+
+ return retval;
+}
+
+/**
+ * g_time_val_from_iso8601:
+ * @iso_date: an ISO 8601 encoded date string
+ * @time_: (out): a #GTimeVal
+ *
+ * Converts a string containing an ISO 8601 encoded date and time
+ * to a #GTimeVal and puts it into @time_.
+ *
+ * @iso_date must include year, month, day, hours, minutes, and
+ * seconds. It can optionally include fractions of a second and a time
+ * zone indicator. (In the absence of any time zone indication, the
+ * timestamp is assumed to be in local time.)
+ *
+ * Return value: %TRUE if the conversion was successful.
+ *
+ * Since: 2.12
+ */
+gboolean
+g_time_val_from_iso8601 (const gchar *iso_date,
+ GTimeVal *time_)
+{
+ struct tm tm = {0};
+ long val;
+
+ g_return_val_if_fail (iso_date != NULL, FALSE);
+ g_return_val_if_fail (time_ != NULL, FALSE);
+
+ /* Ensure that the first character is a digit,
+ * the first digit of the date, otherwise we don't
+ * have an ISO 8601 date */
+ while (g_ascii_isspace (*iso_date))
+ iso_date++;
+
+ if (*iso_date == '\0')
+ return FALSE;
+
+ if (!g_ascii_isdigit (*iso_date) && *iso_date != '-' && *iso_date != '+')
+ return FALSE;
+
+ val = strtoul (iso_date, (char **)&iso_date, 10);
+ if (*iso_date == '-')
+ {
+ /* YYYY-MM-DD */
+ tm.tm_year = val - 1900;
+ iso_date++;
+ tm.tm_mon = strtoul (iso_date, (char **)&iso_date, 10) - 1;
+
+ if (*iso_date++ != '-')
+ return FALSE;
+
+ tm.tm_mday = strtoul (iso_date, (char **)&iso_date, 10);
+ }
+ else
+ {
+ /* YYYYMMDD */
+ tm.tm_mday = val % 100;
+ tm.tm_mon = (val % 10000) / 100 - 1;
+ tm.tm_year = val / 10000 - 1900;