From 54745d512c9b638a65f94628c7cf44350002e769 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Fri, 18 Mar 2022 17:48:57 +0800 Subject: [PATCH] test: add unit test and func comment for time conversion --- datetime/conversion.go | 8 ++++++ datetime/conversion_test.go | 56 +++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 datetime/conversion_test.go diff --git a/datetime/conversion.go b/datetime/conversion.go index 367c5a0..3796ae1 100644 --- a/datetime/conversion.go +++ b/datetime/conversion.go @@ -9,14 +9,17 @@ type theTime struct { unix int64 } +// NewUnixNow return unix timestamp of current time func NewUnixNow() *theTime { return &theTime{unix: time.Now().Unix()} } +// NewUnix return unix timestamp of specified time func NewUnix(unix int64) *theTime { return &theTime{unix: unix} } +// NewFormat return unix timestamp of specified time string, t should be "yyyy-mm-dd hh:mm:ss" func NewFormat(t string) (*theTime, error) { timeLayout := "2006-01-02 15:04:05" loc := time.FixedZone("CST", 8*3600) @@ -27,6 +30,7 @@ func NewFormat(t string) (*theTime, error) { return &theTime{unix: tt.Unix()}, nil } +// NewISO8601 NewFormat return unix timestamp of specified iso8601 time string func NewISO8601(iso8601 string) (*theTime, error) { t, err := time.ParseInLocation(time.RFC3339, iso8601, time.UTC) if err != nil { @@ -35,18 +39,22 @@ func NewISO8601(iso8601 string) (*theTime, error) { return &theTime{unix: t.Unix()}, nil } +// ToUnix return unix timestamp func (t *theTime) ToUnix() int64 { return t.unix } +// ToFormat return the time string 'yyyy-mm-dd hh:mm:ss' of unix time func (t *theTime) ToFormat() string { return time.Unix(t.unix, 0).Format("2006-01-02 15:04:05") } +// ToFormatForTpl return the time string which format is specified tpl func (t *theTime) ToFormatForTpl(tpl string) string { return time.Unix(t.unix, 0).Format(tpl) } +// ToFormatForTpl return iso8601 time string func (t *theTime) ToIso8601() string { return time.Unix(t.unix, 0).Format(time.RFC3339) } diff --git a/datetime/conversion_test.go b/datetime/conversion_test.go new file mode 100644 index 0000000..4fdfd98 --- /dev/null +++ b/datetime/conversion_test.go @@ -0,0 +1,56 @@ +package datetime + +import ( + "testing" + + "github.com/duke-git/lancet/v2/internal" +) + +func TestToUnix(t *testing.T) { + assert := internal.NewAssert(t, "TestToUnix") + + tm1 := NewUnixNow() + unixTimestamp := tm1.ToUnix() + tm2 := NewUnix(unixTimestamp) + + assert.Equal(tm1, tm2) +} + +func TestToFormat(t *testing.T) { + assert := internal.NewAssert(t, "TestToFormat") + + tm1, err := NewFormat("2022/03/18 17:04:05") + assert.IsNotNil(err) + + tm1, err = NewFormat("2022-03-18 17:04:05") + assert.IsNil(err) + + res := tm1.ToFormat() + assert.Equal("2022-03-18 17:04:05", res) +} + +func TestToFormatForTpl(t *testing.T) { + assert := internal.NewAssert(t, "TestToFormatForTpl") + + tm1, err := NewFormat("2022/03/18 17:04:05") + assert.IsNotNil(err) + + tm1, err = NewFormat("2022-03-18 17:04:05") + assert.IsNil(err) + + res := tm1.ToFormatForTpl("2006/01/02 15:04:05") + assert.Equal("2022/03/18 17:04:05", res) +} + +func TestToIso8601(t *testing.T) { + assert := internal.NewAssert(t, "TestToIso8601") + + tm1, err := NewISO8601("2022-03-18 17:04:05") + assert.IsNotNil(err) + + tm1, err = NewISO8601("2006-01-02T15:04:05.999Z") + assert.IsNil(err) + + res := tm1.ToIso8601() + assert.Equal("2006-01-02T23:04:05+08:00", res) +}