datetime: Use GDateTime if available
authorThiago Santos <thiago.sousa.santos@collabora.co.uk>
Mon, 27 Sep 2010 22:35:08 +0000 (19:35 -0300)
committerThiago Santos <thiago.sousa.santos@collabora.co.uk>
Wed, 13 Oct 2010 14:28:52 +0000 (11:28 -0300)
Use GDateTime internally on GstDateTime if glib already
provides it.

https://bugzilla.gnome.org/show_bug.cgi?id=628408

gst/glib-compat-private.h
gst/gstdatetime.c
gst/gstdatetime.h

index ec8b56d..5510d00 100644 (file)
@@ -8,7 +8,6 @@
 #ifndef __GLIB_COMPAT_PRIVATE_H__
 #define __GLIB_COMPAT_PRIVATE_H__
 
-#include "gst_private.h" /* for g_warning */
 #include <glib.h>
 
 G_BEGIN_DECLS
@@ -17,6 +16,10 @@ G_BEGIN_DECLS
 typedef struct stat GStatBuf;
 #endif
 
+#if GLIB_CHECK_VERSION(2,26,0)
+#define GLIB_HAS_GDATETIME
+#endif
+
 /* copies */
 
 /* adaptations */
index e02ea25..00cab13 100644 (file)
 #include "config.h"
 #endif
 
+#include "glib-compat-private.h"
 #include "gst_private.h"
 #include "gstdatetime.h"
+#include <glib.h>
+
+#ifndef GLIB_HAS_GDATETIME
 
 /**
  * SECTION:gstdatetime
@@ -437,44 +441,6 @@ gst_date_time_new_now_local_time (void)
   return datetime;
 }
 
-/**
- * gst_date_time_ref:
- * @datetime: a #GstDateTime
- *
- * Atomically increments the reference count of @datetime by one.
- *
- * Return value: the reference @datetime
- *
- * Since: 0.10.31
- */
-GstDateTime *
-gst_date_time_ref (GstDateTime * datetime)
-{
-  g_return_val_if_fail (datetime != NULL, NULL);
-  g_return_val_if_fail (datetime->ref_count > 0, NULL);
-  g_atomic_int_inc (&datetime->ref_count);
-  return datetime;
-}
-
-/**
- * gst_date_time_unref:
- * @datetime: a #GstDateTime
- *
- * Atomically decrements the reference count of @datetime by one.  When the
- * reference count reaches zero, the structure is freed.
- *
- * Since: 0.10.31
- */
-void
-gst_date_time_unref (GstDateTime * datetime)
-{
-  g_return_if_fail (datetime != NULL);
-  g_return_if_fail (datetime->ref_count > 0);
-
-  if (g_atomic_int_dec_and_test (&datetime->ref_count))
-    gst_date_time_free (datetime);
-}
-
 static GstDateTime *
 gst_date_time_copy (const GstDateTime * dt)
 {
@@ -589,3 +555,179 @@ done:
   gst_date_time_unref (b);
   return res;
 }
+
+#else
+
+struct _GstDateTime
+{
+  GDateTime *datetime;
+
+  volatile gint ref_count;
+};
+
+static GstDateTime *
+gst_date_time_new_from_gdatetime (GDateTime * dt)
+{
+  GstDateTime *gst_dt;
+
+  if (!dt)
+    return NULL;
+
+  gst_dt = g_slice_new (GstDateTime);
+  gst_dt->datetime = dt;
+  gst_dt->ref_count = 1;
+  return gst_dt;
+}
+
+gint
+gst_date_time_get_year (const GstDateTime * datetime)
+{
+  return g_date_time_get_year (datetime->datetime);
+}
+
+gint
+gst_date_time_get_month (const GstDateTime * datetime)
+{
+  return g_date_time_get_month (datetime->datetime);
+}
+
+gint
+gst_date_time_get_day (const GstDateTime * datetime)
+{
+  return g_date_time_get_day_of_month (datetime->datetime);
+}
+
+gint
+gst_date_time_get_hour (const GstDateTime * datetime)
+{
+  return g_date_time_get_hour (datetime->datetime);
+}
+
+gint
+gst_date_time_get_minute (const GstDateTime * datetime)
+{
+  return g_date_time_get_minute (datetime->datetime);
+}
+
+gint
+gst_date_time_get_second (const GstDateTime * datetime)
+{
+  return g_date_time_get_second (datetime->datetime);
+}
+
+gint
+gst_date_time_get_microsecond (const GstDateTime * datetime)
+{
+  return g_date_time_get_microsecond (datetime->datetime);
+}
+
+gfloat
+gst_date_time_get_time_zone_offset (const GstDateTime * datetime)
+{
+  return g_date_time_get_utc_offset (datetime->datetime) /
+      (G_USEC_PER_SEC * G_GINT64_CONSTANT (3600));
+}
+
+GstDateTime *
+gst_date_time_new_from_unix_epoch (gint64 secs)
+{
+  return
+      gst_date_time_new_from_gdatetime (g_date_time_new_from_unix_local (secs));
+}
+
+GstDateTime *
+gst_date_time_new_local_time (gint year, gint month, gint day, gint hour,
+    gint minute, gint second, gint microsecond)
+{
+  return gst_date_time_new_from_gdatetime (g_date_time_new_local (year, month,
+          day, hour, minute, second + (microsecond / 1000000.0)));
+}
+
+GstDateTime *
+gst_date_time_new_now_local_time (void)
+{
+  return gst_date_time_new_from_gdatetime (g_date_time_new_now_local ());
+}
+
+GstDateTime *
+gst_date_time_new_now_utc (void)
+{
+  return gst_date_time_new_from_gdatetime (g_date_time_new_now_utc ());
+}
+
+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);
+}
+
+GstDateTime *
+gst_date_time_new (gint year, gint month, gint day, gint hour, gint minute,
+    gint second, gint microsecond, gfloat tzoffset)
+{
+  gchar buf[6];
+  GTimeZone *tz;
+  GDateTime *dt;
+  gint tzhour, tzminute;
+
+  tzhour = (gint) ABS (tzoffset);
+  tzminute = (gint) ((ABS (tzoffset) - tzhour) * 60);
+
+  g_snprintf (buf, 6, "%c%02d%02d", tzoffset >= 0 ? '+' : '-', tzhour,
+      tzminute);
+
+  tz = g_time_zone_new (buf);
+  dt = g_date_time_new (tz, year, month, day, hour, minute,
+      second + (microsecond / 1000000.0));
+  g_time_zone_unref (tz);
+  return gst_date_time_new_from_gdatetime (dt);
+}
+
+static void
+gst_date_time_free (GstDateTime * datetime)
+{
+  g_date_time_unref (datetime->datetime);
+  g_slice_free (GstDateTime, datetime);
+}
+
+#endif
+
+/**
+ * gst_date_time_ref:
+ * @datetime: a #GstDateTime
+ *
+ * Atomically increments the reference count of @datetime by one.
+ *
+ * Return value: the reference @datetime
+ *
+ * Since: 0.10.31
+ */
+GstDateTime *
+gst_date_time_ref (GstDateTime * datetime)
+{
+  g_return_val_if_fail (datetime != NULL, NULL);
+  g_return_val_if_fail (datetime->ref_count > 0, NULL);
+  g_atomic_int_inc (&datetime->ref_count);
+  return datetime;
+}
+
+/**
+ * gst_date_time_unref:
+ * @datetime: a #GstDateTime
+ *
+ * Atomically decrements the reference count of @datetime by one.  When the
+ * reference count reaches zero, the structure is freed.
+ *
+ * Since: 0.10.31
+ */
+void
+gst_date_time_unref (GstDateTime * datetime)
+{
+  g_return_if_fail (datetime != NULL);
+  g_return_if_fail (datetime->ref_count > 0);
+
+  if (g_atomic_int_dec_and_test (&datetime->ref_count))
+    gst_date_time_free (datetime);
+}
index 26b65d4..5317a18 100644 (file)
@@ -28,7 +28,7 @@ G_BEGIN_DECLS
 /**
  * GstDateTime:
  *
- * Opaque, immutable, refcounted struct that store date, time and timezone
+ * Opaque, immutable, refcounted struct that stores date, time and timezone
  * information. It currently supports ranges from 0001-01-01 to
  * 9999-12-31 in the Gregorian proleptic calendar.
  *