mirror of
https://github.com/hrfee/jfa-go.git
synced 2024-12-22 17:10:10 +00:00
rewrite time unmarshaler for mediabrowser
Last ditch effort for #69, removes quotes and trailing Z's manually and also removes nanoseconds since they're useless.
This commit is contained in:
parent
f0dccc58aa
commit
9875458b01
@ -1,9 +1,6 @@
|
|||||||
package mediabrowser
|
package mediabrowser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -17,44 +14,29 @@ type Time struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *Time) UnmarshalJSON(b []byte) (err error) {
|
func (t *Time) UnmarshalJSON(b []byte) (err error) {
|
||||||
str := strings.TrimSuffix(strings.TrimPrefix(string(b), "\""), "\"")
|
// str := strings.TrimSuffix(strings.TrimPrefix(string(b), "\""), "\"")
|
||||||
// Trim nanoseconds to always have 6 digits, so overall length is always the same.
|
// Trim quotes from beginning and end, and any number of Zs (indicates UTC).
|
||||||
if str[len(str)-1] == 'Z' {
|
for b[0] == '"' {
|
||||||
if str[len(str)-2] == 'Z' {
|
b = b[1:]
|
||||||
/* From #69, "ZZ" is sometimes used, meaning UTC-8:00.
|
|
||||||
TZ doesn't really matter to us, so we'll pretend it's UTC. */
|
|
||||||
str = str[:25] + "0Z"
|
|
||||||
} else {
|
|
||||||
str = str[:26] + "Z"
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
str = str[:26]
|
|
||||||
}
|
}
|
||||||
// decent method
|
for b[len(b)-1] == '"' || b[len(b)-1] == 'Z' {
|
||||||
t.Time, err = time.Parse("2006-01-02T15:04:05.000000Z", str)
|
b = b[:len(b)-1]
|
||||||
if err == nil {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
t.Time, err = time.Parse("2006-01-02T15:04:05.000000", str)
|
// Trim nanoseconds and anything after, we don't care
|
||||||
if err == nil {
|
i := len(b) - 1
|
||||||
return
|
for b[i] != '.' && i > 0 {
|
||||||
|
i--
|
||||||
}
|
}
|
||||||
// emby method
|
if i != 0 {
|
||||||
t.Time, err = time.Parse("2006-01-02T15:04:05.0000000+00:00", str)
|
b = b[:i]
|
||||||
if err == nil {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
fmt.Println("THIRDERR", err)
|
t.Time, err = time.Parse("2006-01-02T15:04:05", string(b))
|
||||||
// if all else fails, just do whatever would usually be done.
|
// str := string(b) + "Z"
|
||||||
// some stored dates from jellyfin have no timezone at the end, if not we assume UTC
|
// timeJSON := []byte("{ \"parseme\": \"" + str + "\" }")
|
||||||
if str[len(str)-1] != 'Z' {
|
// var parsed magicParse
|
||||||
str += "Z"
|
// // Magically turn it into a time.Time
|
||||||
}
|
// err = json.Unmarshal(timeJSON, &parsed)
|
||||||
timeJSON := []byte("{ \"parseme\": \"" + str + "\" }")
|
// t.Time = parsed.Parsed
|
||||||
var parsed magicParse
|
|
||||||
// Magically turn it into a time.Time
|
|
||||||
err = json.Unmarshal(timeJSON, &parsed)
|
|
||||||
t.Time = parsed.Parsed
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user