From 5a6be2201dd4af0a57f634be90ed264d9ab4392d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim-Philipp=20M=C3=BCller?= Date: Wed, 27 Jun 2012 13:16:07 +0100 Subject: [PATCH] datetime: fix compare function Take into account that not all fields might be valid (though they are valid in the GDateTime structure). But we should just return unordered if the set fields don't match. Also, don't check microseconds when comparing datetimes, since we don't serialise those by default if they're available. This ensures date times are still regarded as equal after serialising+deserialising. --- gst/gst_private.h | 4 +++- gst/gstdatetime.c | 28 +++++++++++++++++++++++----- gst/gstvalue.c | 11 ++--------- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/gst/gst_private.h b/gst/gst_private.h index 971b80c..18bfa89 100644 --- a/gst/gst_private.h +++ b/gst/gst_private.h @@ -57,6 +57,8 @@ extern const char g_log_domain_gstreamer[]; /* for GstToc */ #include "gsttoc.h" +#include "gstdatetime.h" + G_BEGIN_DECLS /* used by gstparse.c and grammar.y */ @@ -144,7 +146,7 @@ void __gst_element_factory_add_interface (GstElementFactory * ((c) == '.')) /* This is only meant for internal uses */ -gint priv_gst_date_time_compare (gconstpointer dt1, gconstpointer dt2); +gint __gst_date_time_compare (const GstDateTime * dt1, const GstDateTime * dt2); #ifndef GST_DISABLE_REGISTRY /* Secret variable to initialise gst without registry cache */ diff --git a/gst/gstdatetime.c b/gst/gstdatetime.c index 39de650..a4f714b 100644 --- a/gst/gstdatetime.c +++ b/gst/gstdatetime.c @@ -24,6 +24,7 @@ #include "glib-compat-private.h" #include "gst_private.h" #include "gstdatetime.h" +#include "gstvalue.h" #include #include #include @@ -51,6 +52,8 @@ typedef enum GST_DATE_TIME_FIELDS_YMD, /* have year, month and day */ GST_DATE_TIME_FIELDS_YMD_HM, GST_DATE_TIME_FIELDS_YMD_HMS + /* Note: if we ever add more granularity here, e.g. for microsecs, + * the compare function will need updating */ } GstDateTimeFields; struct _GstDateTime @@ -543,11 +546,26 @@ gst_date_time_new_now_utc (void) } gint -priv_gst_date_time_compare (gconstpointer dt1, gconstpointer dt2) -{ - const GstDateTime *datetime1 = dt1; - const GstDateTime *datetime2 = dt2; - return g_date_time_compare (datetime1->datetime, datetime2->datetime); +__gst_date_time_compare (const GstDateTime * dt1, const GstDateTime * dt2) +{ + gint64 diff; + + /* we assume here that GST_DATE_TIME_FIELDS_YMD_HMS is the highest + * resolution, and ignore microsecond differences on purpose for now */ + if (dt1->fields != dt2->fields) + return GST_VALUE_UNORDERED; + + /* This will round down to nearest second, which is what we want. We're + * not comparing microseconds on purpose here, since we're not + * serialising them when doing new_utc_now() + to_string() */ + diff = + g_date_time_to_unix (dt1->datetime) - g_date_time_to_unix (dt2->datetime); + if (diff < 0) + return GST_VALUE_LESS_THAN; + else if (diff > 0) + return GST_VALUE_GREATER_THAN; + else + return GST_VALUE_EQUAL; } /** diff --git a/gst/gstvalue.c b/gst/gstvalue.c index ef4b4b7..8135d58 100644 --- a/gst/gstvalue.c +++ b/gst/gstvalue.c @@ -5240,7 +5240,6 @@ gst_value_compare_date_time (const GValue * value1, const GValue * value2) { const GstDateTime *date1 = (const GstDateTime *) g_value_get_boxed (value1); const GstDateTime *date2 = (const GstDateTime *) g_value_get_boxed (value2); - gint ret; if (date1 == date2) return GST_VALUE_EQUAL; @@ -5252,14 +5251,8 @@ gst_value_compare_date_time (const GValue * value1, const GValue * value2) return GST_VALUE_LESS_THAN; } - ret = priv_gst_date_time_compare (date1, date2); - - if (ret == 0) - return GST_VALUE_EQUAL; - else if (ret < 0) - return GST_VALUE_LESS_THAN; - else - return GST_VALUE_GREATER_THAN; + /* returns GST_VALUE_* */ + return __gst_date_time_compare (date1, date2); } static gchar * -- 2.7.4