Add custom `Time` type to unmarshal Jira's times

main
Jasper Bok 2023-07-14 18:00:14 +02:00
parent 29d4547646
commit 481e12aec6
2 changed files with 33 additions and 9 deletions

View File

@ -1,13 +1,9 @@
package jiraclient
import (
"time"
)
type Comment struct {
Self string `json:"self"`
Id string `json:"id"`
Author User `json:"author"`
Body string `json:"body"`
Created time.Time `json:created"`
Self string `json:"self"`
Id string `json:"id"`
Author User `json:"author"`
Body string `json:"body"`
Created Time `json:"created"`
}

28
time.go 100644
View File

@ -0,0 +1,28 @@
package jiraclient
import (
"fmt"
"strings"
"time"
)
const jiraTimeLayout = "2006-01-02T15:04:05-0700"
type Time struct {
time.Time
}
func (t *Time) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("\"%s\"", t.Time.Format(jiraTimeLayout))), nil
}
func (t *Time) UnmarshalJSON(data []byte) error {
s := strings.Trim(string(data), "\"")
if s == "null" || s == "" {
return nil
}
parsedTime, err := time.Parse(jiraTimeLayout, s)
t.Time = parsedTime
return err
}