fix(package): [datetime] accept empty timezone options (#384)

This commit is contained in:
Yusuf Efe authored and GitHub committed 2026-10-22 10:57:53 +08:00
1 parent cb45ef2d36
commit 417cf68bd8
2 files changed
+32 -7

No files matched your search

+7 -7
View File
@@ -207,7 +207,7 @@ func FormatTimeToStr(t time.Time, format string, timezone ...string) string {
return "" return ""
} }
if timezone != nil && timezone[0] != "" { if len(timezone) > 0 && timezone[0] != "" {
loc, err := time.LoadLocation(timezone[0]) loc, err := time.LoadLocation(timezone[0])
if err != nil { if err != nil {
return "" 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) 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]) loc, err := time.LoadLocation(timezone[0])
if err != nil { if err != nil {
return time.Time{}, err return time.Time{}, err
@@ -361,7 +361,7 @@ func NowDateOrTime(format string, timezone ...string) string {
return "" return ""
} }
if timezone != nil && timezone[0] != "" { if len(timezone) > 0 && timezone[0] != "" {
loc, err := time.LoadLocation(timezone[0]) loc, err := time.LoadLocation(timezone[0])
if err != nil { if err != nil {
return "" return ""
@@ -378,7 +378,7 @@ func NowDateOrTime(format string, timezone ...string) string {
func Timestamp(timezone ...string) int64 { func Timestamp(timezone ...string) int64 {
t := time.Now() t := time.Now()
if timezone != nil && timezone[0] != "" { if len(timezone) > 0 && timezone[0] != "" {
loc, err := time.LoadLocation(timezone[0]) loc, err := time.LoadLocation(timezone[0])
if err != nil { if err != nil {
return 0 return 0
@@ -395,7 +395,7 @@ func Timestamp(timezone ...string) int64 {
func TimestampMilli(timezone ...string) int64 { func TimestampMilli(timezone ...string) int64 {
t := time.Now() t := time.Now()
if timezone != nil && timezone[0] != "" { if len(timezone) > 0 && timezone[0] != "" {
loc, err := time.LoadLocation(timezone[0]) loc, err := time.LoadLocation(timezone[0])
if err != nil { if err != nil {
return 0 return 0
@@ -411,7 +411,7 @@ func TimestampMilli(timezone ...string) int64 {
func TimestampMicro(timezone ...string) int64 { func TimestampMicro(timezone ...string) int64 {
t := time.Now() t := time.Now()
if timezone != nil && timezone[0] != "" { if len(timezone) > 0 && timezone[0] != "" {
loc, err := time.LoadLocation(timezone[0]) loc, err := time.LoadLocation(timezone[0])
if err != nil { if err != nil {
return 0 return 0
@@ -427,7 +427,7 @@ func TimestampMicro(timezone ...string) int64 {
func TimestampNano(timezone ...string) int64 { func TimestampNano(timezone ...string) int64 {
t := time.Now() t := time.Now()
if timezone != nil && timezone[0] != "" { if len(timezone) > 0 && timezone[0] != "" {
loc, err := time.LoadLocation(timezone[0]) loc, err := time.LoadLocation(timezone[0])
if err != nil { if err != nil {
return 0 return 0
+25
View File
@@ -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")
}
}
}