Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(os/gtime): fix gtime.StrToTime("00:00:00") is not zero time instant #3694

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions contrib/drivers/mysql/mysql_z_unit_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,41 @@ func Test_Issue1733(t *testing.T) {
})
}

// https://github.com/gogf/gf/issues/2102
func Test_Issue2012(t *testing.T) {
table := "issue2012"
array := gstr.SplitAndTrim(gtest.DataContent(`issue2012.sql`), ";")
for _, v := range array {
if _, err := db.Exec(ctx, v); err != nil {
gtest.Error(err)
}
}
defer dropTable(table)

type TimeOnly struct {
Id int `json:"id"`
TimeOnly *gtime.Time `json:"timeOnly"`
}

gtest.C(t, func(t *gtest.T) {
timeOnly := gtime.New("15:04:05")
m := db.Model(table)

_, err := m.Insert(TimeOnly{
TimeOnly: gtime.New(timeOnly),
})
t.AssertNil(err)

_, err = m.Insert(g.Map{
"time_only": timeOnly,
})
t.AssertNil(err)

_, err = m.Insert("time_only", timeOnly)
t.AssertNil(err)
})
}

// https://github.com/gogf/gf/issues/2105
func Test_Issue2105(t *testing.T) {
table := "issue2105"
Expand Down
10 changes: 10 additions & 0 deletions contrib/drivers/mysql/testdata/issue2012.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- ----------------------------
-- Table structure for issue2012
-- ----------------------------
DROP TABLE IF EXISTS `issue2012`;

CREATE TABLE `issue2012`(
`id` int(11) NOT NULL AUTO_INCREMENT,
`time_only` time,
PRIMARY KEY (`id`)
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
2 changes: 1 addition & 1 deletion os/gtime/gtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func StrToTime(str string, format ...string) (*Time, error) {
for i := 0; i < 9-len(match[4]); i++ {
nsec *= 10
}
return NewFromTime(time.Date(0, time.Month(1), 1, hour, min, sec, nsec, local)), nil
return NewFromTime(time.Date(1, time.Month(1), 1, hour, min, sec, nsec, local)), nil
} else {
return nil, gerror.NewCodef(gcode.CodeInvalidParameter, `unsupported time converting for string "%s"`, str)
}
Expand Down
60 changes: 57 additions & 3 deletions os/gtime/gtime_z_unit_format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package gtime_test

import (
"testing"
"time"

"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/test/gtest"
Expand Down Expand Up @@ -97,18 +98,71 @@ func Test_Format(t *testing.T) {
t.Assert(ti.Format("Y-m-d h:i:s"), "")
t.Assert(ti.FormatNew("Y-m-d h:i:s"), nil)
t.Assert(ti.FormatTo("Y-m-d h:i:s"), nil)
t.Assert(ti.Layout("Y-m-d h:i:s"), "")
t.Assert(ti.LayoutNew("Y-m-d h:i:s"), nil)
t.Assert(ti.LayoutTo("Y-m-d h:i:s"), nil)
t.Assert(ti.Layout("2006-01-02 15:04:05"), "")
t.Assert(ti.LayoutNew("2006-01-02 15:04:05"), nil)
t.Assert(ti.LayoutTo("2006-01-02 15:04:05"), nil)
})
gtest.C(t, func(t *gtest.T) {
timeOnly, err := gtime.StrToTime("15:04:05")
t.Assert(err, nil)

t.Assert(timeOnly.Format("Y-m-d h:i:s"), "0001-01-01 03:04:05")
t.Assert(timeOnly.FormatNew("Y-m-d h:i:s"), "0001-01-01 03:04:05")
t.Assert(timeOnly.Format("Y-m-d H:i:s"), "0001-01-01 15:04:05")
t.Assert(timeOnly.FormatNew("Y-m-d H:i:s"), "0001-01-01 15:04:05")
t.Assert(timeOnly.Layout("2006-01-02 15:04:05"), "0001-01-01 15:04:05")
t.Assert(timeOnly.LayoutNew("2006-01-02 15:04:05"), "0001-01-01 15:04:05")

t.Assert(timeOnly.Clone().FormatTo("Y-m-d h:i:s"), "0001-01-01 03:04:05")
t.Assert(timeOnly.Clone().FormatTo("Y-m-d H:i:s"), "0001-01-01 15:04:05")
t.Assert(timeOnly.Clone().LayoutTo("2006-01-02 15:04:05"), "0001-01-01 15:04:05")

d := timeOnly.UTC().Truncate(gtime.D)
t.Assert(d.IsZero(), true)

// std time.IsZero: zero time instant, January 1, year 1, 00:00:00 UTC
zero := time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)
t.Assert(d.Time.Equal(zero), true)
})
}

func Test_Format_ZeroString(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// 0000-00-00 00:00:00 is not a valid time
timeTemp, err := gtime.StrToTime("0000-00-00 00:00:00")
t.AssertNE(err, nil)
t.AssertEQ(timeTemp, nil)
t.Assert(timeTemp.String(), "")
})

const zeroTimeStr = "0001-01-01 00:00:00"

gtest.C(t, func(t *gtest.T) {
// std time.IsZero: zero time instant, January 1, year 1, 00:00:00 UTC
zero := time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)
t.AssertEQ(zero.IsZero(), true)
t.AssertEQ(zero.Format("2006-01-02 15:04:05"), zeroTimeStr)

year0 := time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC)
t.AssertEQ(year0.IsZero(), false)
t.AssertEQ(year0.Format("2006-01-02 15:04:05"), "0000-01-01 00:00:00")
})

gtest.C(t, func(t *gtest.T) {
// set timezone to UTC to get zero time instant
err := gtime.SetTimeZone(time.UTC.String())
t.Assert(err, nil)

zeroDateTime, err := gtime.StrToTime(zeroTimeStr)
t.AssertEQ(err, nil)
t.Assert(zeroDateTime.IsZero(), true)
t.Assert(zeroDateTime.String(), "")

zeroTimeOnly, err := gtime.StrToTime("00:00:00")
t.AssertEQ(err, nil)
t.Assert(zeroTimeOnly.IsZero(), true)
t.Assert(zeroTimeOnly.String(), "")
})
}

func Test_FormatTo(t *testing.T) {
Expand Down
Loading