From 85399e23ed3b36bf9b31c13149111937415ddf1a Mon Sep 17 00:00:00 2001 From: dudaodong Date: Sat, 19 Mar 2022 18:42:22 +0800 Subject: [PATCH] fix: format timezone issue --- datetime/conversion.go | 6 +++--- datetime/conversion_test.go | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/datetime/conversion.go b/datetime/conversion.go index c317378..ddf01fb 100644 --- a/datetime/conversion.go +++ b/datetime/conversion.go @@ -46,15 +46,15 @@ func (t *theTime) ToUnix() int64 { // 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).Local().Format("2006-01-02 15:04:05") + 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).Local().Format(tpl) + return time.Unix(t.unix, 0).Format(tpl) } // ToFormatForTpl return iso8601 time string func (t *theTime) ToIso8601() string { - return time.Unix(t.unix, 0).Local().Format(time.RFC3339) + return time.Unix(t.unix, 0).Format(time.RFC3339) } diff --git a/datetime/conversion_test.go b/datetime/conversion_test.go index 4fdfd98..0119b37 100644 --- a/datetime/conversion_test.go +++ b/datetime/conversion_test.go @@ -2,6 +2,7 @@ package datetime import ( "testing" + "time" "github.com/duke-git/lancet/v2/internal" ) @@ -25,8 +26,19 @@ func TestToFormat(t *testing.T) { tm1, err = NewFormat("2022-03-18 17:04:05") assert.IsNil(err) - res := tm1.ToFormat() - assert.Equal("2022-03-18 17:04:05", res) + // res := tm1.ToFormat() + t1, err1 := time.Parse("2006-01-02 15:04:05", tm1.ToFormat()) + if err1 != nil { + t.FailNow() + } + + t2, err2 := time.Parse("2006-01-02 15:04:05", "2022-03-18 17:04:05") + if err2 != nil { + t.FailNow() + } + + assert.Equal(t2, t1) + // assert.Equal("2022-03-18 17:04:05", res) } func TestToFormatForTpl(t *testing.T) {