You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nulltime.go 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. package mysql
  9. import (
  10. "database/sql/driver"
  11. "fmt"
  12. "time"
  13. )
  14. // Scan implements the Scanner interface.
  15. // The value type must be time.Time or string / []byte (formatted time-string),
  16. // otherwise Scan fails.
  17. func (nt *NullTime) Scan(value interface{}) (err error) {
  18. if value == nil {
  19. nt.Time, nt.Valid = time.Time{}, false
  20. return
  21. }
  22. switch v := value.(type) {
  23. case time.Time:
  24. nt.Time, nt.Valid = v, true
  25. return
  26. case []byte:
  27. nt.Time, err = parseDateTime(v, time.UTC)
  28. nt.Valid = (err == nil)
  29. return
  30. case string:
  31. nt.Time, err = parseDateTime([]byte(v), time.UTC)
  32. nt.Valid = (err == nil)
  33. return
  34. }
  35. nt.Valid = false
  36. return fmt.Errorf("Can't convert %T to time.Time", value)
  37. }
  38. // Value implements the driver Valuer interface.
  39. func (nt NullTime) Value() (driver.Value, error) {
  40. if !nt.Valid {
  41. return nil, nil
  42. }
  43. return nt.Time, nil
  44. }