ソースを参照

Entries date should contains user timezone (API)

Frédéric Guillot 8 年 前
コミット
0c7039de0e
5 ファイル変更146 行追加17 行削除
  1. 9 1
      storage/entry_query_builder.go
  2. 11 2
      storage/feed.go
  3. 4 14
      template/elapsed.go
  4. 47 0
      timezone/timezone.go
  5. 75 0
      timezone/timezone_test.go

+ 9 - 1
storage/entry_query_builder.go

@@ -13,6 +13,7 @@ import (
 
 	"github.com/miniflux/miniflux/model"
 	"github.com/miniflux/miniflux/timer"
+	"github.com/miniflux/miniflux/timezone"
 )
 
 // EntryQueryBuilder builds a SQL query to fetch entries.
@@ -160,7 +161,8 @@ func (e *EntryQueryBuilder) GetEntries() (model.Entries, error) {
 		e.url, e.author, e.content, e.status, e.starred,
 		f.title as feed_title, f.feed_url, f.site_url, f.checked_at,
 		f.category_id, c.title as category_title, f.scraper_rules, f.rewrite_rules, f.crawler,
-		fi.icon_id
+		fi.icon_id,
+		u.timezone
 		FROM entries e
 		LEFT JOIN feeds f ON f.id=e.feed_id
 		LEFT JOIN categories c ON c.id=f.category_id
@@ -183,6 +185,7 @@ func (e *EntryQueryBuilder) GetEntries() (model.Entries, error) {
 	for rows.Next() {
 		var entry model.Entry
 		var iconID interface{}
+		var tz string
 
 		entry.Feed = &model.Feed{UserID: e.userID}
 		entry.Feed.Category = &model.Category{UserID: e.userID}
@@ -210,6 +213,7 @@ func (e *EntryQueryBuilder) GetEntries() (model.Entries, error) {
 			&entry.Feed.RewriteRules,
 			&entry.Feed.Crawler,
 			&iconID,
+			&tz,
 		)
 
 		if err != nil {
@@ -222,6 +226,10 @@ func (e *EntryQueryBuilder) GetEntries() (model.Entries, error) {
 			entry.Feed.Icon.IconID = iconID.(int64)
 		}
 
+		// Make sure that timestamp fields contains timezone information (API)
+		entry.Date = timezone.Convert(tz, entry.Date)
+		entry.Feed.CheckedAt = timezone.Convert(tz, entry.Feed.CheckedAt)
+
 		entry.Feed.ID = entry.FeedID
 		entry.Feed.Icon.FeedID = entry.FeedID
 		entries = append(entries, &entry)

+ 11 - 2
storage/feed.go

@@ -12,6 +12,7 @@ import (
 
 	"github.com/miniflux/miniflux/model"
 	"github.com/miniflux/miniflux/timer"
+	"github.com/miniflux/miniflux/timezone"
 )
 
 // FeedExists checks if the given feed exists.
@@ -56,7 +57,8 @@ func (s *Storage) Feeds(userID int64) (model.Feeds, error) {
 		f.parsing_error_count, f.parsing_error_msg,
 		f.scraper_rules, f.rewrite_rules, f.crawler,
 		f.category_id, c.title as category_title,
-		fi.icon_id
+		fi.icon_id,
+		u.timezone
 		FROM feeds f
 		LEFT JOIN categories c ON c.id=f.category_id
 		LEFT JOIN feed_icons fi ON fi.feed_id=f.id
@@ -73,6 +75,7 @@ func (s *Storage) Feeds(userID int64) (model.Feeds, error) {
 	for rows.Next() {
 		var feed model.Feed
 		var iconID interface{}
+		var tz string
 		feed.Category = &model.Category{UserID: userID}
 
 		err := rows.Scan(
@@ -92,6 +95,7 @@ func (s *Storage) Feeds(userID int64) (model.Feeds, error) {
 			&feed.Category.ID,
 			&feed.Category.Title,
 			&iconID,
+			&tz,
 		)
 
 		if err != nil {
@@ -102,6 +106,7 @@ func (s *Storage) Feeds(userID int64) (model.Feeds, error) {
 			feed.Icon = &model.FeedIcon{FeedID: feed.ID, IconID: iconID.(int64)}
 		}
 
+		feed.CheckedAt = timezone.Convert(tz, feed.CheckedAt)
 		feeds = append(feeds, &feed)
 	}
 
@@ -114,6 +119,7 @@ func (s *Storage) FeedByID(userID, feedID int64) (*model.Feed, error) {
 
 	var feed model.Feed
 	var iconID interface{}
+	var tz string
 	feed.Category = &model.Category{UserID: userID}
 
 	query := `
@@ -123,7 +129,8 @@ func (s *Storage) FeedByID(userID, feedID int64) (*model.Feed, error) {
 		f.parsing_error_count, f.parsing_error_msg,
 		f.scraper_rules, f.rewrite_rules, f.crawler,
 		f.category_id, c.title as category_title,
-		fi.icon_id
+		fi.icon_id,
+		u.timezone
 		FROM feeds f
 		LEFT JOIN categories c ON c.id=f.category_id
 		LEFT JOIN feed_icons fi ON fi.feed_id=f.id
@@ -147,6 +154,7 @@ func (s *Storage) FeedByID(userID, feedID int64) (*model.Feed, error) {
 		&feed.Category.ID,
 		&feed.Category.Title,
 		&iconID,
+		&tz,
 	)
 
 	switch {
@@ -160,6 +168,7 @@ func (s *Storage) FeedByID(userID, feedID int64) (*model.Feed, error) {
 		feed.Icon = &model.FeedIcon{FeedID: feed.ID, IconID: iconID.(int64)}
 	}
 
+	feed.CheckedAt = timezone.Convert(tz, feed.CheckedAt)
 	return &feed, nil
 }
 

+ 4 - 14
template/elapsed.go

@@ -9,6 +9,7 @@ import (
 	"time"
 
 	"github.com/miniflux/miniflux/locale"
+	"github.com/miniflux/miniflux/timezone"
 )
 
 // Texts to be translated if necessary.
@@ -28,24 +29,13 @@ var (
 
 // ElapsedTime returns in a human readable format the elapsed time
 // since the given datetime.
-func elapsedTime(language *locale.Language, timezone string, t time.Time) string {
+func elapsedTime(language *locale.Language, tz string, t time.Time) string {
 	if t.IsZero() {
 		return language.Get(NotYet)
 	}
 
-	var now time.Time
-	loc, err := time.LoadLocation(timezone)
-	if err != nil {
-		now = time.Now()
-	} else {
-		now = time.Now().In(loc)
-
-		// The provided date is already converted to the user timezone by Postgres,
-		// but the timezone information is not set in the time struct.
-		// We cannot use time.In() because the date will be converted a second time.
-		t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), loc)
-	}
-
+	now := timezone.Now(tz)
+	t = timezone.Convert(tz, t)
 	if now.Before(t) {
 		return language.Get(NotYet)
 	}

+ 47 - 0
timezone/timezone.go

@@ -0,0 +1,47 @@
+// Copyright 2018 Frédéric Guillot. All rights reserved.
+// Use of this source code is governed by the Apache 2.0
+// license that can be found in the LICENSE file.
+
+package timezone
+
+import (
+	"time"
+)
+
+// Convert converts provided date time to actual timezone.
+func Convert(tz string, t time.Time) time.Time {
+	userTimezone := getLocation(tz)
+
+	if t.Location().String() == "" {
+		// In this case, the provided date is already converted to the user timezone by Postgres,
+		// but the timezone information is not set in the time struct.
+		// We cannot use time.In() because the date will be converted a second time.
+		t = time.Date(
+			t.Year(),
+			t.Month(),
+			t.Day(),
+			t.Hour(),
+			t.Minute(),
+			t.Second(),
+			t.Nanosecond(),
+			userTimezone,
+		)
+	} else if t.Location() != userTimezone {
+		t = t.In(userTimezone)
+	}
+
+	return t
+}
+
+// Now returns the current time with the given timezone.
+func Now(tz string) time.Time {
+	return time.Now().In(getLocation(tz))
+}
+
+func getLocation(tz string) *time.Location {
+	loc, err := time.LoadLocation(tz)
+	if err != nil {
+		loc = time.Local
+	}
+	return loc
+}

+ 75 - 0
timezone/timezone_test.go

@@ -0,0 +1,75 @@
+// Copyright 2018 Frédéric Guillot. All rights reserved.
+// Use of this source code is governed by the Apache 2.0
+// license that can be found in the LICENSE file.
+
+package timezone
+
+import (
+	"testing"
+	"time"
+)
+
+func TestNow(t *testing.T) {
+	tz := "Europe/Paris"
+	now := Now(tz)
+
+	if now.Location().String() != tz {
+		t.Fatalf(`Unexpected timezone, got %q instead of %q`, now.Location(), tz)
+	}
+}
+
+func TestNowWithInvalidTimezone(t *testing.T) {
+	tz := "Invalid Timezone"
+	expected := time.Local
+	now := Now(tz)
+
+	if now.Location().String() != expected.String() {
+		t.Fatalf(`Unexpected timezone, got %q instead of %q`, now.Location(), expected)
+	}
+}
+
+func TestConvertTimeWithNoTimezoneInformation(t *testing.T) {
+	tz := "Canada/Pacific"
+	input := time.Date(2018, 3, 1, 14, 2, 3, 0, time.FixedZone("", 0))
+	output := Convert(tz, input)
+
+	if output.Location().String() != tz {
+		t.Fatalf(`Unexpected timezone, got %q instead of %s`, output.Location(), tz)
+	}
+
+	hours, minutes, secs := output.Clock()
+	if hours != 14 || minutes != 2 || secs != 3 {
+		t.Fatalf(`Unexpected time, got hours=%d, minutes=%d, secs=%d`, hours, minutes, secs)
+	}
+}
+
+func TestConvertTimeWithDifferentTimezone(t *testing.T) {
+	tz := "Canada/Central"
+	input := time.Date(2018, 3, 1, 14, 2, 3, 0, time.UTC)
+	output := Convert(tz, input)
+
+	if output.Location().String() != tz {
+		t.Fatalf(`Unexpected timezone, got %q instead of %s`, output.Location(), tz)
+	}
+
+	hours, minutes, secs := output.Clock()
+	if hours != 8 || minutes != 2 || secs != 3 {
+		t.Fatalf(`Unexpected time, got hours=%d, minutes=%d, secs=%d`, hours, minutes, secs)
+	}
+}
+
+func TestConvertTimeWithIdenticalTimezone(t *testing.T) {
+	tz := "Canada/Central"
+	loc, _ := time.LoadLocation(tz)
+	input := time.Date(2018, 3, 1, 14, 2, 3, 0, loc)
+	output := Convert(tz, input)
+
+	if output.Location().String() != tz {
+		t.Fatalf(`Unexpected timezone, got %q instead of %s`, output.Location(), tz)
+	}
+
+	hours, minutes, secs := output.Clock()
+	if hours != 14 || minutes != 2 || secs != 3 {
+		t.Fatalf(`Unexpected time, got hours=%d, minutes=%d, secs=%d`, hours, minutes, secs)
+	}
+}