From f5a88d040bdf292dfd78f4130888709e78c1a8cc Mon Sep 17 00:00:00 2001 From: Onur Cinar Date: Sat, 14 Sep 2024 09:36:39 -0700 Subject: [PATCH] Days between helper added. (#219) # Describe Request Days in between helper is added. # Change Type New feature. --- helper/days_between.go | 15 +++++++++++++++ helper/days_between_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 helper/days_between.go create mode 100644 helper/days_between_test.go diff --git a/helper/days_between.go b/helper/days_between.go new file mode 100644 index 0000000..e8e09d0 --- /dev/null +++ b/helper/days_between.go @@ -0,0 +1,15 @@ +// Copyright (c) 2021-2024 Onur Cinar. +// The source code is provided under GNU AGPLv3 License. +// https://github.com/cinar/indicator + +package helper + +import ( + "math" + "time" +) + +// DaysBetween calculates the days between the given two times. +func DaysBetween(from, to time.Time) int { + return int(math.Floor(to.Sub(from).Hours() / 24)) +} diff --git a/helper/days_between_test.go b/helper/days_between_test.go new file mode 100644 index 0000000..7e53816 --- /dev/null +++ b/helper/days_between_test.go @@ -0,0 +1,31 @@ +// Copyright (c) 2021-2024 Onur Cinar. +// The source code is provided under GNU AGPLv3 License. +// https://github.com/cinar/indicator + +package helper_test + +import ( + "testing" + "time" + + "github.com/cinar/indicator/v2/helper" +) + +func TestDaysBetween(t *testing.T) { + from := time.Date(2024, 9, 1, 0, 0, 0, 0, time.UTC) + to := time.Date(2024, 9, 15, 0, 0, 0, 0, time.UTC) + + actual := helper.DaysBetween(from, from) + expected := 0 + + if actual != expected { + t.Fatalf("actual %d expected %d", actual, expected) + } + + actual = helper.DaysBetween(from, to) + expected = 14 + + if actual != expected { + t.Fatalf("actual %d expected %d", actual, expected) + } +}