datetime: Allow setting fractionary seconds in new_full()
authorEmmanuele Bassi <ebassi@linux.intel.com>
Wed, 15 Sep 2010 09:05:41 +0000 (10:05 +0100)
committerRyan Lortie <desrt@desrt.ca>
Fri, 17 Sep 2010 15:40:10 +0000 (11:40 -0400)
Otherwise we'll have to do:

  dt = g_date_time_new_full (Y, M, D, h, m, s, tz);
  tmp = g_date_time_add_usec (dt, usec);
  g_date_time_unref (dt);
  dt = tmp;

With its additional allocations.

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

glib/gdatetime.c
glib/gdatetime.h
glib/tests/gdatetime.c

index a51f28f1a1aa0d3a86ca4240aec866a46f29bd2b..634f73d15b0eac61b13f854c88f9790b77a3a240 100644 (file)
@@ -2111,7 +2111,7 @@ g_date_time_new_from_timeval (GTimeVal *tv)
  * @day: the day of the Gregorian month
  * @hour: the hour of the day
  * @minute: the minute of the hour
- * @second: the second of the minute
+ * @second: the second of the minute, with eventual fractionary parts
  * @time_zone: (allow-none): a #GTimeZone, or %NULL for UTC
  *
  * Creates a new #GDateTime using the date and times in the Gregorian
@@ -2130,14 +2130,14 @@ g_date_time_new_full (gint             year,
                       gint             day,
                       gint             hour,
                       gint             minute,
-                      gint             second,
+                      gdouble          second,
                       const GTimeZone *time_zone)
 {
   GDateTime *dt;
 
   g_return_val_if_fail (hour >= 0 && hour < 24, NULL);
   g_return_val_if_fail (minute >= 0 && minute < 60, NULL);
-  g_return_val_if_fail (second >= 0 && second <= 60, NULL);
+  g_return_val_if_fail (second >= 0.0 && second <= 60.0, NULL);
 
   dt = g_date_time_new ();
   dt->days = date_to_proleptic_gregorian (year, month, day);
index 4a7bf8f4f6276082a77f940cbcf276ba6ef84111..53d62d94b0ccca5a8821e45949fbc9022a951123 100644 (file)
@@ -116,7 +116,7 @@ GDateTime *           g_date_time_new_full               (gint             year,
                                                           gint             day,
                                                           gint             hour,
                                                           gint             minute,
-                                                          gint             second,
+                                                          gdouble          second,
                                                           const GTimeZone *time_zone);
 
 GDateTime *           g_date_time_ref                    (GDateTime       *datetime);
index 1c04459c06c2ed8a6a75a925badeeea05f8ab8ed..fe6808e3f23f239c46d416af3ee3017cd38a5839 100644 (file)
@@ -188,7 +188,6 @@ test_GDateTime_compare (void)
   dt2 = g_date_time_new_full (2000, 1, 1, 0, 0, 0, NULL);
   g_assert_cmpint (0, ==, g_date_time_compare (dt1, dt2));
   g_date_time_unref (dt2);
-
   g_date_time_unref (dt1);
 }
 
@@ -391,6 +390,11 @@ test_GDateTime_get_millisecond (void)
   dt = g_date_time_new_from_timeval (&tv);
   g_assert_cmpint ((tv.tv_usec / 1000), ==, g_date_time_get_millisecond (dt));
   g_date_time_unref (dt);
+
+  dt = g_date_time_new_full (2010, 9, 15, 12, 0, 0.1234, NULL);
+  g_assert_cmpint (123, ==, g_date_time_get_millisecond (dt));
+  g_assert_cmpint (123400, ==, g_date_time_get_microsecond (dt));
+  g_date_time_unref (dt);
 }
 
 static void