gstdatetime: Fix string serialization

Correctly serialize tzoffset as a gstvalue
This commit is contained in:
Thiago Santos 2010-09-27 19:29:24 -03:00
parent 29e23e9142
commit dd53349ad2
2 changed files with 14 additions and 12 deletions

View file

@ -4375,21 +4375,24 @@ gst_value_compare_date_time (const GValue * value1, const GValue * value2)
static gchar *
gst_value_serialize_date_time (const GValue * val)
{
const GstDateTime *date = (const GstDateTime *) g_value_get_boxed (val);
GstDateTime *date = (GstDateTime *) g_value_get_boxed (val);
gint offset;
gint tzhour, tzminute;
if (date == NULL)
return g_strdup ("null");
offset = gst_date_time_get_time_zone_offset (date);
tzhour = (gint) ABS (offset);
tzminute = (gint) ((ABS (offset) - tzhour) * 60);
return g_strdup_printf ("\"%04d-%02d-%02dT%02d:%02d:%02d.%06d"
"%c%02d%02d\"", gst_date_time_get_year (date),
gst_date_time_get_month (date), gst_date_time_get_day (date),
gst_date_time_get_hour (date), gst_date_time_get_minute (date),
gst_date_time_get_second (date), gst_date_time_get_microsecond (date),
offset >= 0 ? '+' : '-', (gint) ABS (offset) / 60,
(gint) ABS (offset) % 60);
offset >= 0 ? '+' : '-', tzhour, tzminute);
}
static gboolean
@ -4398,6 +4401,7 @@ gst_value_deserialize_date_time (GValue * dest, const gchar * s)
gint year, month, day, hour, minute, second, usecond;
gchar signal;
gint offset = 0;
gfloat tzoffset = 0;
gint ret;
if (!s || strcmp (s, "null") == 0) {
@ -4407,14 +4411,14 @@ gst_value_deserialize_date_time (GValue * dest, const gchar * s)
ret = sscanf (s, "%04d-%02d-%02dT%02d:%02d:%02d.%06d%c%04d",
&year, &month, &day, &hour, &minute, &second, &usecond, &signal, &offset);
if (ret >= 9) {
offset = (offset / 100) * 60 + (offset % 100);
tzoffset = (offset / 100) + ((offset % 100) / 60.0);
if (signal == '-')
offset = -offset;
tzoffset = -tzoffset;
} else
return FALSE;
g_value_take_boxed (dest, gst_date_time_new (year, month, day, hour,
minute, second, usecond, offset));
minute, second, usecond, tzoffset));
return TRUE;
}

View file

@ -2066,7 +2066,7 @@ GST_START_TEST (test_date_time)
str = NULL;
/* with timezone */
datetime = gst_date_time_new (2010, 6, 23, 7, 40, 10, 1, -3 * 60);
datetime = gst_date_time_new (2010, 6, 23, 7, 40, 10, 1, -3.0);
s = gst_structure_new ("media/x-type", "SOME_DATE_TIME_TAG",
GST_TYPE_DATE_TIME, datetime, NULL);
@ -2108,8 +2108,7 @@ GST_START_TEST (test_date_time)
fail_unless (gst_date_time_get_minute (datetime) == 40);
fail_unless (gst_date_time_get_second (datetime) == 10);
fail_unless (gst_date_time_get_microsecond (datetime) == 1);
fail_unless (gst_date_time_get_time_zone_offset (datetime) ==
((gint64) - 3 * 60));
fail_unless (gst_date_time_get_time_zone_offset (datetime) == -3);
gst_date_time_unref (datetime);
datetime = NULL;
@ -2123,7 +2122,7 @@ GST_START_TEST (test_date_time)
str = NULL;
/* with positive timezone */
datetime = gst_date_time_new (2010, 6, 23, 7, 40, 10, 1, 2 * 60);
datetime = gst_date_time_new (2010, 6, 23, 7, 40, 10, 1, 2);
s = gst_structure_new ("media/x-type", "SOME_DATE_TIME_TAG",
GST_TYPE_DATE_TIME, datetime, NULL);
@ -2165,8 +2164,7 @@ GST_START_TEST (test_date_time)
fail_unless (gst_date_time_get_minute (datetime) == 40);
fail_unless (gst_date_time_get_second (datetime) == 10);
fail_unless (gst_date_time_get_microsecond (datetime) == 1);
fail_unless (gst_date_time_get_time_zone_offset (datetime) ==
((gint64) 2 * 60));
fail_unless (gst_date_time_get_time_zone_offset (datetime) == 2);
gst_date_time_unref (datetime);
datetime = NULL;