github.com/kunlun-qilian/sqlx/v3@v3.0.0/datatypes/timestamp_test.go (about)

     1  package datatypes
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/onsi/gomega"
     8  )
     9  
    10  func TestTimestamp(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 := Timestamp(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 := Timestamp(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 := TimestampZero
    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  }