package quotedb

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

// This is more or less exactly pq.NullTime
type NullTime struct {
	Time  time.Time
	Valid bool
}

func (nt *NullTime) Scan(value interface{}) error {
	if value == nil {
		nt.Time, nt.Valid = time.Unix(0, 0).UTC(), false
		return nil
	}

	nt.Valid = true
	nt.Time = value.(time.Time)

	return nil
}

func (nt *NullTime) Value() (driver.Value, error) {
	if !nt.Valid {
		return nil, nil
	}

	return nt.Time, nil
}
