Skip to content

make the NullTime type an alias of sql.NullTime #1049

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 0 additions & 50 deletions nulltime.go

This file was deleted.

2 changes: 1 addition & 1 deletion nulltime_go113.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ import (
// }
//
// This NullTime implementation is not driver-specific
type NullTime sql.NullTime
type NullTime = sql.NullTime
37 changes: 37 additions & 0 deletions nulltime_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
package mysql

import (
"database/sql/driver"
"fmt"
"time"
)

Expand All @@ -32,3 +34,38 @@ type NullTime struct {
Time time.Time
Valid bool // Valid is true if Time is not NULL
}

// Scan implements the Scanner interface.
// The value type must be time.Time or string / []byte (formatted time-string),
// otherwise Scan fails.
func (nt *NullTime) Scan(value interface{}) (err error) {
if value == nil {
nt.Time, nt.Valid = time.Time{}, false
return
}

switch v := value.(type) {
case time.Time:
nt.Time, nt.Valid = v, true
return
case []byte:
nt.Time, err = parseDateTime(string(v), time.UTC)
nt.Valid = (err == nil)
return
case string:
nt.Time, err = parseDateTime(v, time.UTC)
nt.Valid = (err == nil)
return
}

nt.Valid = false
return fmt.Errorf("Can't convert %T to time.Time", value)
}

// Value implements the driver Valuer interface.
func (nt NullTime) Value() (driver.Value, error) {
if !nt.Valid {
return nil, nil
}
return nt.Time, nil
}