[glib/wip/rancell/iso8601-2: 1/2] GDateTime: Reject days outside of month limits



commit 6eda868d530ad688273e05491c8db29191f36a3f
Author: Robert Ancell <robert ancell canonical com>
Date:   Fri Aug 4 10:57:26 2017 +1200

    GDateTime: Reject days outside of month limits
    
    The previous code allowed February 30th.

 glib/gdatetime.c       |    2 +-
 glib/tests/gdatetime.c |   67 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 68 insertions(+), 1 deletions(-)
---
diff --git a/glib/gdatetime.c b/glib/gdatetime.c
index 745a32a..f3b27a0 100644
--- a/glib/gdatetime.c
+++ b/glib/gdatetime.c
@@ -963,7 +963,7 @@ g_date_time_new (GTimeZone *tz,
 
   if (year < 1 || year > 9999 ||
       month < 1 || month > 12 ||
-      day < 1 || day > 31 ||
+      day < 1 || day > days_in_months[GREGORIAN_LEAP (year)][month] ||
       hour < 0 || hour > 23 ||
       minute < 0 || minute > 59 ||
       seconds < 0.0 || seconds >= 60.0)
diff --git a/glib/tests/gdatetime.c b/glib/tests/gdatetime.c
index 4cb5a0a..34b25c1 100644
--- a/glib/tests/gdatetime.c
+++ b/glib/tests/gdatetime.c
@@ -752,6 +752,73 @@ test_GDateTime_new_full (void)
 #endif
   g_assert (!g_date_time_is_daylight_savings (dt));
   g_date_time_unref (dt);
+
+  /* Check month limits */
+  dt = g_date_time_new_utc (2016, 1, 31, 22, 10, 42);
+  ASSERT_DATE (dt, 2016, 1, 31);
+  g_date_time_unref (dt);
+  dt = g_date_time_new_utc (2016, 1, 32, 22, 10, 42);
+  g_assert_null (dt);
+  dt = g_date_time_new_utc (2016, 2, 29, 22, 10, 42);
+  ASSERT_DATE (dt, 2016, 2, 29);
+  g_date_time_unref (dt);
+  dt = g_date_time_new_utc (2016, 2, 30, 22, 10, 42);
+  g_assert_null (dt);
+  dt = g_date_time_new_utc (2017, 2, 28, 22, 10, 42);
+  ASSERT_DATE (dt, 2017, 2, 28);
+  g_date_time_unref (dt);
+  dt = g_date_time_new_utc (2017, 2, 29, 22, 10, 42);
+  g_assert_null (dt);
+  dt = g_date_time_new_utc (2016, 3, 31, 22, 10, 42);
+  ASSERT_DATE (dt, 2016, 3, 31);
+  g_date_time_unref (dt);
+  dt = g_date_time_new_utc (2016, 3, 32, 22, 10, 42);
+  g_assert_null (dt);
+  dt = g_date_time_new_utc (2016, 4, 30, 22, 10, 42);
+  ASSERT_DATE (dt, 2016, 4, 30);
+  g_date_time_unref (dt);
+  dt = g_date_time_new_utc (2016, 4, 31, 22, 10, 42);
+  g_assert_null (dt);
+  dt = g_date_time_new_utc (2016, 5, 31, 22, 10, 42);
+  ASSERT_DATE (dt, 2016, 5, 31);
+  g_date_time_unref (dt);
+  dt = g_date_time_new_utc (2016, 5, 32, 22, 10, 42);
+  g_assert_null (dt);
+  dt = g_date_time_new_utc (2016, 6, 30, 22, 10, 42);
+  ASSERT_DATE (dt, 2016, 6, 30);
+  g_date_time_unref (dt);
+  dt = g_date_time_new_utc (2016, 6, 31, 22, 10, 42);
+  g_assert_null (dt);
+  dt = g_date_time_new_utc (2016, 7, 31, 22, 10, 42);
+  ASSERT_DATE (dt, 2016, 7, 31);
+  g_date_time_unref (dt);
+  dt = g_date_time_new_utc (2016, 7, 32, 22, 10, 42);
+  g_assert_null (dt);
+  dt = g_date_time_new_utc (2016, 8, 31, 22, 10, 42);
+  ASSERT_DATE (dt, 2016, 8, 31);
+  g_date_time_unref (dt);
+  dt = g_date_time_new_utc (2016, 8, 32, 22, 10, 42);
+  g_assert_null (dt);
+  dt = g_date_time_new_utc (2016, 9, 30, 22, 10, 42);
+  ASSERT_DATE (dt, 2016, 9, 30);
+  g_date_time_unref (dt);
+  dt = g_date_time_new_utc (2016, 9, 31, 22, 10, 42);
+  g_assert_null (dt);
+  dt = g_date_time_new_utc (2016, 10, 31, 22, 10, 42);
+  ASSERT_DATE (dt, 2016, 10, 31);
+  g_date_time_unref (dt);
+  dt = g_date_time_new_utc (2016, 10, 32, 22, 10, 42);
+  g_assert_null (dt);
+  dt = g_date_time_new_utc (2016, 11, 30, 22, 10, 42);
+  ASSERT_DATE (dt, 2016, 11, 30);
+  g_date_time_unref (dt);
+  dt = g_date_time_new_utc (2016, 11, 31, 22, 10, 42);
+  g_assert_null (dt);
+  dt = g_date_time_new_utc (2016, 12, 31, 22, 10, 42);
+  ASSERT_DATE (dt, 2016, 12, 31);
+  g_date_time_unref (dt);
+  dt = g_date_time_new_utc (2016, 12, 32, 22, 10, 42);
+  g_assert_null (dt);
 }
 
 static void


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