From 417cf68bd8b560d442a07f781b1737da4e48cb79 Mon Sep 17 00:00:00 2001 From: Yusuf Efe <120668197+yuefdev@users.noreply.github.com> Date: Thu, 22 Oct 2026 05:57:53 +0300 Subject: [PATCH] fix(package): [datetime] accept empty timezone options (#384) --- datetime/datetime.go | 14 +++++++------- datetime/timezone_empty_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 datetime/timezone_empty_test.go diff --git a/datetime/datetime.go b/datetime/datetime.go index a0332e6..5523ca6 100644 --- a/datetime/datetime.go +++ b/datetime/datetime.go @@ -207,7 +207,7 @@ func FormatTimeToStr(t time.Time, format string, timezone ...string) string { return "" } - if timezone != nil && timezone[0] != "" { + if len(timezone) > 0 && timezone[0] != "" { loc, err := time.LoadLocation(timezone[0]) if err != nil { return "" @@ -225,7 +225,7 @@ func FormatStrToTime(str, format string, timezone ...string) (time.Time, error) return time.Time{}, fmt.Errorf("format %s not support", format) } - if timezone != nil && timezone[0] != "" { + if len(timezone) > 0 && timezone[0] != "" { loc, err := time.LoadLocation(timezone[0]) if err != nil { return time.Time{}, err @@ -361,7 +361,7 @@ func NowDateOrTime(format string, timezone ...string) string { return "" } - if timezone != nil && timezone[0] != "" { + if len(timezone) > 0 && timezone[0] != "" { loc, err := time.LoadLocation(timezone[0]) if err != nil { return "" @@ -378,7 +378,7 @@ func NowDateOrTime(format string, timezone ...string) string { func Timestamp(timezone ...string) int64 { t := time.Now() - if timezone != nil && timezone[0] != "" { + if len(timezone) > 0 && timezone[0] != "" { loc, err := time.LoadLocation(timezone[0]) if err != nil { return 0 @@ -395,7 +395,7 @@ func Timestamp(timezone ...string) int64 { func TimestampMilli(timezone ...string) int64 { t := time.Now() - if timezone != nil && timezone[0] != "" { + if len(timezone) > 0 && timezone[0] != "" { loc, err := time.LoadLocation(timezone[0]) if err != nil { return 0 @@ -411,7 +411,7 @@ func TimestampMilli(timezone ...string) int64 { func TimestampMicro(timezone ...string) int64 { t := time.Now() - if timezone != nil && timezone[0] != "" { + if len(timezone) > 0 && timezone[0] != "" { loc, err := time.LoadLocation(timezone[0]) if err != nil { return 0 @@ -427,7 +427,7 @@ func TimestampMicro(timezone ...string) int64 { func TimestampNano(timezone ...string) int64 { t := time.Now() - if timezone != nil && timezone[0] != "" { + if len(timezone) > 0 && timezone[0] != "" { loc, err := time.LoadLocation(timezone[0]) if err != nil { return 0 diff --git a/datetime/timezone_empty_test.go b/datetime/timezone_empty_test.go new file mode 100644 index 0000000..a7df790 --- /dev/null +++ b/datetime/timezone_empty_test.go @@ -0,0 +1,25 @@ +package datetime + +import ( + "testing" + "time" +) + +func TestEmptyTimezoneOptions(t *testing.T) { + empty := []string{} + now := time.Now() + if got := FormatTimeToStr(now, "yyyy-mm-dd", empty...); got != FormatTimeToStr(now, "yyyy-mm-dd") { + t.Errorf("unexpected date: %s", got) + } + if _, err := FormatStrToTime("2026-01-02", "yyyy-mm-dd", empty...); err != nil { + t.Fatal(err) + } + if NowDateOrTime("yyyy", empty...) == "" { + t.Error("empty date") + } + for _, fn := range []func(...string) int64{Timestamp, TimestampMilli, TimestampMicro, TimestampNano} { + if fn(empty...) == 0 { + t.Error("empty timestamp") + } + } +}