storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/s3select/sql/timestampfuncs_test.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2019 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package sql
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  )
    23  
    24  func TestParseAndDisplaySQLTimestamp(t *testing.T) {
    25  	beijing := time.FixedZone("", int((8 * time.Hour).Seconds()))
    26  	fakeLosAngeles := time.FixedZone("", -int((8 * time.Hour).Seconds()))
    27  	cases := []struct {
    28  		s string
    29  		t time.Time
    30  	}{
    31  		{"2010T", time.Date(2010, 1, 1, 0, 0, 0, 0, time.UTC)},
    32  		{"2010-02T", time.Date(2010, 2, 1, 0, 0, 0, 0, time.UTC)},
    33  		{"2010-02-03T", time.Date(2010, 2, 3, 0, 0, 0, 0, time.UTC)},
    34  		{"2010-02-03T04:11Z", time.Date(2010, 2, 3, 4, 11, 0, 0, time.UTC)},
    35  		{"2010-02-03T04:11:30Z", time.Date(2010, 2, 3, 4, 11, 30, 0, time.UTC)},
    36  		{"2010-02-03T04:11:30.23Z", time.Date(2010, 2, 3, 4, 11, 30, 230000000, time.UTC)},
    37  		{"2010-02-03T04:11+08:00", time.Date(2010, 2, 3, 4, 11, 0, 0, beijing)},
    38  		{"2010-02-03T04:11:30+08:00", time.Date(2010, 2, 3, 4, 11, 30, 0, beijing)},
    39  		{"2010-02-03T04:11:30.23+08:00", time.Date(2010, 2, 3, 4, 11, 30, 230000000, beijing)},
    40  		{"2010-02-03T04:11:30-08:00", time.Date(2010, 2, 3, 4, 11, 30, 0, fakeLosAngeles)},
    41  		{"2010-02-03T04:11:30.23-08:00", time.Date(2010, 2, 3, 4, 11, 30, 230000000, fakeLosAngeles)},
    42  	}
    43  	for i, tc := range cases {
    44  		tval, err := parseSQLTimestamp(tc.s)
    45  		if err != nil {
    46  			t.Errorf("Case %d: Unexpected error: %v", i+1, err)
    47  			continue
    48  		}
    49  		if !tval.Equal(tc.t) {
    50  			t.Errorf("Case %d: Expected %v got %v", i+1, tc.t, tval)
    51  			continue
    52  		}
    53  
    54  		tstr := FormatSQLTimestamp(tc.t)
    55  		if tstr != tc.s {
    56  			t.Errorf("Case %d: Expected %s got %s", i+1, tc.s, tstr)
    57  			continue
    58  		}
    59  	}
    60  }