2019-08-08 14:47:04 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package date_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-03-17 10:06:20 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-08-08 14:47:04 +01:00
|
|
|
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/date"
|
2019-08-08 14:47:04 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMonthBoundary(t *testing.T) {
|
2020-03-10 22:05:01 +00:00
|
|
|
now := time.Now()
|
2019-08-08 14:47:04 +01:00
|
|
|
|
|
|
|
start, end := date.MonthBoundary(now)
|
2020-03-10 22:05:01 +00:00
|
|
|
assert.Equal(t, start, time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location()))
|
|
|
|
assert.Equal(t, end, time.Date(now.Year(), now.Month()+1, 1, 0, 0, 0, -1, now.Location()))
|
2019-08-08 14:47:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDayBoundary(t *testing.T) {
|
2020-03-10 22:05:01 +00:00
|
|
|
now := time.Now()
|
2019-08-08 14:47:04 +01:00
|
|
|
|
|
|
|
start, end := date.DayBoundary(now)
|
2020-03-10 22:05:01 +00:00
|
|
|
assert.Equal(t, start, time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()))
|
|
|
|
assert.Equal(t, end, time.Date(now.Year(), now.Month(), now.Day()+1, 0, 0, 0, -1, now.Location()))
|
2019-08-08 14:47:04 +01:00
|
|
|
}
|
2020-03-17 10:06:20 +00:00
|
|
|
|
|
|
|
func TestPeriodToTime(t *testing.T) {
|
|
|
|
testCases := [...]struct {
|
|
|
|
period string
|
|
|
|
periodTime time.Time
|
|
|
|
}{
|
|
|
|
{"2020-01", time.Date(2020, 01, 01, 0, 0, 0, 0, &time.Location{})},
|
|
|
|
{"2020-02-01", time.Date(2020, 02, 01, 0, 0, 0, 0, &time.Location{})},
|
|
|
|
{"2019-11-04 14:14:14", time.Date(2019, 11, 01, 0, 0, 0, 0, &time.Location{})},
|
|
|
|
{"2020-03-04T15:04:05-0700", time.Date(2020, 03, 01, 0, 0, 0, 0, &time.Location{})},
|
|
|
|
{"2020-04gasgahsgnasjfasgjs", time.Date(2020, 04, 01, 0, 0, 0, 0, &time.Location{})},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
periodTime, err := date.PeriodToTime(tc.period)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, periodTime.String(), tc.periodTime.String())
|
|
|
|
}
|
|
|
|
}
|