github.com/binbinly/pkg@v0.0.11-0.20240321014439-f4fbf666eb0f/util/time_test.go (about) 1 package util 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestGet0DataTime(t *testing.T) { 11 now := time.Date(2020, 2, 18, 12, 13, 14, 123456789, time.UTC) 12 assert.Equal(t, "2020-02-18T00:00:00Z", BeginOfDay(now).Format(time.RFC3339Nano)) 13 assert.Equal(t, "2020-02-18T23:59:59.999999999Z", EndOfDay(now).Format(time.RFC3339Nano)) 14 assert.Equal(t, "2020-02-18T12:00:00Z", BeginOfHour(now).Format(time.RFC3339Nano)) 15 assert.Equal(t, "2020-02-18T12:59:59.999999999Z", EndOfHour(now).Format(time.RFC3339Nano)) 16 assert.Equal(t, "2020-02-18T12:13:00Z", BeginOfMinute(now).Format(time.RFC3339Nano)) 17 assert.Equal(t, "2020-02-18T12:13:59.999999999Z", EndOfMinute(now).Format(time.RFC3339Nano)) 18 assert.Equal(t, "2020-02-18T12:13:14Z", BeginOfSecond(now).Format(time.RFC3339Nano)) 19 assert.Equal(t, "2020-02-18T12:13:14.999999999Z", EndOfSecond(now).Format(time.RFC3339Nano)) 20 assert.Equal(t, "2020-02-17T00:00:00Z", BeginOfYesterday(now).Format(time.RFC3339Nano)) 21 assert.Equal(t, "2020-02-17T23:59:59.999999999Z", EndOfYesterday(now).Format(time.RFC3339Nano)) 22 assert.Equal(t, "2020-02-19T00:00:00Z", BeginOfTomorrow(now).Format(time.RFC3339Nano)) 23 assert.Equal(t, "2020-02-19T23:59:59.999999999Z", EndOfTomorrow(now).Format(time.RFC3339Nano)) 24 25 assert.Equal(t, "2020-02-17T00:00:00Z", BeginOfWeek(now).Format(time.RFC3339Nano)) 26 assert.Equal(t, "2020-02-23T23:59:59.999999999Z", EndOfWeek(now).Format(time.RFC3339Nano)) 27 assert.Equal(t, "2020-02-10T00:00:00Z", BeginOfLastWeek(now).Format(time.RFC3339Nano)) 28 assert.Equal(t, "2020-02-16T23:59:59.999999999Z", EndOfLastWeek(now).Format(time.RFC3339Nano)) 29 assert.Equal(t, "2020-02-24T00:00:00Z", BeginOfNextWeek(now).Format(time.RFC3339Nano)) 30 assert.Equal(t, "2020-03-01T23:59:59.999999999Z", EndOfNextWeek(now).Format(time.RFC3339Nano)) 31 32 assert.Equal(t, "2020-02-01T00:00:00Z", BeginOfMonth(now).Format(time.RFC3339Nano)) 33 assert.Equal(t, "2020-02-29T23:59:59.999999999Z", EndOfMonth(now).Format(time.RFC3339Nano)) 34 assert.Equal(t, "2020-01-01T00:00:00Z", BeginOfLastMonth(now).Format(time.RFC3339Nano)) 35 assert.Equal(t, "2020-01-31T23:59:59.999999999Z", EndOfLastMonth(now).Format(time.RFC3339Nano)) 36 assert.Equal(t, "2020-03-01T00:00:00Z", BeginOfNextMonth(now).Format(time.RFC3339Nano)) 37 assert.Equal(t, "2020-03-31T23:59:59.999999999Z", EndOfNextMonth(now).Format(time.RFC3339Nano)) 38 assert.Equal(t, 29, GetMonthDays(now)) 39 40 assert.Equal(t, "2020-01-01T00:00:00Z", BeginOfYear(now).Format(time.RFC3339Nano)) 41 assert.Equal(t, "2020-12-31T23:59:59.999999999Z", EndOfYear(now).Format(time.RFC3339Nano)) 42 43 now = time.Date(2019, 1, 31, 0, 0, 0, 0, time.UTC) 44 assert.Equal(t, "2018-12-01T00:00:00Z", BeginOfLastMonth(now).Format(time.RFC3339Nano)) 45 assert.Equal(t, "2018-12-31T23:59:59.999999999Z", EndOfLastMonth(now).Format(time.RFC3339Nano)) 46 assert.Equal(t, "2019-02-01T00:00:00Z", BeginOfNextMonth(now).Format(time.RFC3339Nano)) 47 assert.Equal(t, "2019-02-28T23:59:59.999999999Z", EndOfNextMonth(now).Format(time.RFC3339Nano)) 48 } 49 50 func TestDaysInYear(t *testing.T) { 51 assert.Equal(t, 365, DaysInYear(1900)) 52 assert.Equal(t, 365, DaysInYear(2021)) 53 assert.Equal(t, 366, DaysInYear(2000)) 54 assert.Equal(t, 366, DaysInYear(2020)) 55 }