Files
stash-box/pkg/models/sqlite_date.go
InfiniteStash 9a20325ae0 Refactor time/date types (#370)
* Rename SQLiteDate -> SQLDate

* Switch from SQLiteTimestamp to time.Time
2022-05-12 08:01:42 +02:00

53 lines
958 B
Go

package models
import (
"database/sql/driver"
"time"
"github.com/stashapp/stash-box/pkg/logger"
"github.com/stashapp/stash-box/pkg/utils"
)
type SQLDate struct {
String string
Valid bool
}
// Scan implements the Scanner interface.
func (t *SQLDate) Scan(value interface{}) error {
dateTime, ok := value.(time.Time)
if !ok {
t.String = ""
t.Valid = false
return nil
}
t.String = dateTime.Format("2006-01-02")
if t.String != "" && t.String != "0001-01-01" {
t.Valid = true
} else {
t.Valid = false
}
return nil
}
// Value implements the driver Valuer interface.
func (t SQLDate) Value() (driver.Value, error) {
if !t.Valid || t.String == "" {
return nil, nil
}
result, err := utils.ParseDateStringAsFormat(t.String, "2006-01-02")
if err != nil {
logger.Debugf("date conversion error: %s", err.Error())
}
if result == "" {
return nil, nil
}
return result, nil
}
func (t SQLDate) IsValid() bool {
return t.Valid
}