+
+/* 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.)
+ *
+ * Returns: %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;
+ }
+
+ if (*iso_date != 'T')
+ return FALSE;
+
+ iso_date++;
+
+ /* If there is a 'T' then there has to be a time */
+ if (!g_ascii_isdigit (*iso_date))
+ return FALSE;
+
+ val = strtoul (iso_date, (char **)&iso_date, 10);
+ if (*iso_date == ':')
+ {
+ /* hh:mm:ss */
+ tm.tm_hour = val;
+ iso_date++;
+ tm.tm_min = strtoul (iso_date, (char **)&iso_date, 10);
+
+ if (*iso_date++ != ':')
+ return FALSE;
+
+ tm.tm_sec = strtoul (iso_date, (char **)&iso_date, 10);
+ }
+ else
+ {
+ /* hhmmss */
+ tm.tm_sec = val % 100;
+ tm.tm_min = (val % 10000) / 100;
+ tm.tm_hour = val / 10000;
+ }
+
+ time_->tv_usec = 0;
+
+ if (*iso_date == ',' || *iso_date == '.')
+ {
+ glong mul = 100000;
+
+ while (g_ascii_isdigit (*++iso_date))
+ {
+ time_->tv_usec += (*iso_date - '0') * mul;
+ mul /= 10;
+ }
+ }
+
+ /* Now parse the offset and convert tm to a time_t */
+ if (*iso_date == 'Z')
+ {
+ iso_date++;
+ time_->tv_sec = mktime_utc (&tm);
+ }
+ else if (*iso_date == '+' || *iso_date == '-')
+ {
+ gint sign = (*iso_date == '+') ? -1 : 1;
+
+ val = strtoul (iso_date + 1, (char **)&iso_date, 10);
+
+ if (*iso_date == ':')
+ val = 60 * val + strtoul (iso_date + 1, (char **)&iso_date, 10);
+ else
+ val = 60 * (val / 100) + (val % 100);
+
+ time_->tv_sec = mktime_utc (&tm) + (time_t) (60 * val * sign);
+ }
+ else
+ {
+ /* No "Z" or offset, so local time */
+ tm.tm_isdst = -1; /* locale selects DST */
+ time_->tv_sec = mktime (&tm);
+ }
+
+ while (g_ascii_isspace (*iso_date))
+ iso_date++;
+
+ return *iso_date == '\0';
+}
+
+/**
+ * g_time_val_to_iso8601:
+ * @time_: a #GTimeVal
+ *
+ * Converts @time_ into an RFC 3339 encoded string, relative to the
+ * Coordinated Universal Time (UTC). This is one of the many formats
+ * allowed by ISO 8601.
+ *
+ * ISO 8601 allows a large number of date/time formats, with or without
+ * punctuation and optional elements. The format returned by this function
+ * is a complete date and time, with optional punctuation included, the
+ * UTC time zone represented as "Z", and the @tv_usec part included if
+ * and only if it is nonzero, i.e. either
+ * "YYYY-MM-DDTHH:MM:SSZ" or "YYYY-MM-DDTHH:MM:SS.fffffZ".
+ *
+ * This corresponds to the Internet date/time format defined by
+ * [RFC 3339](https://www.ietf.org/rfc/rfc3339.txt),
+ * and to either of the two most-precise formats defined by
+ * the W3C Note
+ * [Date and Time Formats](http://www.w3.org/TR/NOTE-datetime-19980827).
+ * Both of these documents are profiles of ISO 8601.
+ *
+ * Use g_date_time_format() or g_strdup_printf() if a different
+ * variation of ISO 8601 format is required.
+ *
+ * Returns: a newly allocated string containing an ISO 8601 date
+ *
+ * Since: 2.12
+ */
+gchar *
+g_time_val_to_iso8601 (GTimeVal *time_)
+{
+ gchar *retval;
+ struct tm *tm;
+#ifdef HAVE_GMTIME_R
+ struct tm tm_;
+#endif
+ time_t secs;
+
+ g_return_val_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC, NULL);
+
+ secs = time_->tv_sec;
+#ifdef _WIN32
+ tm = gmtime (&secs);
+#else
+#ifdef HAVE_GMTIME_R
+ tm = gmtime_r (&secs, &tm_);
+#else
+ tm = gmtime (&secs);
+#endif
+#endif
+
+ if (time_->tv_usec != 0)
+ {
+ /* ISO 8601 date and time format, with fractionary seconds:
+ * YYYY-MM-DDTHH:MM:SS.MMMMMMZ
+ */
+ retval = g_strdup_printf ("%4d-%02d-%02dT%02d:%02d:%02d.%06ldZ",
+ tm->tm_year + 1900,
+ tm->tm_mon + 1,
+ tm->tm_mday,
+ tm->tm_hour,
+ tm->tm_min,
+ tm->tm_sec,
+ time_->tv_usec);
+ }
+ else
+ {
+ /* ISO 8601 date and time format:
+ * YYYY-MM-DDTHH:MM:SSZ
+ */
+ retval = g_strdup_printf ("%4d-%02d-%02dT%02d:%02d:%02dZ",
+ tm->tm_year + 1900,
+ tm->tm_mon + 1,
+ tm->tm_mday,
+ tm->tm_hour,
+ tm->tm_min,
+ tm->tm_sec);
+ }
+
+ return retval;
+}