123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- // Copyright 2022-2023 EMQ Technologies Co., Ltd.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package cast
- import (
- "testing"
- "time"
- "github.com/stretchr/testify/assert"
- )
- func TestTimeToAndFromMilli(t *testing.T) {
- tests := []struct {
- m int64
- t time.Time
- }{
- {int64(1579140864913), time.Date(2020, time.January, 16, 2, 14, 24, 913000000, time.UTC)},
- {int64(4913), time.Date(1970, time.January, 1, 0, 0, 4, 913000000, time.UTC)},
- {int64(2579140864913), time.Date(2051, time.September, 24, 4, 1, 4, 913000000, time.UTC)},
- {int64(-1579140864913), time.Date(1919, time.December, 17, 21, 45, 35, 87000000, time.UTC)},
- }
- for i, tt := range tests {
- time := TimeFromUnixMilli(tt.m)
- assert.Equal(t, tt.t, time, "%d time from milli result mismatch:\n\nexp=%#v\n\ngot=%#v", i, tt.t, time)
- milli := TimeToUnixMilli(tt.t)
- assert.Equal(t, tt.m, milli, "%d time to milli result mismatch:\n\nexp=%#v\n\ngot=%#v", i, tt.m, milli)
- }
- }
- func TestFormatTime(t *testing.T) {
- date := time.Date(2020, time.January, 16, 2, 14, 24, 913000000, time.UTC)
- tests := []struct {
- format string
- want string
- wantErr bool
- }{
- {
- format: "YYYY-MM-dd HH:mm:ssSSS",
- want: "2020-01-16 02:14:24.913",
- wantErr: false,
- },
- {
- format: "YYYY-MM-dd T HH:mm:ss",
- want: "2020-01-16 T 02:14:24",
- wantErr: false,
- },
- {
- format: "YYY",
- want: "2020",
- wantErr: true,
- },
- }
- for _, tt := range tests {
- got, err := FormatTime(date, tt.format)
- if tt.wantErr {
- assert.Error(t, err)
- continue
- }
- assert.NoError(t, err)
- assert.Equal(t, tt.want, got)
- }
- }
- func TestParseTime(t *testing.T) {
- tests := []struct {
- d time.Time
- t string
- f string
- wantErr bool
- }{
- {
- time.Date(2020, time.January, 16, 2, 14, 24, 913000000, time.UTC),
- "2020-01-16 02:14:24.913",
- "YYYY-MM-dd HH:mm:ssSSS",
- false,
- },
- {
- time.Date(2020, time.January, 16, 2, 14, 24, 0, time.UTC),
- "2020-01-16 02:14:24",
- "YYYY-MM-dd HH:mm:ss",
- false,
- },
- {
- time.Date(2020, time.January, 16, 2, 14, 24, 0, time.UTC),
- "2020-01-16 02:14:24",
- "",
- false,
- },
- {
- time.Time{},
- "2020",
- "YYY",
- true,
- },
- }
- for _, tt := range tests {
- date, err := ParseTime(tt.t, tt.f)
- if tt.wantErr {
- assert.Error(t, err)
- continue
- }
- assert.NoError(t, err)
- assert.Equal(t, tt.d, date)
- }
- }
- func TestInterfaceToTime(t *testing.T) {
- tests := []struct {
- i interface{}
- f string
- want time.Time
- wantErr bool
- }{
- {
- "2022-04-13 06:22:32.233",
- "YYYY-MM-dd HH:mm:ssSSS",
- time.Date(2022, time.April, 13, 6, 22, 32, 233000000, time.UTC),
- false,
- },
- {
- "2022-04-13 6:22:32.2",
- "YYYY-MM-dd h:m:sS",
- time.Date(2022, time.April, 13, 6, 22, 32, 200000000, time.UTC),
- false,
- },
- {
- "2022-04-13 6:22:32.23",
- "YYYY-MM-dd h:m:sSS",
- time.Date(2022, time.April, 13, 6, 22, 32, 230000000, time.UTC),
- false,
- },
- {
- "2022-04-13 Wed 06:22:32.233",
- "YYYY-MM-dd EEE HH:m:ssSSS",
- time.Date(2022, time.April, 13, 6, 22, 32, 233000000, time.UTC),
- false,
- },
- {
- "2022-04-13 Wednesday 06:22:32.233",
- "YYYY-MM-dd EEEE HH:m:ssSSS",
- time.Date(2022, time.April, 13, 6, 22, 32, 233000000, time.UTC),
- false,
- },
- {
- 1649830952233,
- "YYYY-MM-dd HH:mm:ssSSS",
- time.Date(2022, time.April, 13, 6, 22, 32, 233000000, time.UTC),
- false,
- },
- {
- int64(1649830952233),
- "YYYY-MM-dd HH:mm:ssSSS",
- time.Date(2022, time.April, 13, 6, 22, 32, 233000000, time.UTC),
- false,
- },
- {
- float64(1649830952233),
- "YYYY-MM-dd HH:mm:ssSSS",
- time.Date(2022, time.April, 13, 6, 22, 32, 233000000, time.UTC),
- false,
- },
- {
- time.Date(2022, time.April, 13, 6, 22, 32, 233000000, time.UTC),
- "YYYY-MM-dd HH:mm:ssSSS",
- time.Date(2022, time.April, 13, 6, 22, 32, 233000000, time.UTC),
- false,
- },
- {
- "2022-04-13 06:22:32.233",
- "YYYy-MM-dd HH:mm:ssSSS",
- time.Date(2022, time.April, 13, 6, 22, 32, 233000000, time.UTC),
- true,
- },
- {
- struct{}{},
- "YYYY-MM-dd HH:mm:ssSSS",
- time.Date(2022, time.April, 13, 6, 22, 32, 233000000, time.UTC),
- true,
- },
- }
- for _, tt := range tests {
- got, err := InterfaceToTime(tt.i, tt.f)
- if tt.wantErr {
- assert.Error(t, err)
- continue
- }
- assert.NoError(t, err)
- assert.Equal(t, tt.want, got)
- }
- }
- func TestInterfaceToUnixMilli(t *testing.T) {
- tests := []struct {
- i interface{}
- f string
- want int64
- wantErr bool
- }{
- {
- "2022-04-13 06:22:32.233",
- "YYYY-MM-dd HH:mm:ssSSS",
- 1649830952233,
- false,
- },
- {
- 1649830952233,
- "YYYY-MM-dd HH:mm:ssSSS",
- 1649830952233,
- false,
- },
- {
- int64(1649830952233),
- "YYYY-MM-dd HH:mm:ssSSS",
- 1649830952233,
- false,
- },
- {
- float64(1649830952233),
- "YYYY-MM-dd HH:mm:ssSSS",
- 1649830952233,
- false,
- },
- {
- time.Date(2022, time.April, 13, 6, 22, 32, 233000000, time.UTC),
- "YYYY-MM-dd HH:mm:ssSSS",
- 1649830952233,
- false,
- },
- {
- "2022-04-13 06:22:32.233",
- "YYYy-MM-dd HH:mm:ssSSS",
- 1649830952233,
- true,
- },
- {
- struct{}{},
- "YYYY-MM-dd HH:mm:ssSSS",
- 1649830952233,
- true,
- },
- }
- for _, tt := range tests {
- got, err := InterfaceToUnixMilli(tt.i, tt.f)
- if tt.wantErr {
- assert.Error(t, err)
- continue
- }
- assert.NoError(t, err)
- assert.Equal(t, tt.want, got)
- }
- }
|