diff --git a/spreadsheet/formula/fndatetime.go b/spreadsheet/formula/fndatetime.go index 80635b11..34b47b87 100644 --- a/spreadsheet/formula/fndatetime.go +++ b/spreadsheet/formula/fndatetime.go @@ -8,6 +8,8 @@ package formula import ( + "fmt" + "github.com/davecgh/go-spew/spew" "regexp" "strconv" "strings" @@ -205,7 +207,7 @@ const nsPerDay = 86400000000000 func dateFromDays(days float64) time.Time { unix := int64((days - daysTo1970) * nsPerDay) - return time.Unix(0, unix) + return time.Unix(0, unix).UTC() } func daysFromDate(y, m, d int) float64 { @@ -786,17 +788,26 @@ func YearFrac(args []Result) Result { if args[1].Type != ResultTypeNumber { return MakeErrorResult("YEARFRAC requires end date to be number argument") } + spew.Dump(args) + spew.Dump(startDate) endDate := args[1].ValueNumber + spew.Dump(endDate) + spew.Dump(basis) yf, errResult := yearFrac(startDate, endDate, basis) if errResult.Type == ResultTypeError { return errResult } + spew.Dump(yf) return MakeNumberResult(yf) } -// yearFrac returns float64 fraction of the year and Result value which can be of ResultTypeError type if an error occurs or ResultTypeEmpty if doesn't. +// yearFrac returns float64 fraction of the year and Result value which can be of +// ResultTypeError type if an error occurs or ResultTypeEmpty if doesn't. func yearFrac(startDateF, endDateF float64, basis int) (float64, Result) { startDate, endDate := dateFromDays(startDateF), dateFromDays(endDateF) + fmt.Printf("yearFRac\n") + spew.Dump(startDate) + spew.Dump(endDate) startDateS := startDate.Unix() endDateS := endDate.Unix() if startDateS == endDateS { diff --git a/spreadsheet/formula/functions_test.go b/spreadsheet/formula/functions_test.go index f2776858..d7602432 100644 --- a/spreadsheet/formula/functions_test.go +++ b/spreadsheet/formula/functions_test.go @@ -1136,14 +1136,14 @@ func TestYearFrac(t *testing.T) { ss := spreadsheet.New() sheet := ss.AddSheet() - sheet.Cell("A1").SetTime(time.Date(1900, time.January, 1, 0, 0, 0, 0, time.UTC)) - sheet.Cell("A2").SetTime(time.Date(1900, time.January, 2, 0, 0, 0, 0, time.UTC)) - sheet.Cell("A3").SetTime(time.Date(1900, time.January, 31, 0, 0, 0, 0, time.UTC)) - sheet.Cell("A4").SetTime(time.Date(1900, time.March, 31, 0, 0, 0, 0, time.UTC)) - sheet.Cell("A5").SetTime(time.Date(1900, time.February, 1, 0, 0, 0, 0, time.UTC)) - sheet.Cell("A6").SetTime(time.Date(1904, time.January, 1, 0, 0, 0, 0, time.UTC)) - sheet.Cell("A7").SetTime(time.Date(1904, time.January, 2, 0, 0, 0, 0, time.UTC)) - sheet.Cell("A8").SetTime(time.Date(1905, time.January, 1, 0, 0, 0, 0, time.UTC)) + sheet.Cell("A1").SetTime(time.Date(1900, time.January, 1, 0, 0, 0, 0, time.Local)) + sheet.Cell("A2").SetTime(time.Date(1900, time.January, 2, 0, 0, 0, 0, time.Local)) + sheet.Cell("A3").SetTime(time.Date(1900, time.January, 31, 0, 0, 0, 0, time.Local)) + sheet.Cell("A4").SetTime(time.Date(1900, time.March, 31, 0, 0, 0, 0, time.Local)) + sheet.Cell("A5").SetTime(time.Date(1900, time.February, 1, 0, 0, 0, 0, time.Local)) + sheet.Cell("A6").SetTime(time.Date(1904, time.January, 1, 0, 0, 0, 0, time.Local)) + sheet.Cell("A7").SetTime(time.Date(1904, time.January, 2, 0, 0, 0, 0, time.Local)) + sheet.Cell("A8").SetTime(time.Date(1905, time.January, 1, 0, 0, 0, 0, time.Local)) sheet.Cell("A9").SetString("Hello") sheet.Cell("A10").SetString("World")