[gnumeric] Fix TIME and add ODF.TIME. [#676596]



commit 31183888f083e36a60fe1e7f254e5c35eb211f53
Author: Andreas J Guelzow <aguelzow pyrshep ca>
Date:   Tue May 22 23:58:40 2012 -0600

    Fix TIME and add ODF.TIME. [#676596]
    
    2012-05-22  Andreas J. Guelzow <aguelzow pyrshep ca>
    
    	* functions.c (help_time): adjust information
    	(gnumeric_time): use teh ECMA 376 description but do not trigger error
    	on arguments exceeding 32767
    	(help_odf_time): new
    	(gnumeric_odf_time): formerly gnumeric_time
    	(datetime_functions): add odf.time
    	* plugin.xml.in: add odf.time

 NEWS                          |    1 +
 plugins/fn-date/ChangeLog     |   10 ++++++++
 plugins/fn-date/functions.c   |   52 +++++++++++++++++++++++++++++++++++-----
 plugins/fn-date/plugin.xml.in |    1 +
 4 files changed, 57 insertions(+), 7 deletions(-)
---
diff --git a/NEWS b/NEWS
index 4c44d40..9fc9c57 100644
--- a/NEWS
+++ b/NEWS
@@ -18,6 +18,7 @@ Andreas:
 	* Improve ODF import/export of style conditions. [#676289, #676441]
 	* Fix object placement in ODF import. [#676339]
 	* Fix space handling on ODF import. [Part of #676535]
+	* Fix TIME and add ODF.TIME. [#676596]
 
 Jean:
 	* Fix graph series headers when a multiple selection is used. [#675913]
diff --git a/plugins/fn-date/ChangeLog b/plugins/fn-date/ChangeLog
index 58a6d05..8feed62 100644
--- a/plugins/fn-date/ChangeLog
+++ b/plugins/fn-date/ChangeLog
@@ -1,3 +1,13 @@
+2012-05-22  Andreas J. Guelzow <aguelzow pyrshep ca>
+
+	* functions.c (help_time): adjust information
+	(gnumeric_time): use teh ECMA 376 description but do not trigger error
+	on arguments exceeding 32767
+	(help_odf_time): new
+	(gnumeric_odf_time): formerly gnumeric_time
+	(datetime_functions): add odf.time
+	* plugin.xml.in: add odf.time
+
 2012-04-21  Morten Welinder <terra gnome org>
 
 	* Release 1.11.3
diff --git a/plugins/fn-date/functions.c b/plugins/fn-date/functions.c
index 8aa0cd8..57fdcec 100644
--- a/plugins/fn-date/functions.c
+++ b/plugins/fn-date/functions.c
@@ -236,12 +236,10 @@ datedif_opt_ym (GDate *gdate1, GDate *gdate2)
 static int
 datedif_opt_yd (GDate *gdate1, GDate *gdate2, int excel_compat)
 {
-	int day;
-
 	g_assert (g_date_valid (gdate1));
 	g_assert (g_date_valid (gdate2));
 
-	day = g_date_get_day (gdate1);
+	g_date_get_day (gdate1);
 
 	gnm_date_add_years (gdate1,
 			    go_date_g_years_between (gdate1, gdate2));
@@ -395,7 +393,7 @@ static GnmFuncHelp const help_today[] = {
 };
 
 static GnmValue *
-gnumeric_today (GnmFuncEvalInfo *ei, GnmValue const * const *argv)
+gnumeric_today (GnmFuncEvalInfo *ei, G_GNUC_UNUSED GnmValue const * const *argv)
 {
 	return make_date (value_new_int (go_date_timet_to_serial (time (NULL), DATE_CONV (ei->pos))));
 }
@@ -412,7 +410,7 @@ static GnmFuncHelp const help_now[] = {
 };
 
 static GnmValue *
-gnumeric_now (GnmFuncEvalInfo *ei, GnmValue const * const *argv)
+gnumeric_now (GnmFuncEvalInfo *ei, G_GNUC_UNUSED GnmValue const * const *argv)
 {
 	return value_new_float (go_date_timet_to_serial_raw (time (NULL), DATE_CONV (ei->pos)));
 }
@@ -424,10 +422,12 @@ static GnmFuncHelp const help_time[] = {
         { GNM_FUNC_HELP_ARG, F_("hour:hour of the day")},
         { GNM_FUNC_HELP_ARG, F_("minute:minute within the hour")},
         { GNM_FUNC_HELP_ARG, F_("second:second within the minute")},
-	{ GNM_FUNC_HELP_DESCRIPTION, F_("The TIME function computes the fractional day between midnight at the time given by @{hour}, @{minute}, and @{second}.") },
+	{ GNM_FUNC_HELP_DESCRIPTION, F_("The TIME function computes the fractional day after midnight at the time given by @{hour}, @{minute}, and @{second}.") },
+	{ GNM_FUNC_HELP_NOTE, F_("If any of @{hour}, @{minute}, and @{second} is negative, #NUM! is returned")},
         { GNM_FUNC_HELP_EXAMPLES, "=TIME(12,30,2)" },
+        { GNM_FUNC_HELP_EXAMPLES, "=TIME(25,100,18)" },
 	{ GNM_FUNC_HELP_EXCEL, F_("This function is Excel compatible.") },
-        { GNM_FUNC_HELP_SEEALSO, "HOUR,MINUTE,SECOND"},
+        { GNM_FUNC_HELP_SEEALSO, "ODF.TIME,HOUR,MINUTE,SECOND"},
 	{ GNM_FUNC_HELP_END }
 };
 
@@ -435,6 +435,40 @@ static GnmValue *
 gnumeric_time (GnmFuncEvalInfo *ei, GnmValue const * const *argv)
 {
 	gnm_float hours, minutes, seconds;
+	gnm_float time;
+
+	hours   = gnm_fmod (value_get_as_float (argv [0]), 24);
+	minutes = value_get_as_float (argv [1]);
+	seconds = value_get_as_float (argv [2]);
+
+	if (hours < 0 || minutes < 0 || seconds < 0)
+		return value_new_error_NUM (ei->pos);
+
+	time = (hours * 3600 + minutes * 60 + seconds) / DAY_SECONDS;
+	time -= gnm_fake_floor (time);
+
+	return make_date (value_new_float (time));
+}
+
+/***************************************************************************/
+
+static GnmFuncHelp const help_odf_time[] = {
+        { GNM_FUNC_HELP_NAME, F_("TIME:create a time serial value")},
+        { GNM_FUNC_HELP_ARG, F_("hour:hour")},
+        { GNM_FUNC_HELP_ARG, F_("minute:minute")},
+        { GNM_FUNC_HELP_ARG, F_("second:second")},
+	{ GNM_FUNC_HELP_DESCRIPTION, F_("The TIME function computes the time given by @{hour}, @{minute}, and @{second} as a fraction of a day.") },
+        { GNM_FUNC_HELP_EXAMPLES, "=ODF.TIME(12,30,2)" },
+        { GNM_FUNC_HELP_EXAMPLES, "=ODF.TIME(25,100,-18)" },
+	{ GNM_FUNC_HELP_ODF, F_("This function is OpenFormula compatible.") },
+        { GNM_FUNC_HELP_SEEALSO, "TIME,HOUR,MINUTE,SECOND"},
+	{ GNM_FUNC_HELP_END }
+};
+
+static GnmValue *
+gnumeric_odf_time (G_GNUC_UNUSED GnmFuncEvalInfo *ei, GnmValue const * const *argv)
+{
+	gnm_float hours, minutes, seconds;
 
 	hours   = value_get_as_float (argv [0]);
 	minutes = value_get_as_float (argv [1]);
@@ -1327,6 +1361,10 @@ GnmFuncDescriptor const datetime_functions[] = {
 	  gnumeric_now, NULL, NULL, NULL, NULL,
 	  GNM_FUNC_VOLATILE + GNM_FUNC_AUTO_TIME,
 	  GNM_FUNC_IMPL_STATUS_COMPLETE, GNM_FUNC_TEST_STATUS_BASIC },
+	{ "odf.time",        "fff",   help_odf_time,
+	  gnumeric_odf_time, NULL, NULL, NULL, NULL,
+	  GNM_FUNC_SIMPLE + GNM_FUNC_AUTO_TIME,
+	  GNM_FUNC_IMPL_STATUS_COMPLETE, GNM_FUNC_TEST_STATUS_NO_TESTSUITE },
 	{ "second",      "f",     help_second,
 	  gnumeric_second, NULL, NULL, NULL, NULL,
 	  GNM_FUNC_SIMPLE + GNM_FUNC_AUTO_UNITLESS,
diff --git a/plugins/fn-date/plugin.xml.in b/plugins/fn-date/plugin.xml.in
index b20dd2b..203ab4f 100644
--- a/plugins/fn-date/plugin.xml.in
+++ b/plugins/fn-date/plugin.xml.in
@@ -28,6 +28,7 @@
 				<function name="now"/>
 				<function name="second"/>
 				<function name="time"/>
+				<function name="odf.time"/>
 				<function name="timevalue"/>
 				<function name="today"/>
 				<function name="weekday"/>



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