diff options
author | xuri <xuri.me@gmail.com> | 2021-11-12 01:01:50 +0800 |
---|---|---|
committer | xuri <xuri.me@gmail.com> | 2021-11-12 01:01:50 +0800 |
commit | 5de671f8bb8a4d8951d5511c3bd1745fc46ce842 (patch) | |
tree | 43e7b5b75bf3accf1cc70c61294adb1196e36043 | |
parent | 00eece4f53f034a8dff009330ca45f1012e64ee3 (diff) |
ref #65: new formula function MINUTE
-rw-r--r-- | calc.go | 43 | ||||
-rw-r--r-- | calc_test.go | 11 |
2 files changed, 54 insertions, 0 deletions
@@ -471,6 +471,7 @@ type formulaFuncs struct { // MIDB // MIN // MINA +// MINUTE // MIRR // MOD // MONTH @@ -7112,6 +7113,17 @@ func isDateOnlyFmt(dateString string) bool { return false } +// isTimeOnlyFmt check if the given string matches time-only format regular expressions. +func isTimeOnlyFmt(timeString string) bool { + for _, tf := range timeFormats { + submatch := tf.FindStringSubmatch(timeString) + if len(submatch) > 1 { + return true + } + } + return false +} + // strToTimePatternHandler1 parse and convert the given string in pattern // hh to the time. func strToTimePatternHandler1(submatch []string) (h, m int, s float64, err error) { @@ -7461,6 +7473,37 @@ func (fn *formulaFuncs) ISOWEEKNUM(argsList *list.List) formulaArg { return newNumberFormulaArg(float64(weeknum)) } +// MINUTE function returns an integer representing the minute component of a +// supplied Excel time. The syntax of the function is: +// +// MINUTE(serial_number) +// +func (fn *formulaFuncs) MINUTE(argsList *list.List) formulaArg { + if argsList.Len() != 1 { + return newErrorFormulaArg(formulaErrorVALUE, "MINUTE requires exactly 1 argument") + } + date := argsList.Front().Value.(formulaArg) + num := date.ToNumber() + if num.Type != ArgNumber { + timeString := strings.ToLower(date.Value()) + if !isTimeOnlyFmt(timeString) { + _, _, _, _, err := strToDate(timeString) + if err.Type == ArgError { + return err + } + } + _, m, _, _, _, err := strToTime(timeString) + if err.Type == ArgError { + return err + } + return newNumberFormulaArg(float64(m)) + } + if num.Number < 0 { + return newErrorFormulaArg(formulaErrorNUM, "MINUTE only accepts positive argument") + } + return newNumberFormulaArg(float64(timeFromExcelTime(num.Number, false).Minute())) +} + // 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: diff --git a/calc_test.go b/calc_test.go index 6420715..90138a1 100644 --- a/calc_test.go +++ b/calc_test.go @@ -1105,6 +1105,12 @@ func TestCalcCellValue(t *testing.T) { "=ISOWEEKNUM(\"42370\")": "53", "=ISOWEEKNUM(\"01/01/2005\")": "53", "=ISOWEEKNUM(\"02/02/2005\")": "5", + // MINUTE + "=MINUTE(1)": "0", + "=MINUTE(0.04)": "57", + "=MINUTE(\"0.04\")": "57", + "=MINUTE(\"13:35:55\")": "35", + "=MINUTE(\"12/09/2015 08:55\")": "55", // MONTH "=MONTH(42171)": "6", "=MONTH(\"31-May-2015\")": "5", @@ -2438,6 +2444,11 @@ func TestCalcCellValue(t *testing.T) { "=ISOWEEKNUM(\"\")": "#VALUE!", "=ISOWEEKNUM(\"January 25, 100\")": "#VALUE!", "=ISOWEEKNUM(-1)": "#NUM!", + // MINUTE + "=MINUTE()": "MINUTE requires exactly 1 argument", + "=MINUTE(-1)": "MINUTE only accepts positive argument", + "=MINUTE(\"\")": "#VALUE!", + "=MINUTE(\"13:60:55\")": "#VALUE!", // MONTH "=MONTH()": "MONTH requires exactly 1 argument", "=MONTH(0,0)": "MONTH requires exactly 1 argument", |