From 5fa656d1cc924999519dd16a6770a80f01c24ecf Mon Sep 17 00:00:00 2001 From: hailaz <739476267@qq.com> Date: Tue, 24 Jun 2025 15:53:47 +0800 Subject: [PATCH] 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 --- os/gtime/gtime_time.go | 5 ++++- os/gtime/gtime_z_unit_issue_test.go | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/os/gtime/gtime_time.go b/os/gtime/gtime_time.go index 388c4248f..9e6b291b8 100644 --- a/os/gtime/gtime_time.go +++ b/os/gtime/gtime_time.go @@ -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 diff --git a/os/gtime/gtime_z_unit_issue_test.go b/os/gtime/gtime_z_unit_issue_test.go index 6650c5aa8..2be17abd9 100644 --- a/os/gtime/gtime_z_unit_issue_test.go +++ b/os/gtime/gtime_z_unit_issue_test.go @@ -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) + }) +}