From 0c7039de0e22587913e23988a4e2d6c06fd3bb34 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Thu, 1 Mar 2018 21:24:58 -0800 Subject: Entries date should contains user timezone (API) --- storage/entry_query_builder.go | 10 +++++- storage/feed.go | 13 ++++++-- template/elapsed.go | 18 +++------- timezone/timezone.go | 47 ++++++++++++++++++++++++++ timezone/timezone_test.go | 75 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 146 insertions(+), 17 deletions(-) create mode 100644 timezone/timezone.go create mode 100644 timezone/timezone_test.go diff --git a/storage/entry_query_builder.go b/storage/entry_query_builder.go index 7c8d924..6650dbb 100644 --- a/storage/entry_query_builder.go +++ b/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) diff --git a/storage/feed.go b/storage/feed.go index 87b234a..7aa78b0 100644 --- a/storage/feed.go +++ b/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 } diff --git a/template/elapsed.go b/template/elapsed.go index 273771d..5c5d98c 100644 --- a/template/elapsed.go +++ b/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) } diff --git a/timezone/timezone.go b/timezone/timezone.go new file mode 100644 index 0000000..96b7fe4 --- /dev/null +++ b/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 +} diff --git a/timezone/timezone_test.go b/timezone/timezone_test.go new file mode 100644 index 0000000..eb960f2 --- /dev/null +++ b/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) + } +} -- cgit v1.2.3