github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/unixtimestamp/unixtimestamp_test.go (about)

     1  // Copyright 2022 Matrix Origin
     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  package unixtimestamp
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/container/types"
    23  )
    24  
    25  func mustTimestamp(s string) types.Timestamp {
    26  	timestamp, err := types.ParseTimestamp(time.Local, s, 6)
    27  	if err != nil {
    28  		panic("bad datetime")
    29  	}
    30  	return timestamp
    31  }
    32  
    33  func TestUnixTimestamp(t *testing.T) {
    34  
    35  	xs := make([]types.Timestamp, 3)
    36  	rs := make([]int64, 3)
    37  	want := make([]int64, 3)
    38  	for i, timestr := range []string{"2022-01-01 22:23:00", "2022-01-02 22:23:00", "2022-01-03 22:23:00"} {
    39  		xs[i] = mustTimestamp(timestr)
    40  		goLcoalTime, _ := time.ParseInLocation("2006-01-02 15:04:05", timestr, time.Local)
    41  		want[i] = goLcoalTime.Unix()
    42  	}
    43  
    44  	got := unixTimestampToInt(xs, rs)
    45  	if !reflect.DeepEqual(got, want) {
    46  		t.Errorf("unixtimestamp() want %v but got %v", want, got)
    47  	}
    48  }