From 525d2bdc03958bc0701038649ed6735f90595161 Mon Sep 17 00:00:00 2001 From: Michael K Date: Fri, 28 Nov 2014 16:44:20 +0100 Subject: [PATCH 1/3] Made use of SecondsTillUpdate() Increases code comprehensibility --- feed.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/feed.go b/feed.go index 6a15364..b2ba231 100644 --- a/feed.go +++ b/feed.go @@ -226,11 +226,12 @@ func (this *Feed) notifyListeners() { func (this *Feed) CanUpdate() bool { // Make sure we are not within the specified cache-limit. // This ensures we don't request data too often. - utc := time.Now().UTC() - if utc.UnixNano()-this.lastupdate < int64(this.CacheTimeout*60) { + if SecondsTillUpdate() > 0 { return false } + utc := time.Now().UTC() + // If skipDays or skipHours are set in the RSS feed, use these to see if // we can update. if len(this.Channels) == 1 && this.Type == "rss" { From ee66dbdb55d25797bc4d95fbe340cd214c579815 Mon Sep 17 00:00:00 2001 From: Michael K Date: Fri, 28 Nov 2014 16:53:09 +0100 Subject: [PATCH 2/3] Store time of last update as time.Time This commit breaks the API, because LastUpdate() now returns a time.Time object instead of an int64. --- feed.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/feed.go b/feed.go index b2ba231..e40290a 100644 --- a/feed.go +++ b/feed.go @@ -98,7 +98,7 @@ type Feed struct { // Last time content was fetched. Used in conjunction with CacheTimeout // to ensure we don't get content too often. - lastupdate int64 + lastupdate time.Time } // New is a helper function to stay semi-compatible with @@ -125,8 +125,9 @@ func NewWithHandlers(cachetimeout int, enforcecachelimit bool, ch ChannelHandler } // This returns a timestamp of the last time the feed was updated. -// The value is in seconds. -func (this *Feed) LastUpdate() int64 { return this.lastupdate } +func (this *Feed) LastUpdate() time.Time { + return this.lastupdate +} // Fetch retrieves the feed's latest content if necessary. // @@ -154,7 +155,7 @@ func (this *Feed) FetchClient(uri string, client *http.Client, charset xmlx.Char return } - this.lastupdate = time.Now().UTC().UnixNano() + this.lastupdate = time.Now().UTC() this.Url = uri doc := xmlx.New() @@ -259,7 +260,8 @@ func (this *Feed) CanUpdate() bool { // before the feed should update. func (this *Feed) SecondsTillUpdate() int64 { utc := time.Now().UTC() - return int64(this.CacheTimeout*60) - (utc.Unix() - (this.lastupdate / 1e9)) + elapsed := utc.Sub(this.lastupdate) + return int64(this.CacheTimeout*60) - int64(elapsed.Seconds()) } func (this *Feed) buildFeed(doc *xmlx.Document) (err error) { From 13b293d8cdef1d3fc167db714961b2adadc86e69 Mon Sep 17 00:00:00 2001 From: Michael K Date: Fri, 28 Nov 2014 16:55:58 +0100 Subject: [PATCH 3/3] Added a new function; time till next update as time.Duration --- feed.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/feed.go b/feed.go index e40290a..2761deb 100644 --- a/feed.go +++ b/feed.go @@ -264,6 +264,11 @@ func (this *Feed) SecondsTillUpdate() int64 { return int64(this.CacheTimeout*60) - int64(elapsed.Seconds()) } +// Returns the duration needed to elapse before the feed should update. +func (this *Feed) TillUpdate() (time.Duration, error) { + return time.ParseDuration(fmt.Sprintf("%ds", SecondsTillUpdate())) +} + func (this *Feed) buildFeed(doc *xmlx.Document) (err error) { switch this.Type { case "rss":