datetime: do our own serialisation so we can serialise microseconds as well

We still don't do that in _to_iso8601_string() though, since
this will probably mostly be used in tags, where it doesn't
matter so much and the microsecond argument might not be
well-received by some tag readers.
This commit is contained in:
Tim-Philipp Müller 2012-07-07 22:40:12 +01:00
parent 853e2bf67f
commit 8e7bc47ccd

View file

@ -671,6 +671,74 @@ gst_date_time_new (gfloat tzoffset, gint year, gint month, gint day, gint hour,
return datetime;
}
gchar *
__gst_date_time_serialize (GstDateTime * datetime, gboolean serialize_usecs)
{
GString *s;
gfloat gmt_offset;
guint msecs;
/* we always have at least the year */
s = g_string_new (NULL);
g_string_append_printf (s, "%04u", gst_date_time_get_year (datetime));
if (datetime->fields == GST_DATE_TIME_FIELDS_Y)
goto done;
/* add month */
g_string_append_printf (s, "-%02u", gst_date_time_get_month (datetime));
if (datetime->fields == GST_DATE_TIME_FIELDS_YM)
goto done;
/* add day of month */
g_string_append_printf (s, "-%02u", gst_date_time_get_day (datetime));
if (datetime->fields == GST_DATE_TIME_FIELDS_YMD)
goto done;
/* add time */
g_string_append_printf (s, "T%02u:%02u", gst_date_time_get_hour (datetime),
gst_date_time_get_minute (datetime));
if (datetime->fields == GST_DATE_TIME_FIELDS_YMD_HM)
goto add_timezone;
/* add seconds */
g_string_append_printf (s, ":%02u", gst_date_time_get_second (datetime));
/* add microseconds */
if (serialize_usecs) {
msecs = gst_date_time_get_microsecond (datetime);
if (msecs != 0) {
g_string_append_printf (s, ".%06u", msecs);
/* trim trailing 0s */
while (s->str[s->len - 1] == '0')
g_string_truncate (s, s->len - 1);
}
}
/* add timezone */
add_timezone:
gmt_offset = gst_date_time_get_time_zone_offset (datetime);
if (gmt_offset == 0) {
g_string_append_c (s, 'Z');
} else {
guint tzhour, tzminute;
tzhour = (guint) ABS (gmt_offset);
tzminute = (guint) ((ABS (gmt_offset) - tzhour) * 60);
g_string_append_c (s, (gmt_offset >= 0) ? '+' : '-');
g_string_append_printf (s, "%02u%02u", tzhour, tzminute);
}
done:
return g_string_free (s, FALSE);
}
/**
* gst_date_time_to_iso8601_string:
@ -687,30 +755,12 @@ gst_date_time_new (gfloat tzoffset, gint year, gint month, gint day, gint hour,
gchar *
gst_date_time_to_iso8601_string (GstDateTime * datetime)
{
gfloat gmt_offset;
g_return_val_if_fail (datetime != NULL, NULL);
switch (datetime->fields) {
case GST_DATE_TIME_FIELDS_Y:
return g_date_time_format (datetime->datetime, "%Y");
case GST_DATE_TIME_FIELDS_YM:
return g_date_time_format (datetime->datetime, "%Y-%m");
case GST_DATE_TIME_FIELDS_YMD:
return g_date_time_format (datetime->datetime, "%F");
case GST_DATE_TIME_FIELDS_YMD_HM:
gmt_offset = gst_date_time_get_time_zone_offset (datetime);
if (gmt_offset == 0)
return g_date_time_format (datetime->datetime, "%FT%RZ");
else
return g_date_time_format (datetime->datetime, "%FT%R%z");
case GST_DATE_TIME_FIELDS_YMD_HMS:
gmt_offset = gst_date_time_get_time_zone_offset (datetime);
if (gmt_offset == 0)
return g_date_time_format (datetime->datetime, "%FT%TZ");
else
return g_date_time_format (datetime->datetime, "%FT%T%z");
default:
return NULL;
}
if (datetime->fields == GST_DATE_TIME_FIELDS_INVALID)
return NULL;
return __gst_date_time_serialize (datetime, FALSE);
}
/**