[glib] gdate: add g_date_copy()



commit 5564ddef1247322bbef3c3a7dc79a12bbe1ab374
Author: Andrew Potter <agpotter gmail com>
Date:   Sun Jan 3 15:50:34 2016 -0800

    gdate: add g_date_copy()
    
    This will allow passing invalid GDates through GValues.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=760109

 docs/reference/glib/glib-sections.txt |    1 +
 glib/gdate.c                          |   29 +++++++++++++++++++++++++++++
 glib/gdate.h                          |    2 ++
 glib/tests/date.c                     |   27 +++++++++++++++++++++++++++
 gobject/gboxed.c                      |    8 +-------
 5 files changed, 60 insertions(+), 7 deletions(-)
---
diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt
index 7f7668c..e7a53ce 100644
--- a/docs/reference/glib/glib-sections.txt
+++ b/docs/reference/glib/glib-sections.txt
@@ -1551,6 +1551,7 @@ g_date_new_dmy
 g_date_new_julian
 g_date_clear
 g_date_free
+g_date_copy
 
 <SUBSECTION>
 g_date_set_day
diff --git a/glib/gdate.c b/glib/gdate.c
index 1519cf0..58cb75c 100644
--- a/glib/gdate.c
+++ b/glib/gdate.c
@@ -343,6 +343,35 @@ g_date_free (GDate *date)
 }
 
 /**
+ * g_date_copy:
+ * @date: a #GDate to copy
+ *
+ * Copies a GDate to a newly-allocated GDate. If the input was invalid
+ * (as determined by g_date_valid()), the invalid state will be copied
+ * as is into the new object.
+ *
+ * Returns: (transfer full): a newly-allocated #GDate initialized from @date
+ *
+ * Since: 2.56
+ */
+GDate *
+g_date_copy (const GDate *date)
+{
+  GDate *res;
+  g_return_val_if_fail (date != NULL, NULL);
+
+  if (g_date_valid (date))
+    res = g_date_new_julian (g_date_get_julian (date));
+  else
+    {
+      res = g_date_new ();
+      *res = *date;
+    }
+
+  return res;
+}
+
+/**
  * g_date_valid:
  * @date: a #GDate to check
  *
diff --git a/glib/gdate.h b/glib/gdate.h
index bc7e931..63feb35 100644
--- a/glib/gdate.h
+++ b/glib/gdate.h
@@ -127,6 +127,8 @@ GLIB_AVAILABLE_IN_ALL
 GDate*       g_date_new_julian            (guint32      julian_day);
 GLIB_AVAILABLE_IN_ALL
 void         g_date_free                  (GDate       *date);
+GLIB_AVAILABLE_IN_2_56
+GDate*       g_date_copy                  (const GDate *date);
 
 /* check g_date_valid() after doing an operation that might fail, like
  * _parse.  Almost all g_date operations are undefined on invalid
diff --git a/glib/tests/date.c b/glib/tests/date.c
index 012e707..73234d2 100644
--- a/glib/tests/date.c
+++ b/glib/tests/date.c
@@ -357,6 +357,32 @@ test_order (void)
   g_assert (g_date_compare (&d1, &d2) == 1);
 }
 
+static void
+test_copy (void)
+{
+  GDate *d;
+  GDate *c;
+
+  d = g_date_new ();
+  g_assert_false (g_date_valid (d));
+
+  c = g_date_copy (d);
+  g_assert_nonnull (c);
+  g_assert_false (g_date_valid (c));
+  g_date_free (c);
+
+  g_date_set_day (d, 10);
+
+  c = g_date_copy (d);
+  g_date_set_month (c, 1);
+  g_date_set_year (c, 2015);
+  g_assert_true (g_date_valid (c));
+  g_assert_cmpuint (g_date_get_day (c), ==, 10);
+  g_date_free (c);
+
+  g_date_free (d);
+}
+
 int
 main (int argc, char** argv)
 {
@@ -401,6 +427,7 @@ main (int argc, char** argv)
       g_test_add_data_func (path, GINT_TO_POINTER(check_years[i]), test_year);
       g_free (path);
     }
+  g_test_add_func ("/date/copy", test_copy);
 
   return g_test_run ();
 }
diff --git a/gobject/gboxed.c b/gobject/gboxed.c
index 97aa075..cc71664 100644
--- a/gobject/gboxed.c
+++ b/gobject/gboxed.c
@@ -109,12 +109,6 @@ _g_boxed_type_init (void)
   g_assert (type == G_TYPE_BOXED);
 }
 
-static GDate *
-gdate_copy (GDate *date)
-{
-  return g_date_new_julian (g_date_get_julian (date));
-}
-
 static GString *
 gstring_copy (GString *src_gstring)
 {
@@ -130,7 +124,7 @@ gstring_free (GString *gstring)
 G_DEFINE_BOXED_TYPE (GClosure, g_closure, g_closure_ref, g_closure_unref)
 G_DEFINE_BOXED_TYPE (GValue, g_value, value_copy, value_free)
 G_DEFINE_BOXED_TYPE (GValueArray, g_value_array, g_value_array_copy, g_value_array_free)
-G_DEFINE_BOXED_TYPE (GDate, g_date, gdate_copy, g_date_free)
+G_DEFINE_BOXED_TYPE (GDate, g_date, g_date_copy, g_date_free)
 /* the naming is a bit odd, but GString is obviously not G_TYPE_STRING */
 G_DEFINE_BOXED_TYPE (GString, g_gstring, gstring_copy, gstring_free)
 G_DEFINE_BOXED_TYPE (GHashTable, g_hash_table, g_hash_table_ref, g_hash_table_unref)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]