Skip to content

Commit

Permalink
Days between helper added. (#219)
Browse files Browse the repository at this point in the history
# Describe Request

Days in between helper is added.

# Change Type

New feature.
  • Loading branch information
cinar authored Sep 14, 2024
1 parent 6615f03 commit f5a88d0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
15 changes: 15 additions & 0 deletions helper/days_between.go
Original file line number Diff line number Diff line change
@@ -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))
}
31 changes: 31 additions & 0 deletions helper/days_between_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit f5a88d0

Please sign in to comment.