Skip to content

Commit

Permalink
fix(os/gtime): fix gtime.Value() when time only, add time only exam…
Browse files Browse the repository at this point in the history
…ple (#3714)
  • Loading branch information
niluan304 authored Aug 14, 2024
1 parent dc8bb80 commit a6c361d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 6 deletions.
39 changes: 39 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,45 @@ func Test_Issue1733(t *testing.T) {
})
}

// https://github.com/gogf/gf/issues/2012
func Test_Issue2012(t *testing.T) {
table := "time_only_" + guid.S()
if _, err := db.Exec(ctx, fmt.Sprintf(`
CREATE TABLE %s(
id int(8) unsigned zerofill NOT NULL AUTO_INCREMENT,
time_only time,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
`, table,
)); err != nil {
gtest.AssertNil(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
6 changes: 6 additions & 0 deletions os/gtime/gtime_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,11 @@ func (t *Time) Value() (driver.Value, error) {
if t.IsZero() {
return nil, nil
}

if t.Year() == 0 {
// Only time.
return t.Format("15:04:05"), nil
}

return t.Time, nil
}
30 changes: 24 additions & 6 deletions os/gtime/gtime_z_example_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,22 @@ func ExampleNew_Basic() {
t3 := gtime.New(curTime, "Y-m-d H:i:s")
t4 := gtime.New(curTime)
t5 := gtime.New(1533686888)
t6 := gtime.New("08:08:08")

fmt.Println(t1)
fmt.Println(t2)
fmt.Println(t3)
fmt.Println(t4)
fmt.Println(t5)
fmt.Println(t6)

// Output:
// 2018-08-08 08:08:08
// 2018-08-08 08:08:08
// 2018-08-08 08:08:08
// 2018-08-08 08:08:08
// 2018-08-08 08:08:08
// 08:08:08
}

func ExampleNew_WithFormat() {
Expand Down Expand Up @@ -68,12 +71,15 @@ func ExampleNewFromTime() {
// NewFromStr creates and returns a Time object with given string.
// Note that it returns nil if there's error occurs.
func ExampleNewFromStr() {
t := gtime.NewFromStr("2018-08-08 08:08:08")
t1 := gtime.NewFromStr("2018-08-08 08:08:08")
t2 := gtime.NewFromStr("08:08:08")

fmt.Println(t)
fmt.Println(t1)
fmt.Println(t2)

// Output:
// 2018-08-08 08:08:08
// 08:08:08
}

// NewFromStrFormat creates and returns a Time object with given string and
Expand Down Expand Up @@ -189,26 +195,38 @@ func ExampleTime_Second() {

// String returns current time object as string.
func ExampleTime_String() {
gt := gtime.New("2018-08-08 08:08:08")
t1 := gt.String()
gt1 := gtime.New("2018-08-08 08:08:08")
t1 := gt1.String()
gt2 := gtime.New("08:08:08")

fmt.Println(t1)
fmt.Println(reflect.TypeOf(t1))
fmt.Println(gt2)

// Output:
// 2018-08-08 08:08:08
// string
// 08:08:08
}

// IsZero reports whether t represents the zero time instant,
// January 1, year 1, 00:00:00 UTC.
func ExampleTime_IsZero() {
gt := gtime.New("2018-08-08 08:08:08")
gt1 := gtime.New("2018-08-08 08:08:08")
gt2 := gtime.New("00:00:00")
timer, _ := time.Parse("15:04:05", "00:00:00")
gt3 := gtime.NewFromTime(timer)

fmt.Println(gt.IsZero())
fmt.Println(gt1.IsZero())
fmt.Println(gt2.IsZero())
fmt.Println(timer.IsZero()) // stdlib is also false
fmt.Println(gt3.IsZero())

// Output:
// false
// false
// false
// false
}

// Add adds the duration to current time.
Expand Down

0 comments on commit a6c361d

Please sign in to comment.