From 6a281cd5d47a3fe54f28302b5a1d8a5171c841bd Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Fri, 24 Jul 2015 12:45:32 +0200 Subject: Add PubDate parser --- rss.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/rss.go b/rss.go index ecf6f24..eb553a6 100644 --- a/rss.go +++ b/rss.go @@ -4,6 +4,8 @@ package rss import ( "encoding/xml" "net/http" + "strings" + "time" "code.google.com/p/go-charset/charset" _ "code.google.com/p/go-charset/data" @@ -15,13 +17,18 @@ type RSS struct { Channel Channel `xml:"channel"` } +// Time in RFC1123Z or RFC1123 format +type Time struct { + time.Time +} + // Channel container type Channel struct { Link string `xml:"link"` Language string `xml:"language"` Title string `xml:"title"` Description string `xml:"description"` - PubDate string `xml:"pubDate"` + PubDate Time `xml:"pubDate"` Items []Item `xml:"item"` } @@ -31,7 +38,23 @@ type Item struct { Link string `xml:"link"` GUID string `xml:"guid"` Title string `xml:"title"` - PubDate string `xml:"pubDate"` + PubDate Time `xml:"pubDate"` +} + +// UnmarshalXML decodes Time format +func (t *Time) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { + var v string + d.DecodeElement(&v, &start) + v = strings.TrimSpace(v) + parse, err := time.Parse(time.RFC1123Z, v) + if err != nil { + parse, err = time.Parse(time.RFC1123, v) + if err != nil { + return err + } + } + *t = Time{parse} + return nil } // Fetch and parse RSS from given URL -- cgit v1.2.3