1// Go MySQL Driver - A MySQL-Driver for Go's database/sql package 2// 3// Copyright 2013 The Go-MySQL-Driver Authors. All rights reserved. 4// 5// This Source Code Form is subject to the terms of the Mozilla Public 6// License, v. 2.0. If a copy of the MPL was not distributed with this file, 7// You can obtain one at http://mozilla.org/MPL/2.0/. 8 9package mysql 10 11import ( 12 "database/sql/driver" 13 "fmt" 14 "time" 15) 16 17// Scan implements the Scanner interface. 18// The value type must be time.Time or string / []byte (formatted time-string), 19// otherwise Scan fails. 20func (nt *NullTime) Scan(value interface{}) (err error) { 21 if value == nil { 22 nt.Time, nt.Valid = time.Time{}, false 23 return 24 } 25 26 switch v := value.(type) { 27 case time.Time: 28 nt.Time, nt.Valid = v, true 29 return 30 case []byte: 31 nt.Time, err = parseDateTime(string(v), time.UTC) 32 nt.Valid = (err == nil) 33 return 34 case string: 35 nt.Time, err = parseDateTime(v, time.UTC) 36 nt.Valid = (err == nil) 37 return 38 } 39 40 nt.Valid = false 41 return fmt.Errorf("Can't convert %T to time.Time", value) 42} 43 44// Value implements the driver Valuer interface. 45func (nt NullTime) Value() (driver.Value, error) { 46 if !nt.Valid { 47 return nil, nil 48 } 49 return nt.Time, nil 50} 51