From 4d716fa7edde76ed838aefcd8c5945bcfe3ba8f3 Mon Sep 17 00:00:00 2001 From: Stani Date: Sat, 21 Aug 2021 09:46:43 +0200 Subject: [PATCH] * This closes #1004, new fn: MONTH ref #65 --- calc.go | 32 ++++++++++++++++++++++++++++++++ calc_test.go | 9 +++++++++ 2 files changed, 41 insertions(+) diff --git a/calc.go b/calc.go index 9ff0535..9f3e25f 100644 --- a/calc.go +++ b/calc.go @@ -420,6 +420,7 @@ type formulaFuncs struct { // MINA // MIRR // MOD +// MONTH // MROUND // MULTINOMIAL // MUNIT @@ -6413,6 +6414,37 @@ func (fn *formulaFuncs) DAY(argsList *list.List) formulaArg { return newNumberFormulaArg(float64(timeFromExcelTime(num.Number, false).Day())) } +// MONTH function returns the month of a date represented by a serial number. +// The month is given as an integer, ranging from 1 (January) to 12 +// (December). The syntax of the function is: +// +// MONTH(serial_number) +// +func (fn *formulaFuncs) MONTH(argsList *list.List) formulaArg { + if argsList.Len() != 1 { + return newErrorFormulaArg(formulaErrorVALUE, "MONTH requires exactly 1 argument") + } + arg := argsList.Front().Value.(formulaArg) + num := arg.ToNumber() + if num.Type != ArgNumber { + dateString := strings.ToLower(arg.Value()) + if !isDateOnlyFmt(dateString) { + if _, _, _, _, _, err := strToTime(dateString); err.Type == ArgError { + return err + } + } + _, month, _, _, err := strToDate(dateString) + if err.Type == ArgError { + return err + } + return newNumberFormulaArg(float64(month)) + } + if num.Number < 0 { + return newErrorFormulaArg(formulaErrorNUM, "MONTH only accepts positive argument") + } + return newNumberFormulaArg(float64(timeFromExcelTime(num.Number, false).Month())) +} + // NOW function returns the current date and time. The function receives no // arguments and therefore. The syntax of the function is: // diff --git a/calc_test.go b/calc_test.go index 8deab93..cea9521 100644 --- a/calc_test.go +++ b/calc_test.go @@ -962,6 +962,9 @@ func TestCalcCellValue(t *testing.T) { "=DAY(\"3-February-2008\")": "3", "=DAY(\"01/25/20\")": "25", "=DAY(\"01/25/31\")": "25", + // MONTH + "=MONTH(42171)": "6", + "=MONTH(\"31-May-2015\")": "5", // Text Functions // CHAR "=CHAR(65)": "A", @@ -1981,6 +1984,12 @@ func TestCalcCellValue(t *testing.T) { "=DAY(\"3-January-9223372036854775808\")": "#VALUE!", "=DAY(\"9223372036854775808-January-1900\")": "#VALUE!", "=DAY(\"0-January-1900\")": "#VALUE!", + // MONTH + "=MONTH()": "MONTH requires exactly 1 argument", + "=MONTH(43891,43101)": "MONTH requires exactly 1 argument", + "=MONTH(-1)": "MONTH only accepts positive argument", + "=MONTH(\"text\")": "#VALUE!", + "=MONTH(\"January 25, 100\")": "#VALUE!", // NOW "=NOW(A1)": "NOW accepts no arguments", // TODAY