ref #65, new formula function: TIME

This commit is contained in:
xuri 2021-10-16 16:05:50 +08:00
parent c64ce0f9ac
commit cf8766df83
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
3 changed files with 38 additions and 5 deletions

25
calc.go
View File

@ -489,6 +489,7 @@ type formulaFuncs struct {
// T // T
// TAN // TAN
// TANH // TANH
// TIME
// TODAY // TODAY
// TRANSPOSE // TRANSPOSE
// TRIM // TRIM
@ -6848,6 +6849,30 @@ func (fn *formulaFuncs) NOW(argsList *list.List) formulaArg {
return newNumberFormulaArg(25569.0 + float64(now.Unix()+int64(offset))/86400) return newNumberFormulaArg(25569.0 + float64(now.Unix()+int64(offset))/86400)
} }
// TIME function accepts three integer arguments representing hours, minutes
// and seconds, and returns an Excel time. I.e. the function returns the
// decimal value that represents the time in Excel. The syntax of the Time
// function is:
//
// TIME(hour,minute,second)
//
func (fn *formulaFuncs) TIME(argsList *list.List) formulaArg {
if argsList.Len() != 3 {
return newErrorFormulaArg(formulaErrorVALUE, "TIME requires 3 number arguments")
}
h := argsList.Front().Value.(formulaArg).ToNumber()
m := argsList.Front().Next().Value.(formulaArg).ToNumber()
s := argsList.Back().Value.(formulaArg).ToNumber()
if h.Type != ArgNumber || m.Type != ArgNumber || s.Type != ArgNumber {
return newErrorFormulaArg(formulaErrorVALUE, "TIME requires 3 number arguments")
}
t := (h.Number*3600 + m.Number*60 + s.Number) / 86400
if t < 0 {
return newErrorFormulaArg(formulaErrorNUM, formulaErrorNUM)
}
return newNumberFormulaArg(t)
}
// TODAY function returns the current date. The function has no arguments and // TODAY function returns the current date. The function has no arguments and
// therefore. The syntax of the function is: // therefore. The syntax of the function is:
// //

View File

@ -1016,6 +1016,10 @@ func TestCalcCellValue(t *testing.T) {
"=YEARFRAC(\"02/29/2000\", \"01/29/2001\",1)": "0.915300546448087", "=YEARFRAC(\"02/29/2000\", \"01/29/2001\",1)": "0.915300546448087",
"=YEARFRAC(\"02/29/2000\", \"03/29/2000\",1)": "0.0792349726775956", "=YEARFRAC(\"02/29/2000\", \"03/29/2000\",1)": "0.0792349726775956",
"=YEARFRAC(\"01/31/2000\", \"03/29/2000\",4)": "0.163888888888889", "=YEARFRAC(\"01/31/2000\", \"03/29/2000\",4)": "0.163888888888889",
// TIME
"=TIME(5,44,32)": "0.239259259259259",
"=TIME(\"5\",\"44\",\"32\")": "0.239259259259259",
"=TIME(0,0,73)": "0.000844907407407407",
// Text Functions // Text Functions
// CHAR // CHAR
"=CHAR(65)": "A", "=CHAR(65)": "A",
@ -2093,6 +2097,10 @@ func TestCalcCellValue(t *testing.T) {
"=YEARFRAC(42005,42094,\"\")": "strconv.ParseFloat: parsing \"\": invalid syntax", "=YEARFRAC(42005,42094,\"\")": "strconv.ParseFloat: parsing \"\": invalid syntax",
// NOW // NOW
"=NOW(A1)": "NOW accepts no arguments", "=NOW(A1)": "NOW accepts no arguments",
// TIME
"=TIME()": "TIME requires 3 number arguments",
"=TIME(\"\",0,0)": "TIME requires 3 number arguments",
"=TIME(0,0,-1)": "#NUM!",
// TODAY // TODAY
"=TODAY(A1)": "TODAY accepts no arguments", "=TODAY(A1)": "TODAY accepts no arguments",
// Text Functions // Text Functions