2020-07-11 21:07:52 +00:00
|
|
|
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
|
|
|
|
//
|
|
|
|
// Copyright 2013 The Go-MySQL-Driver Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql/driver"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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:
|
2021-08-28 11:15:21 +00:00
|
|
|
nt.Time, err = parseDateTime(v, time.UTC)
|
2020-07-11 21:07:52 +00:00
|
|
|
nt.Valid = (err == nil)
|
|
|
|
return
|
|
|
|
case string:
|
2021-08-28 11:15:21 +00:00
|
|
|
nt.Time, err = parseDateTime([]byte(v), time.UTC)
|
2020-07-11 21:07:52 +00:00
|
|
|
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
|
|
|
|
}
|