github.com/KinWaiYuen/client-go/v2@v2.5.4/util/misc_test.go (about) 1 // Copyright 2021 TiKV Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // NOTE: The code in this file is based on code from the 16 // TiDB project, licensed under the Apache License v 2.0 17 // 18 // https://github.com/pingcap/tidb/tree/cc5e161ac06827589c4966674597c137cc9e809c/store/tikv/util/misc_test.go 19 // 20 21 // Copyright 2021 PingCAP, Inc. 22 // 23 // Licensed under the Apache License, Version 2.0 (the "License"); 24 // you may not use this file except in compliance with the License. 25 // You may obtain a copy of the License at 26 // 27 // http://www.apache.org/licenses/LICENSE-2.0 28 // 29 // Unless required by applicable law or agreed to in writing, software 30 // distributed under the License is distributed on an "AS IS" BASIS, 31 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 32 // See the License for the specific language governing permissions and 33 // limitations under the License. 34 35 package util 36 37 import ( 38 "testing" 39 "time" 40 41 "github.com/stretchr/testify/assert" 42 "github.com/stretchr/testify/require" 43 ) 44 45 func TestCompatibleParseGCTime(t *testing.T) { 46 assert, require := assert.New(t), require.New(t) 47 48 values := []string{ 49 "20181218-19:53:37 +0800 CST", 50 "20181218-19:53:37 +0800 MST", 51 "20181218-19:53:37 +0800 FOO", 52 "20181218-19:53:37 +0800 +08", 53 "20181218-19:53:37 +0800", 54 "20181218-19:53:37 +0800 ", 55 "20181218-11:53:37 +0000", 56 } 57 58 invalidValues := []string{ 59 "", 60 " ", 61 "foo", 62 "20181218-11:53:37", 63 "20181218-19:53:37 +0800CST", 64 "20181218-19:53:37 +0800 FOO BAR", 65 "20181218-19:53:37 +0800FOOOOOOO BAR", 66 "20181218-19:53:37 ", 67 } 68 69 expectedTime := time.Date(2018, 12, 18, 11, 53, 37, 0, time.UTC) 70 expectedTimeFormatted := "20181218-19:53:37 +0800" 71 72 beijing, err := time.LoadLocation("Asia/Shanghai") 73 require.Nil(err) 74 75 for _, value := range values { 76 t, err := CompatibleParseGCTime(value) 77 assert.Nil(err) 78 assert.True(t.Equal(expectedTime)) 79 80 formatted := t.In(beijing).Format(GCTimeFormat) 81 assert.Equal(formatted, expectedTimeFormatted) 82 } 83 84 for _, value := range invalidValues { 85 _, err := CompatibleParseGCTime(value) 86 assert.NotNil(err) 87 } 88 }