mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 12:24:38 +03:00
Fix json time when unmarshalling
https://github.com/stashapp/stash/issues/25
This commit is contained in:
30
pkg/models/json_time.go
Normal file
30
pkg/models/json_time.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user