datetime: fix compare function
authorTim-Philipp Müller <tim.muller@collabora.co.uk>
Wed, 27 Jun 2012 12:16:07 +0000 (13:16 +0100)
committerTim-Philipp Müller <tim.muller@collabora.co.uk>
Wed, 27 Jun 2012 12:51:33 +0000 (13:51 +0100)
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
gst/gstdatetime.c
gst/gstvalue.c

index 971b80c..18bfa89 100644 (file)
@@ -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 */
index 39de650..a4f714b 100644 (file)
@@ -24,6 +24,7 @@
 #include "glib-compat-private.h"
 #include "gst_private.h"
 #include "gstdatetime.h"
+#include "gstvalue.h"
 #include <glib.h>
 #include <math.h>
 #include <stdio.h>
@@ -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;
 }
 
 /**
index ef4b4b7..8135d58 100644 (file)
@@ -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 *