Skip to content

Commit

Permalink
Add auto-roll billing start date changes CE changes (#27656)
Browse files Browse the repository at this point in the history
* add NormalizeToYear function and test

* add ent changelog

* test name typo
  • Loading branch information
akshya96 authored Jul 2, 2024
1 parent c5c185f commit 01f78f5
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
3 changes: 3 additions & 0 deletions changelog/27656.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
license utilization reporting (enterprise): Auto-roll billing start date.
```
10 changes: 10 additions & 0 deletions helper/timeutil/timeutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,13 @@ func (_ DefaultClock) NewTicker(d time.Duration) *time.Ticker {
func (_ DefaultClock) NewTimer(d time.Duration) *time.Timer {
return time.NewTimer(d)
}

// NormalizeToYear returns date normalized to the latest date
// within one year of normal. Assumes the date argument is
// some date before normal.
func NormalizeToYear(date, normal time.Time) time.Time {
for date.AddDate(1, 0, 0).Compare(normal) <= 0 {
date = date.AddDate(1, 0, 0)
}
return date
}
93 changes: 93 additions & 0 deletions helper/timeutil/timeutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"reflect"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestTimeutil_StartOfPreviousMonth(t *testing.T) {
Expand Down Expand Up @@ -367,3 +369,94 @@ func TestTimeUtil_ParseTimeFromPath(t *testing.T) {
}
}
}

// TestTimeUtil_NormalizeToYear tests NormalizeToYear function which returns the normalized input date wrt to the normal.
func TestTimeUtil_NormalizeToYear(t *testing.T) {
testCases := []struct {
inputDate time.Time
normalDate time.Time
expectedNormalizedDate time.Time
}{
{
inputDate: time.Date(2024, 9, 29, 0, 0, 0, 0, time.UTC),
normalDate: time.Date(2024, 10, 1, 0, 0, 0, 0, time.UTC),
expectedNormalizedDate: time.Date(2024, 9, 29, 0, 0, 0, 0, time.UTC),
},
{
inputDate: time.Date(2024, 9, 29, 0, 0, 0, 0, time.UTC),
normalDate: time.Date(2025, 9, 29, 0, 0, 0, 0, time.UTC),
expectedNormalizedDate: time.Date(2025, 9, 29, 0, 0, 0, 0, time.UTC),
},
{
inputDate: time.Date(2024, 9, 29, 0, 0, 0, 0, time.UTC),
normalDate: time.Date(2025, 10, 1, 0, 0, 0, 0, time.UTC),
expectedNormalizedDate: time.Date(2025, 9, 29, 0, 0, 0, 0, time.UTC),
},
// inputDate more than 2 years prior to normal date
{
inputDate: time.Date(2022, 9, 29, 0, 0, 0, 0, time.UTC),
normalDate: time.Date(2024, 6, 15, 0, 0, 0, 0, time.UTC),
expectedNormalizedDate: time.Date(2023, 9, 29, 0, 0, 0, 0, time.UTC),
},
{
inputDate: time.Date(2022, 9, 29, 0, 0, 0, 0, time.UTC),
normalDate: time.Date(2024, 9, 28, 0, 0, 0, 0, time.UTC),
expectedNormalizedDate: time.Date(2023, 9, 29, 0, 0, 0, 0, time.UTC),
},
{
inputDate: time.Date(2022, 9, 29, 0, 0, 0, 0, time.UTC),
normalDate: time.Date(2024, 9, 29, 0, 0, 0, 0, time.UTC),
expectedNormalizedDate: time.Date(2024, 9, 29, 0, 0, 0, 0, time.UTC),
},
{
inputDate: time.Date(2022, 9, 29, 0, 0, 0, 0, time.UTC),
normalDate: time.Date(2024, 9, 30, 0, 0, 0, 0, time.UTC),
expectedNormalizedDate: time.Date(2024, 9, 29, 0, 0, 0, 0, time.UTC),
},
{
inputDate: time.Date(2020, 9, 29, 0, 0, 0, 0, time.UTC),
normalDate: time.Date(2024, 12, 1, 0, 0, 0, 0, time.UTC),
expectedNormalizedDate: time.Date(2024, 9, 29, 0, 0, 0, 0, time.UTC),
},
// leap year test cases
{
inputDate: time.Date(2020, 9, 29, 0, 0, 0, 0, time.UTC),
normalDate: time.Date(2024, 9, 28, 0, 0, 0, 0, time.UTC),
expectedNormalizedDate: time.Date(2023, 9, 29, 0, 0, 0, 0, time.UTC),
},
{
inputDate: time.Date(2024, 2, 29, 0, 0, 0, 0, time.UTC),
normalDate: time.Date(2025, 2, 28, 0, 0, 0, 0, time.UTC),
expectedNormalizedDate: time.Date(2024, 2, 29, 0, 0, 0, 0, time.UTC),
},
{
inputDate: time.Date(2024, 2, 29, 0, 0, 0, 0, time.UTC),
normalDate: time.Date(2025, 3, 1, 0, 0, 0, 0, time.UTC),
expectedNormalizedDate: time.Date(2025, 3, 1, 0, 0, 0, 0, time.UTC),
},
{
inputDate: time.Date(2024, 2, 29, 0, 0, 0, 0, time.UTC),
normalDate: time.Date(2025, 3, 2, 0, 0, 0, 0, time.UTC),
expectedNormalizedDate: time.Date(2025, 3, 1, 0, 0, 0, 0, time.UTC),
},
{
inputDate: time.Date(2024, 2, 29, 0, 0, 0, 0, time.UTC),
normalDate: time.Date(2028, 2, 28, 0, 0, 0, 0, time.UTC),
expectedNormalizedDate: time.Date(2027, 3, 1, 0, 0, 0, 0, time.UTC),
},
{
inputDate: time.Date(2024, 2, 29, 0, 0, 0, 0, time.UTC),
normalDate: time.Date(2028, 2, 29, 0, 0, 0, 0, time.UTC),
expectedNormalizedDate: time.Date(2027, 3, 1, 0, 0, 0, 0, time.UTC),
},
{
inputDate: time.Date(2024, 2, 29, 0, 0, 0, 0, time.UTC),
normalDate: time.Date(2028, 3, 1, 0, 0, 0, 0, time.UTC),
expectedNormalizedDate: time.Date(2028, 3, 1, 0, 0, 0, 0, time.UTC),
},
}
for _, tc := range testCases {
normalizedDate := NormalizeToYear(tc.inputDate, tc.normalDate)
require.Equal(t, tc.expectedNormalizedDate, normalizedDate)
}
}

0 comments on commit 01f78f5

Please sign in to comment.