init: 1.0.0
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package calculateMonths
|
||||
|
||||
import "time"
|
||||
|
||||
// CalculateMonths calculates the number of months between startTime and endTime.
|
||||
// It rounds up to the next month if there are remaining days.
|
||||
func CalculateMonths(startTime, endTime time.Time) int8 {
|
||||
// Calculate the year and month difference
|
||||
years := endTime.Year() - startTime.Year()
|
||||
months := int8(years*12) + int8(endTime.Month()) - int8(startTime.Month())
|
||||
|
||||
// Always round up if endTime is not on the same or earlier day of the month
|
||||
if endTime.Day() > startTime.Day() || (endTime.Day() < startTime.Day() && endTime.After(startTime)) {
|
||||
months++
|
||||
}
|
||||
return months
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package calculateMonths
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestCalculateMonths(t *testing.T) {
|
||||
startTime, _ := time.Parse(time.DateTime, "2025-01-15 00:00:00")
|
||||
EndTime, _ := time.Parse(time.DateTime, "2025-05-15 00:00:00")
|
||||
months := CalculateMonths(startTime, EndTime)
|
||||
t.Log(months)
|
||||
}
|
||||
Reference in New Issue
Block a user