private/date: fix MonthsCountSince build issue
Change-Id: I58a70ea85f966dece4b3c75f54cfaa5238f9ecd9
This commit is contained in:
parent
cadb435d25
commit
4a98c9514c
@ -33,22 +33,16 @@ func PeriodToTime(period string) (_ time.Time, err error) {
|
|||||||
|
|
||||||
// MonthsCountSince calculates the months between now and the createdAtTime time.Time value passed.
|
// MonthsCountSince calculates the months between now and the createdAtTime time.Time value passed.
|
||||||
func MonthsCountSince(from time.Time) int {
|
func MonthsCountSince(from time.Time) int {
|
||||||
now := time.Now().UTC()
|
return MonthsBetweenDates(from, time.Now())
|
||||||
return MonthsBetweenDates(from, now)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MonthsBetweenDates calculates amount of months between two dates
|
// MonthsBetweenDates calculates amount of months between two dates
|
||||||
func MonthsBetweenDates(from time.Time, to time.Time) int {
|
func MonthsBetweenDates(from time.Time, to time.Time) int {
|
||||||
months := 0
|
// we need UTC here before its the only sensible way to say what day it is
|
||||||
month := from.Month()
|
y1, M1, _ := from.UTC().Date()
|
||||||
for from.Before(to) {
|
y2, M2, _ := to.UTC().Date()
|
||||||
from = from.Add(time.Hour * 24)
|
|
||||||
nextMonth := from.Month()
|
|
||||||
if nextMonth != month {
|
|
||||||
months++
|
|
||||||
}
|
|
||||||
month = nextMonth
|
|
||||||
}
|
|
||||||
|
|
||||||
|
months := ((y2 - y1) * 12) + int(M2) - int(M1)
|
||||||
|
//note that according to the tests, we ignore days of the month
|
||||||
return months
|
return months
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user