ref #65: new formula function MINUTE

This commit is contained in:
xuri 2021-11-12 01:01:50 +08:00
parent 00eece4f53
commit 5de671f8bb
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
2 changed files with 54 additions and 0 deletions

43
calc.go
View File

@ -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:

View File

@ -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",