Fix json time when unmarshalling

https://github.com/stashapp/stash/issues/25
This commit is contained in:
Stash Dev
2019-03-09 10:14:55 -08:00
parent f57c2bff1d
commit c4d45db30c
8 changed files with 70 additions and 70 deletions

30
pkg/models/json_time.go Normal file
View File

@@ -0,0 +1,30 @@
package models
import (
"fmt"
"github.com/stashapp/stash/pkg/utils"
"strings"
"time"
)
type JSONTime struct {
time.Time
}
func (jt *JSONTime) UnmarshalJSON(b []byte) (err error) {
s := strings.Trim(string(b), "\"")
if s == "null" {
jt.Time = time.Time{}
return
}
jt.Time, err = utils.ParseDateStringAsTime(s)
return
}
func (jt *JSONTime) MarshalJSON() ([]byte, error) {
if jt.Time.IsZero() {
return []byte("null"), nil
}
return []byte(fmt.Sprintf("\"%s\"", jt.Time.Format(time.RFC3339))), nil
}