package main import ( "encoding" "encoding/json" "fmt" "strings" "time" ) // httpGetGerritJSON is like [httpGetJSON], but // https://gerrit-review.googlesource.com/Documentation/rest-api.html#output func httpGetGerritJSON(u string, out any) error { str, err := httpGet(u) if err != nil { return err } if _, body, ok := strings.Cut(str, "\n"); ok { str = body } return json.Unmarshal([]byte(str), out) } const GerritTimeFormat = "2006-01-02 15:04:05.000000000" type GerritTime struct { Val time.Time } var ( _ fmt.Stringer = GerritTime{} _ encoding.TextMarshaler = GerritTime{} _ encoding.TextUnmarshaler = (*GerritTime)(nil) ) // String implements [fmt.Stringer]. func (t GerritTime) String() string { return t.Val.Format(GerritTimeFormat) } // MarshalText implements [encoding.TextMarshaler]. func (t GerritTime) MarshalText() ([]byte, error) { return []byte(t.String()), nil } // UnmarshalText implements [encoding.TextUnmarshaler]. func (t *GerritTime) UnmarshalText(data []byte) error { val, err := time.Parse(GerritTimeFormat, string(data)) if err != nil { return err } t.Val = val return nil }