fix(os/gtime): add handling for nil time pointers to avoid causing panic (#4323)

from https://github.com/gogf/gf/pull/4322
Fixes https://github.com/gogf/gf/issues/4307
This commit is contained in:
hailaz
2025-06-24 15:53:47 +08:00
committed by GitHub
parent 09ec90746a
commit 5fa656d1cc
2 changed files with 14 additions and 1 deletions

View File

@ -36,8 +36,11 @@ func New(param ...interface{}) *Time {
switch r := param[0].(type) {
case time.Time:
return NewFromTime(r)
case *time.Time:
return NewFromTime(*r)
if r != nil {
return NewFromTime(*r)
}
case Time:
return &r

View File

@ -71,3 +71,13 @@ func Test_Issue3558(t *testing.T) {
t.Assert(gfTimeFormat, stdTimeFormat)
})
}
// Test_Issue4307 https://github.com/gogf/gf/issues/4307
func Test_Issue4307(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var timeNil *time.Time = nil
// This should not panic.
gfTime := gtime.New(timeNil)
t.AssertNil(gfTime)
})
}