forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/go-sql-driver/mysql/nulltime.go

84 lines
1.2 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Copyright 2013 The Go-MySQL-Driver Authors. All rights reserved.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// This Source Code Form is subject to the terms of the Mozilla Public
2024-02-18 10:42:21 +00:00
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
2024-02-18 10:42:21 +00:00
// You can obtain one at http://mozilla.org/MPL/2.0/.
package mysql
import (
"database/sql/driver"
"fmt"
"time"
)
// Scan implements the Scanner interface.
2024-02-18 10:42:21 +00:00
// The value type must be time.Time or string / []byte (formatted time-string),
2024-02-18 10:42:21 +00:00
// otherwise Scan fails.
2024-02-18 10:42:21 +00:00
func (nt *NullTime) Scan(value interface{}) (err error) {
2024-02-18 10:42:21 +00:00
if value == nil {
2024-02-18 10:42:21 +00:00
nt.Time, nt.Valid = time.Time{}, false
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
switch v := value.(type) {
2024-02-18 10:42:21 +00:00
case time.Time:
2024-02-18 10:42:21 +00:00
nt.Time, nt.Valid = v, true
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
case []byte:
2024-02-18 10:42:21 +00:00
nt.Time, err = parseDateTime(v, time.UTC)
2024-02-18 10:42:21 +00:00
nt.Valid = (err == nil)
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
case string:
2024-02-18 10:42:21 +00:00
nt.Time, err = parseDateTime([]byte(v), time.UTC)
2024-02-18 10:42:21 +00:00
nt.Valid = (err == nil)
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
nt.Valid = false
2024-02-18 10:42:21 +00:00
return fmt.Errorf("Can't convert %T to time.Time", value)
2024-02-18 10:42:21 +00:00
}
// Value implements the driver Valuer interface.
2024-02-18 10:42:21 +00:00
func (nt NullTime) Value() (driver.Value, error) {
2024-02-18 10:42:21 +00:00
if !nt.Valid {
2024-02-18 10:42:21 +00:00
return nil, nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nt.Time, nil
2024-02-18 10:42:21 +00:00
}