mirror of https://github.com/stashapp/stash.git
Improved date handling
This commit is contained in:
parent
9aba952dbe
commit
b70d5f33d2
|
@ -32,7 +32,7 @@ func (r *mutationResolver) PerformerCreate(ctx context.Context, input models.Per
|
|||
newPerformer.URL = sql.NullString{String: *input.URL, Valid: true}
|
||||
}
|
||||
if input.Birthdate != nil {
|
||||
newPerformer.Birthdate = sql.NullString{String: *input.Birthdate, Valid: true}
|
||||
newPerformer.Birthdate = models.SQLiteDate{String: *input.Birthdate, Valid: true}
|
||||
}
|
||||
if input.Ethnicity != nil {
|
||||
newPerformer.Ethnicity = sql.NullString{String: *input.Ethnicity, Valid: true}
|
||||
|
@ -115,7 +115,7 @@ func (r *mutationResolver) PerformerUpdate(ctx context.Context, input models.Per
|
|||
updatedPerformer.URL = sql.NullString{String: *input.URL, Valid: true}
|
||||
}
|
||||
if input.Birthdate != nil {
|
||||
updatedPerformer.Birthdate = sql.NullString{String: *input.Birthdate, Valid: true}
|
||||
updatedPerformer.Birthdate = models.SQLiteDate{String: *input.Birthdate, Valid: true}
|
||||
}
|
||||
if input.Ethnicity != nil {
|
||||
updatedPerformer.Ethnicity = sql.NullString{String: *input.Ethnicity, Valid: true}
|
||||
|
|
|
@ -27,7 +27,7 @@ func (r *mutationResolver) SceneUpdate(ctx context.Context, input models.SceneUp
|
|||
updatedScene.URL = sql.NullString{String: *input.URL, Valid: true}
|
||||
}
|
||||
if input.Date != nil {
|
||||
updatedScene.Date = sql.NullString{String: *input.Date, Valid: true}
|
||||
updatedScene.Date = models.SQLiteDate{String: *input.Date, Valid: true}
|
||||
}
|
||||
if input.Rating != nil {
|
||||
updatedScene.Rating = sql.NullInt64{Int64: int64(*input.Rating), Valid: true}
|
||||
|
|
|
@ -2,6 +2,7 @@ package jsonschema
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/stashapp/stash/pkg/utils"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
@ -10,17 +11,15 @@ type RailsTime struct {
|
|||
time.Time
|
||||
}
|
||||
|
||||
const railsTimeLayout = "2006-01-02 15:04:05 MST"
|
||||
|
||||
func (ct *RailsTime) UnmarshalJSON(b []byte) (err error) {
|
||||
s := strings.Trim(string(b), "\"")
|
||||
if s == "null" {
|
||||
ct.Time = time.Time{}
|
||||
return
|
||||
}
|
||||
ct.Time, err = time.Parse(railsTimeLayout, s)
|
||||
if err != nil {
|
||||
ct.Time, err = time.Parse(time.RFC3339, s)
|
||||
t, err := utils.ParseDateStringAsTime(s)
|
||||
if t != nil {
|
||||
ct.Time = *t
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
@ -88,7 +88,7 @@ func (t *ImportTask) ImportPerformers(ctx context.Context) {
|
|||
newPerformer.URL = sql.NullString{String: performerJSON.URL, Valid: true}
|
||||
}
|
||||
if performerJSON.Birthdate != "" {
|
||||
newPerformer.Birthdate = sql.NullString{String: performerJSON.Birthdate, Valid: true}
|
||||
newPerformer.Birthdate = models.SQLiteDate{String: performerJSON.Birthdate, Valid: true}
|
||||
}
|
||||
if performerJSON.Ethnicity != "" {
|
||||
newPerformer.Ethnicity = sql.NullString{String: performerJSON.Ethnicity, Valid: true}
|
||||
|
@ -317,7 +317,7 @@ func (t *ImportTask) ImportScrapedItems(ctx context.Context) {
|
|||
Title: sql.NullString{String: mappingJSON.Title, Valid: true},
|
||||
Description: sql.NullString{String: mappingJSON.Description, Valid: true},
|
||||
URL: sql.NullString{String: mappingJSON.URL, Valid: true},
|
||||
Date: sql.NullString{String: mappingJSON.Date, Valid: true},
|
||||
Date: models.SQLiteDate{String: mappingJSON.Date, Valid: true},
|
||||
Rating: sql.NullString{String: mappingJSON.Rating, Valid: true},
|
||||
Tags: sql.NullString{String: mappingJSON.Tags, Valid: true},
|
||||
Models: sql.NullString{String: mappingJSON.Models, Valid: true},
|
||||
|
@ -392,7 +392,7 @@ func (t *ImportTask) ImportScenes(ctx context.Context) {
|
|||
newScene.URL = sql.NullString{String: sceneJSON.URL, Valid: true}
|
||||
}
|
||||
if sceneJSON.Date != "" {
|
||||
newScene.Date = sql.NullString{String: sceneJSON.Date, Valid: true}
|
||||
newScene.Date = models.SQLiteDate{String: sceneJSON.Date, Valid: true}
|
||||
}
|
||||
if sceneJSON.Rating != 0 {
|
||||
newScene.Rating = sql.NullInt64{Int64: int64(sceneJSON.Rating), Valid: true}
|
||||
|
|
|
@ -12,7 +12,7 @@ type Performer struct {
|
|||
URL sql.NullString `db:"url" json:"url"`
|
||||
Twitter sql.NullString `db:"twitter" json:"twitter"`
|
||||
Instagram sql.NullString `db:"instagram" json:"instagram"`
|
||||
Birthdate sql.NullString `db:"birthdate" json:"birthdate"` // TODO dates?
|
||||
Birthdate SQLiteDate `db:"birthdate" json:"birthdate"`
|
||||
Ethnicity sql.NullString `db:"ethnicity" json:"ethnicity"`
|
||||
Country sql.NullString `db:"country" json:"country"`
|
||||
EyeColor sql.NullString `db:"eye_color" json:"eye_color"`
|
||||
|
|
|
@ -11,7 +11,7 @@ type Scene struct {
|
|||
Title sql.NullString `db:"title" json:"title"`
|
||||
Details sql.NullString `db:"details" json:"details"`
|
||||
URL sql.NullString `db:"url" json:"url"`
|
||||
Date sql.NullString `db:"date" json:"date"` // TODO dates?
|
||||
Date SQLiteDate `db:"date" json:"date"`
|
||||
Rating sql.NullInt64 `db:"rating" json:"rating"`
|
||||
Size sql.NullString `db:"size" json:"size"`
|
||||
Duration sql.NullFloat64 `db:"duration" json:"duration"`
|
||||
|
|
|
@ -9,7 +9,7 @@ type ScrapedItem struct {
|
|||
Title sql.NullString `db:"title" json:"title"`
|
||||
Description sql.NullString `db:"description" json:"description"`
|
||||
URL sql.NullString `db:"url" json:"url"`
|
||||
Date sql.NullString `db:"date" json:"date"` // TODO dates?
|
||||
Date SQLiteDate `db:"date" json:"date"`
|
||||
Rating sql.NullString `db:"rating" json:"rating"`
|
||||
Tags sql.NullString `db:"tags" json:"tags"`
|
||||
Models sql.NullString `db:"models" json:"models"`
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -1,9 +1,47 @@
|
|||
package utils
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
const railsTimeLayout = "2006-01-02 15:04:05 MST"
|
||||
|
||||
func GetYMDFromDatabaseDate(dateString string) string {
|
||||
t, _ := time.Parse(time.RFC3339, dateString)
|
||||
// https://stackoverflow.com/a/20234207 WTF?
|
||||
return t.Format("2006-01-02")
|
||||
result, _ := ParseDateStringAsFormat(dateString, "2006-01-02")
|
||||
return result
|
||||
}
|
||||
|
||||
func ParseDateStringAsFormat(dateString string, format string) (string, error) {
|
||||
t, e := ParseDateStringAsTime(dateString)
|
||||
if t != nil {
|
||||
return t.Format(format), e
|
||||
}
|
||||
return "", fmt.Errorf("ParseDateStringAsFormat failed: dateString <%s>, format <%s>", dateString, format)
|
||||
}
|
||||
|
||||
func ParseDateStringAsTime(dateString string) (*time.Time, error) {
|
||||
// https://stackoverflow.com/a/20234207 WTF?
|
||||
|
||||
t, e := time.Parse(time.RFC3339, dateString)
|
||||
if e == nil {
|
||||
return &t, nil
|
||||
}
|
||||
|
||||
t, e = time.Parse("2006-01-02", dateString)
|
||||
if e == nil {
|
||||
return &t, nil
|
||||
}
|
||||
|
||||
t, e = time.Parse("2006-01-02 15:04:05", dateString)
|
||||
if e == nil {
|
||||
return &t, nil
|
||||
}
|
||||
|
||||
t, e = time.Parse(railsTimeLayout, dateString)
|
||||
if e == nil {
|
||||
return &t, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("ParseDateStringAsTime failed: dateString <%s>", dateString)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue