gdate: add g_date_copy()

This will allow passing invalid GDates through GValues.

https://bugzilla.gnome.org/show_bug.cgi?id=760109
This commit is contained in:
Andrew Potter 2016-01-03 15:50:34 -08:00 committed by Philip Withnall
parent 652a47d1d1
commit 5564ddef12
5 changed files with 60 additions and 7 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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 ();
}

View File

@ -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)