Skip to content

Commit

Permalink
minor types.DateTime optimizations to minimize time.Time value copies
Browse files Browse the repository at this point in the history
  • Loading branch information
ganigeorgiev committed Dec 8, 2023
1 parent 6e6c873 commit 1bf7f14
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions tools/types/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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{}
Expand All @@ -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 == "" {
Expand Down

0 comments on commit 1bf7f14

Please sign in to comment.