mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 12:24:38 +03:00
Improved date handling
This commit is contained in:
38
pkg/models/sqlite_date.go
Normal file
38
pkg/models/sqlite_date.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"github.com/stashapp/stash/pkg/logger"
|
||||
"github.com/stashapp/stash/pkg/utils"
|
||||
"time"
|
||||
)
|
||||
|
||||
type SQLiteDate struct {
|
||||
String string
|
||||
Valid bool
|
||||
}
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (t *SQLiteDate) 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.Valid = true
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (t SQLiteDate) Value() (driver.Value, error) {
|
||||
result, err := utils.ParseDateStringAsFormat(t.String, "2006-01-02")
|
||||
if err != nil {
|
||||
logger.Debugf("sqlite date conversion error: %s", err.Error())
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
Reference in New Issue
Block a user