Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Financial functions: part 3 #361

Merged
merged 12 commits into from
Dec 27, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions spreadsheet/formula/fndatetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -783,14 +783,18 @@ func YearFrac(ctx Context, ev Evaluator, args []Result) Result {
if err != nil {
return MakeErrorResult("incorrect end date")
}
return yearFracFromTime(startDate, endDate, basis)
yf, errResult := yearFracFromTime(startDate, endDate, basis)
if errResult.Type == ResultTypeError {
return errResult
}
return MakeNumberResult(yf)
}

func yearFrac(startDate, endDate float64, basis int) Result {
func yearFrac(startDate, endDate float64, basis int) (float64, Result) {
return yearFracFromTime(dateFromDays(startDate), dateFromDays(endDate), basis)
}

func yearFracFromTime(startDate, endDate time.Time, basis int) Result {
func yearFracFromTime(startDate, endDate time.Time, basis int) (float64, Result) {
gunnsth marked this conversation as resolved.
Show resolved Hide resolved
startDateS := startDate.Unix()
endDateS := endDate.Unix()
sy, sm, sd := startDate.Date()
Expand All @@ -806,27 +810,27 @@ func yearFracFromTime(startDate, endDate time.Time, basis int) Result {
} else if sd == 30 && ed == 31 {
ed = 30
}
return MakeNumberResult(float64(((ed + int(em) * 30 + ey * 360) - (sd + int(sm) * 30 + sy * 360))) / 360)
return float64(((ed + int(em) * 30 + ey * 360) - (sd + int(sm) * 30 + sy * 360))) / 360, MakeEmptyResult()
case 1:
var ylength = 365.0
if (sy == ey || ((sy + 1) == ey) && ((sm > em) || ((sm == em) && (sd >= ed)))) {
if ((sy == ey && isLeapYear(sy)) || feb29Between(startDate, endDate) || (em == 1 && ed == 29)) {
ylength = 366.0
}
return MakeNumberResult(daysBetween(startDateS, endDateS) / ylength)
return daysBetween(startDateS, endDateS) / ylength, MakeEmptyResult()
}
var years = float64((ey - sy) + 1)
var days = float64((makeDateS(ey + 1, time.January, 1) - makeDateS(sy, time.January, 1)) / 86400)
var average = days / years
return MakeNumberResult(daysBetween(startDateS, endDateS) / average)
return daysBetween(startDateS, endDateS) / average, MakeEmptyResult()
case 2:
return MakeNumberResult(daysBetween(startDateS, endDateS) / 360.0)
return daysBetween(startDateS, endDateS) / 360.0, MakeEmptyResult()
case 3:
return MakeNumberResult(daysBetween(startDateS, endDateS) / 365.0)
return daysBetween(startDateS, endDateS) / 365.0, MakeEmptyResult()
case 4:
return MakeNumberResult(float64(((ed + int(em) * 30 + ey * 360) - (sd + int(sm) * 30 + sy * 360))) / 360.0)
return float64(((ed + int(em) * 30 + ey * 360) - (sd + int(sm) * 30 + sy * 360))) / 360.0, MakeEmptyResult()
}
return MakeErrorResultType(ErrorTypeValue, "")
return 0, MakeErrorResult("")
}

func getDaysInYear(year, basis int) int {
Expand Down
Loading