commit d33056098fdd12e73cad42fb5cbdf5476ac5c023 from: Matthew Streatfield via: GitHub date: Thu Apr 17 11:43:59 2025 UTC Merge pull request #6 from ollytom/time rss: store pubdate etc. as native time.Time commit - 874b706b1cb8542a2aa62bd35d6da2ba7bc16f78 commit + d33056098fdd12e73cad42fb5cbdf5476ac5c023 blob - b54f0b31a6885e0c6d5655fff30222904da89b17 blob + 912e003542d1aca5a7f6acaf7e070ea812418ed8 --- rss/rss.go +++ rss/rss.go @@ -6,38 +6,21 @@ import ( "time" ) -type RFC1123Time struct { - time.Time -} - -func (ct *RFC1123Time) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { - var content string - if err := d.DecodeElement(&content, &start); err != nil { - return err - } - t, err := time.Parse(time.RFC1123Z, content) - if err != nil { - return err - } - ct.Time = t - return nil -} - type RSS struct { Version string `xml:"version,attr"` Channel Channel `xml:"channel"` } type Channel struct { - Title string `xml:"title"` - Description string `xml:"description"` - Link []string `xml:"link"` - Copyright string `xml:"copyright"` - LastBuildDate RFC1123Time `xml:"lastBuildDate"` - PubDate RFC1123Time `xml:"pubDate"` - TTL int `xml:"ttl"` - Items []Item `xml:"item"` - Language string `xml:"language"` + Title string `xml:"title"` + Description string `xml:"description"` + Link []string `xml:"link"` + Copyright string `xml:"copyright"` + LastBuildDate time.Time `xml:"lastBuildDate"` + PubDate time.Time `xml:"pubDate"` + TTL int `xml:"ttl"` + Items []Item `xml:"item"` + Language string `xml:"language"` //Additional metadata Image Image `xml:"image"` @@ -47,6 +30,32 @@ type Channel struct { Explicit bool `xml:"explicit"` } +func (ch *Channel) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { + type alias Channel + aux := &struct { + LastBuildDate string `xml:"lastBuildDate"` + PubDate string `xml:"pubDate"` + *alias + }{ + alias: (*alias)(ch), + } + if err := d.DecodeElement(aux, &start); err != nil { + return err + } + t, err := time.Parse(time.RFC1123Z, aux.PubDate) + if err != nil { + return err + } + ch.PubDate = t + + t, err = time.Parse(time.RFC1123Z, aux.LastBuildDate) + if err != nil { + return err + } + ch.LastBuildDate = t + return nil +} + type AtomLink struct { Href string `xml:"href,attr"` } @@ -65,13 +74,32 @@ type Owner struct { } type Item struct { - Title string `xml:"title"` - Description string `xml:"description"` - Link string `xml:"link"` - GUUID string `xml:"guid"` - PubDate RFC1123Time `xml:"pubDate"` + Title string `xml:"title"` + Description string `xml:"description"` + Link string `xml:"link"` + GUUID string `xml:"guid"` + PubDate time.Time `xml:"pubDate"` } +func (it *Item) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { + type alias Item + aux := &struct { + PubDate string `xml:"pubDate"` + *alias + }{ + alias: (*alias)(it), + } + if err := d.DecodeElement(aux, &start); err != nil { + return err + } + t, err := time.Parse(time.RFC1123Z, aux.PubDate) + if err != nil { + return err + } + it.PubDate = t + return nil +} + func Marshal(rss *RSS) ([]byte, error) { return xml.MarshalIndent(rss, "", "\t") }