From 1bf7f148b03099aeb9234110291c1eccf048a2fe Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Fri, 8 Dec 2023 10:36:12 +0200 Subject: [PATCH] minor types.DateTime optimizations to minimize time.Time value copies --- tools/types/datetime.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tools/types/datetime.go b/tools/types/datetime.go index 70bf3f34..5db96fc5 100644 --- a/tools/types/datetime.go +++ b/tools/types/datetime.go @@ -45,10 +45,11 @@ func (d DateTime) IsZero() bool { // // The zero value is serialized to an empty string. func (d DateTime) String() string { - if d.IsZero() { + t := d.Time() + if t.IsZero() { return "" } - return d.Time().UTC().Format(DefaultDateLayout) + return t.UTC().Format(DefaultDateLayout) } // MarshalJSON implements the [json.Marshaler] interface. @@ -74,12 +75,10 @@ func (d DateTime) Value() (driver.Value, error) { // into the current DateTime instance. func (d *DateTime) Scan(value any) error { switch v := value.(type) { - case DateTime: - d.t = v.Time() case time.Time: d.t = v - case int, int64, int32, uint, uint64, uint32: - d.t = cast.ToTime(v) + case DateTime: + d.t = v.Time() case string: if v == "" { d.t = time.Time{} @@ -91,6 +90,8 @@ func (d *DateTime) Scan(value any) error { } d.t = t } + case int, int64, int32, uint, uint64, uint32: + d.t = cast.ToTime(v) default: str := cast.ToString(v) if str == "" {