diff --git a/helper/timeutil/timeutil.go b/helper/timeutil/timeutil.go index 6904b67517..56a20615af 100644 --- a/helper/timeutil/timeutil.go +++ b/helper/timeutil/timeutil.go @@ -17,6 +17,20 @@ func StartOfPreviousMonth(t time.Time) time.Time { return time.Date(year, month, 1, 0, 0, 0, 0, t.Location()).AddDate(0, -1, 0) } +func StartOfDay(t time.Time) time.Time { + year, month, day := t.Date() + return time.Date(year, month, day, 0, 0, 0, 0, t.Location()) +} + +// IsCurrentDay checks if :t: is in the current day, as defined by :compare: +// generally, pass in time.Now().UTC() as :compare: +func IsCurrentDay(t, compare time.Time) bool { + thisDayStart := StartOfDay(compare) + queryDayStart := StartOfDay(t) + + return queryDayStart.Equal(thisDayStart) +} + func StartOfMonth(t time.Time) time.Time { year, month, _ := t.Date() return time.Date(year, month, 1, 0, 0, 0, 0, t.Location()) diff --git a/helper/timeutil/timeutil_test.go b/helper/timeutil/timeutil_test.go index f4098e4675..df14a6fd17 100644 --- a/helper/timeutil/timeutil_test.go +++ b/helper/timeutil/timeutil_test.go @@ -223,6 +223,47 @@ func TestTimeutil_IsCurrentMonth(t *testing.T) { } } +// TestTimeutil_IsCurrentDay checks if the test times equals the current day or not. +func TestTimeutil_IsCurrentDay(t *testing.T) { + now := time.Now() + testCases := []struct { + input time.Time + expected bool + }{ + { + input: now, + expected: true, + }, + { + input: StartOfDay(now).AddDate(0, 0, -1), + expected: false, + }, + { + input: StartOfDay(now).AddDate(-1, 0, 0), + expected: false, + }, + { + input: StartOfDay(now).Add(1 * time.Second), + expected: true, + }, + { + input: StartOfDay(now).Add(-1 * time.Second), + expected: false, + }, + { + input: StartOfDay(now).Add(86400), // a day is 86400 seconds + expected: true, + }, + } + + for _, tc := range testCases { + result := IsCurrentDay(tc.input, now) + if result != tc.expected { + t.Errorf("invalid result. expected %t for %v", tc.expected, tc.input) + } + } +} + func TestTimeUtil_ContiguousMonths(t *testing.T) { testCases := []struct { input []time.Time diff --git a/vault/census.go b/vault/census.go index 03a9790147..e492ac2c88 100644 --- a/vault/census.go +++ b/vault/census.go @@ -10,12 +10,13 @@ import "time" // CensusAgent is a stub for OSS type CensusReporter interface{} -func (c *Core) setupCensusManager() error { return nil } -func (c *Core) BillingStart() time.Time { return time.Time{} } -func (c *Core) AutomatedLicenseReportingEnabled() bool { return false } -func (c *Core) CensusAgent() CensusReporter { return nil } -func (c *Core) ReloadCensus() error { return nil } -func (c *Core) teardownCensusManager() error { return nil } -func (c *Core) StartManualCensusSnapshots() {} -func (c *Core) ManualLicenseReportingEnabled() bool { return false } -func (c *Core) ManualCensusSnapshotInterval() time.Duration { return time.Duration(0) } +func (c *Core) setupCensusManager() error { return nil } +func (c *Core) BillingStart() time.Time { return time.Time{} } +func (c *Core) AutomatedLicenseReportingEnabled() bool { return false } +func (c *Core) CensusAgent() CensusReporter { return nil } +func (c *Core) ReloadCensus() error { return nil } +func (c *Core) teardownCensusManager() error { return nil } +func (c *Core) StartManualCensusSnapshots() {} +func (c *Core) ManualLicenseReportingEnabled() bool { return false } +func (c *Core) ManualCensusSnapshotInterval() time.Duration { return time.Duration(0) } +func (c *Core) ManualCensusSnapshotRetentionTime() time.Duration { return time.Duration(0) } diff --git a/vault/core.go b/vault/core.go index 518b168aa3..5c631a5f8f 100644 --- a/vault/core.go +++ b/vault/core.go @@ -2419,7 +2419,7 @@ func (s standardUnsealStrategy) unseal(ctx context.Context, logger log.Logger, c if !c.IsDRSecondary() { if !c.perfStandby { if err := c.setupCensusManager(); err != nil { - logger.Error("skipping license reporting for nil agent", "error", err) + logger.Error("failed to instantiate the license reporting agent", "error", err) } }