From 4a98c9514c2f4c11a6cc7570a2c88e6f9e5794f6 Mon Sep 17 00:00:00 2001 From: Bill Thorp Date: Tue, 30 Jun 2020 17:26:31 -0400 Subject: [PATCH] private/date: fix MonthsCountSince build issue Change-Id: I58a70ea85f966dece4b3c75f54cfaa5238f9ecd9 --- private/date/utils.go | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/private/date/utils.go b/private/date/utils.go index 27e06e81d..e2da7fa1d 100644 --- a/private/date/utils.go +++ b/private/date/utils.go @@ -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. func MonthsCountSince(from time.Time) int { - now := time.Now().UTC() - return MonthsBetweenDates(from, now) + return MonthsBetweenDates(from, time.Now()) } // MonthsBetweenDates calculates amount of months between two dates func MonthsBetweenDates(from time.Time, to time.Time) int { - months := 0 - month := from.Month() - for from.Before(to) { - from = from.Add(time.Hour * 24) - nextMonth := from.Month() - if nextMonth != month { - months++ - } - month = nextMonth - } + // we need UTC here before its the only sensible way to say what day it is + y1, M1, _ := from.UTC().Date() + y2, M2, _ := to.UTC().Date() + months := ((y2 - y1) * 12) + int(M2) - int(M1) + //note that according to the tests, we ignore days of the month return months }