Improved date handling

This commit is contained in:
Stash Dev 2019-03-04 17:14:52 -08:00
parent 9aba952dbe
commit b70d5f33d2
9 changed files with 93 additions and 18 deletions

View File

@ -32,7 +32,7 @@ func (r *mutationResolver) PerformerCreate(ctx context.Context, input models.Per
newPerformer.URL = sql.NullString{String: *input.URL, Valid: true} newPerformer.URL = sql.NullString{String: *input.URL, Valid: true}
} }
if input.Birthdate != nil { 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 { if input.Ethnicity != nil {
newPerformer.Ethnicity = sql.NullString{String: *input.Ethnicity, Valid: true} 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} updatedPerformer.URL = sql.NullString{String: *input.URL, Valid: true}
} }
if input.Birthdate != nil { 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 { if input.Ethnicity != nil {
updatedPerformer.Ethnicity = sql.NullString{String: *input.Ethnicity, Valid: true} updatedPerformer.Ethnicity = sql.NullString{String: *input.Ethnicity, Valid: true}

View File

@ -27,7 +27,7 @@ func (r *mutationResolver) SceneUpdate(ctx context.Context, input models.SceneUp
updatedScene.URL = sql.NullString{String: *input.URL, Valid: true} updatedScene.URL = sql.NullString{String: *input.URL, Valid: true}
} }
if input.Date != nil { 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 { if input.Rating != nil {
updatedScene.Rating = sql.NullInt64{Int64: int64(*input.Rating), Valid: true} updatedScene.Rating = sql.NullInt64{Int64: int64(*input.Rating), Valid: true}

View File

@ -2,6 +2,7 @@ package jsonschema
import ( import (
"fmt" "fmt"
"github.com/stashapp/stash/pkg/utils"
"strings" "strings"
"time" "time"
) )
@ -10,17 +11,15 @@ type RailsTime struct {
time.Time time.Time
} }
const railsTimeLayout = "2006-01-02 15:04:05 MST"
func (ct *RailsTime) UnmarshalJSON(b []byte) (err error) { func (ct *RailsTime) UnmarshalJSON(b []byte) (err error) {
s := strings.Trim(string(b), "\"") s := strings.Trim(string(b), "\"")
if s == "null" { if s == "null" {
ct.Time = time.Time{} ct.Time = time.Time{}
return return
} }
ct.Time, err = time.Parse(railsTimeLayout, s) t, err := utils.ParseDateStringAsTime(s)
if err != nil { if t != nil {
ct.Time, err = time.Parse(time.RFC3339, s) ct.Time = *t
} }
return return
} }

View File

@ -88,7 +88,7 @@ func (t *ImportTask) ImportPerformers(ctx context.Context) {
newPerformer.URL = sql.NullString{String: performerJSON.URL, Valid: true} newPerformer.URL = sql.NullString{String: performerJSON.URL, Valid: true}
} }
if performerJSON.Birthdate != "" { if performerJSON.Birthdate != "" {
newPerformer.Birthdate = sql.NullString{String: performerJSON.Birthdate, Valid: true} newPerformer.Birthdate = models.SQLiteDate{String: performerJSON.Birthdate, Valid: true}
} }
if performerJSON.Ethnicity != "" { if performerJSON.Ethnicity != "" {
newPerformer.Ethnicity = sql.NullString{String: performerJSON.Ethnicity, Valid: true} 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}, Title: sql.NullString{String: mappingJSON.Title, Valid: true},
Description: sql.NullString{String: mappingJSON.Description, Valid: true}, Description: sql.NullString{String: mappingJSON.Description, Valid: true},
URL: sql.NullString{String: mappingJSON.URL, 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}, Rating: sql.NullString{String: mappingJSON.Rating, Valid: true},
Tags: sql.NullString{String: mappingJSON.Tags, Valid: true}, Tags: sql.NullString{String: mappingJSON.Tags, Valid: true},
Models: sql.NullString{String: mappingJSON.Models, 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} newScene.URL = sql.NullString{String: sceneJSON.URL, Valid: true}
} }
if sceneJSON.Date != "" { 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 { if sceneJSON.Rating != 0 {
newScene.Rating = sql.NullInt64{Int64: int64(sceneJSON.Rating), Valid: true} newScene.Rating = sql.NullInt64{Int64: int64(sceneJSON.Rating), Valid: true}

View File

@ -12,7 +12,7 @@ type Performer struct {
URL sql.NullString `db:"url" json:"url"` URL sql.NullString `db:"url" json:"url"`
Twitter sql.NullString `db:"twitter" json:"twitter"` Twitter sql.NullString `db:"twitter" json:"twitter"`
Instagram sql.NullString `db:"instagram" json:"instagram"` 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"` Ethnicity sql.NullString `db:"ethnicity" json:"ethnicity"`
Country sql.NullString `db:"country" json:"country"` Country sql.NullString `db:"country" json:"country"`
EyeColor sql.NullString `db:"eye_color" json:"eye_color"` EyeColor sql.NullString `db:"eye_color" json:"eye_color"`

View File

@ -11,7 +11,7 @@ type Scene struct {
Title sql.NullString `db:"title" json:"title"` Title sql.NullString `db:"title" json:"title"`
Details sql.NullString `db:"details" json:"details"` Details sql.NullString `db:"details" json:"details"`
URL sql.NullString `db:"url" json:"url"` 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"` Rating sql.NullInt64 `db:"rating" json:"rating"`
Size sql.NullString `db:"size" json:"size"` Size sql.NullString `db:"size" json:"size"`
Duration sql.NullFloat64 `db:"duration" json:"duration"` Duration sql.NullFloat64 `db:"duration" json:"duration"`

View File

@ -9,7 +9,7 @@ type ScrapedItem struct {
Title sql.NullString `db:"title" json:"title"` Title sql.NullString `db:"title" json:"title"`
Description sql.NullString `db:"description" json:"description"` Description sql.NullString `db:"description" json:"description"`
URL sql.NullString `db:"url" json:"url"` 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"` Rating sql.NullString `db:"rating" json:"rating"`
Tags sql.NullString `db:"tags" json:"tags"` Tags sql.NullString `db:"tags" json:"tags"`
Models sql.NullString `db:"models" json:"models"` Models sql.NullString `db:"models" json:"models"`

38
pkg/models/sqlite_date.go Normal file
View 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
}

View File

@ -1,9 +1,47 @@
package utils package utils
import "time" import (
"fmt"
"time"
)
const railsTimeLayout = "2006-01-02 15:04:05 MST"
func GetYMDFromDatabaseDate(dateString string) string { func GetYMDFromDatabaseDate(dateString string) string {
t, _ := time.Parse(time.RFC3339, dateString) result, _ := ParseDateStringAsFormat(dateString, "2006-01-02")
// https://stackoverflow.com/a/20234207 WTF? return result
return t.Format("2006-01-02") }
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)
} }