github.com/kunlun-qilian/sqlx/v2@v2.24.0/datatypes/datetime_test.go (about)

     1  package datatypes
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/onsi/gomega"
     8  )
     9  
    10  func TestDatetime(t *testing.T) {
    11  	t.Run("Parse", func(t *testing.T) {
    12  		t0, _ := time.Parse(time.RFC3339, "2017-03-27T23:58:59+08:00")
    13  		dt := Datetime(t0)
    14  
    15  		gomega.NewWithT(t).Expect(dt.String()).To(gomega.Equal("2017-03-27T23:58:59+08:00"))
    16  		gomega.NewWithT(t).Expect(dt.Format(time.RFC3339)).To(gomega.Equal("2017-03-27T23:58:59+08:00"))
    17  		gomega.NewWithT(t).Expect(dt.Unix()).To(gomega.Equal(int64(1490630339)))
    18  	})
    19  	t.Run("Marshal & Unmarshal", func(t *testing.T) {
    20  		t0, _ := time.Parse(time.RFC3339, "2017-03-27T23:58:59+08:00")
    21  		dt := Datetime(t0)
    22  
    23  		dateString, err := dt.MarshalText()
    24  		gomega.NewWithT(t).Expect(err).To(gomega.BeNil())
    25  		gomega.NewWithT(t).Expect(string(dateString)).To(gomega.Equal("2017-03-27T23:58:59+08:00"))
    26  
    27  		dt2 := DatetimeZero
    28  		gomega.NewWithT(t).Expect(dt2.IsZero()).To(gomega.BeTrue())
    29  
    30  		err = dt2.UnmarshalText(dateString)
    31  		gomega.NewWithT(t).Expect(err).To(gomega.BeNil())
    32  		gomega.NewWithT(t).Expect(dt2).To(gomega.Equal(dt))
    33  		gomega.NewWithT(t).Expect(dt2.IsZero()).To(gomega.BeFalse())
    34  
    35  		dt3 := TimestampZero
    36  		err = dt3.UnmarshalText([]byte(""))
    37  		gomega.NewWithT(t).Expect(err).To(gomega.BeNil())
    38  	})
    39  
    40  	t.Run("Scan & Value", func(t *testing.T) {
    41  		t0, _ := time.Parse(time.RFC3339, "2017-03-27T23:58:59+08:00")
    42  		dt := Datetime(t0)
    43  
    44  		value, err := dt.Value()
    45  		gomega.NewWithT(t).Expect(err).To(gomega.BeNil())
    46  		gomega.NewWithT(t).Expect(value.(time.Time).In(CST).Format(time.RFC3339)).To(gomega.Equal("2017-03-27T23:58:59+08:00"))
    47  
    48  		dt2 := DatetimeZero
    49  		gomega.NewWithT(t).Expect(dt2.IsZero()).To(gomega.BeTrue())
    50  
    51  		err = dt2.Scan(value)
    52  		gomega.NewWithT(t).Expect(err).To(gomega.BeNil())
    53  		gomega.NewWithT(t).Expect(dt2.In(CST)).To(gomega.Equal(dt.In(CST)))
    54  		gomega.NewWithT(t).Expect(dt2.IsZero()).To(gomega.BeFalse())
    55  	})
    56  }