-
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Describe Request Days in between helper is added. # Change Type New feature.
- Loading branch information
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |