mirror of
https://github.com/hrfee/jfa-go.git
synced 2024-11-05 18:00:22 +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 {
|
for b[len(b)-1] == '"' || b[len(b)-1] == 'Z' {
|
||||||
str = str[:26]
|
b = b[:len(b)-1]
|
||||||
}
|
}
|
||||||
// decent method
|
// Trim nanoseconds and anything after, we don't care
|
||||||
t.Time, err = time.Parse("2006-01-02T15:04:05.000000Z", str)
|
i := len(b) - 1
|
||||||
if err == nil {
|
for b[i] != '.' && i > 0 {
|
||||||
return
|
i--
|
||||||
}
|
}
|
||||||
t.Time, err = time.Parse("2006-01-02T15:04:05.000000", str)
|
if i != 0 {
|
||||||
if err == nil {
|
b = b[:i]
|
||||||
return
|
|
||||||
}
|
}
|
||||||
// emby method
|
t.Time, err = time.Parse("2006-01-02T15:04:05", string(b))
|
||||||
t.Time, err = time.Parse("2006-01-02T15:04:05.0000000+00:00", str)
|
// str := string(b) + "Z"
|
||||||
if err == nil {
|
// timeJSON := []byte("{ \"parseme\": \"" + str + "\" }")
|
||||||
return
|
// var parsed magicParse
|
||||||
}
|
// // Magically turn it into a time.Time
|
||||||
fmt.Println("THIRDERR", err)
|
// err = json.Unmarshal(timeJSON, &parsed)
|
||||||
// if all else fails, just do whatever would usually be done.
|
// t.Time = parsed.Parsed
|
||||||
// some stored dates from jellyfin have no timezone at the end, if not we assume UTC
|
|
||||||
if str[len(str)-1] != 'Z' {
|
|
||||||
str += "Z"
|
|
||||||
}
|
|
||||||
timeJSON := []byte("{ \"parseme\": \"" + str + "\" }")
|
|
||||||
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