From 5564ddef1247322bbef3c3a7dc79a12bbe1ab374 Mon Sep 17 00:00:00 2001 From: Andrew Potter Date: Sun, 3 Jan 2016 15:50:34 -0800 Subject: [PATCH] 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 7f7668c4a..e7a53ceff 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 g_date_set_day diff --git a/glib/gdate.c b/glib/gdate.c index 1519cf024..58cb75cb4 100644 --- a/glib/gdate.c +++ b/glib/gdate.c @@ -342,6 +342,35 @@ g_date_free (GDate *date) g_free (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 bc7e93120..63feb3535 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 012e70713..73234d2d1 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 97aa07587..cc7166456 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)