github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/s3select/sql/timestampfuncs_test.go (about)

     1  // Copyright (c) 2015-2021 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package sql
    19  
    20  import (
    21  	"testing"
    22  	"time"
    23  )
    24  
    25  func TestParseAndDisplaySQLTimestamp(t *testing.T) {
    26  	beijing := time.FixedZone("", int((8 * time.Hour).Seconds()))
    27  	fakeLosAngeles := time.FixedZone("", -int((8 * time.Hour).Seconds()))
    28  	cases := []struct {
    29  		s string
    30  		t time.Time
    31  	}{
    32  		{"2010T", time.Date(2010, 1, 1, 0, 0, 0, 0, time.UTC)},
    33  		{"2010-02T", time.Date(2010, 2, 1, 0, 0, 0, 0, time.UTC)},
    34  		{"2010-02-03T", time.Date(2010, 2, 3, 0, 0, 0, 0, time.UTC)},
    35  		{"2010-02-03T04:11Z", time.Date(2010, 2, 3, 4, 11, 0, 0, time.UTC)},
    36  		{"2010-02-03T04:11:30Z", time.Date(2010, 2, 3, 4, 11, 30, 0, time.UTC)},
    37  		{"2010-02-03T04:11:30.23Z", time.Date(2010, 2, 3, 4, 11, 30, 230000000, time.UTC)},
    38  		{"2010-02-03T04:11+08:00", time.Date(2010, 2, 3, 4, 11, 0, 0, beijing)},
    39  		{"2010-02-03T04:11:30+08:00", time.Date(2010, 2, 3, 4, 11, 30, 0, beijing)},
    40  		{"2010-02-03T04:11:30.23+08:00", time.Date(2010, 2, 3, 4, 11, 30, 230000000, beijing)},
    41  		{"2010-02-03T04:11:30-08:00", time.Date(2010, 2, 3, 4, 11, 30, 0, fakeLosAngeles)},
    42  		{"2010-02-03T04:11:30.23-08:00", time.Date(2010, 2, 3, 4, 11, 30, 230000000, fakeLosAngeles)},
    43  	}
    44  	for i, tc := range cases {
    45  		tval, err := parseSQLTimestamp(tc.s)
    46  		if err != nil {
    47  			t.Errorf("Case %d: Unexpected error: %v", i+1, err)
    48  			continue
    49  		}
    50  		if !tval.Equal(tc.t) {
    51  			t.Errorf("Case %d: Expected %v got %v", i+1, tc.t, tval)
    52  			continue
    53  		}
    54  
    55  		tstr := FormatSQLTimestamp(tc.t)
    56  		if tstr != tc.s {
    57  			t.Errorf("Case %d: Expected %s got %s", i+1, tc.s, tstr)
    58  			continue
    59  		}
    60  	}
    61  }