From b5b1930599190e2d7a6613db8779d9bbb970a1f7 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Sun, 4 Feb 2018 14:37:42 -0800 Subject: Take timezone into consideration when calculating relative time --- duration/duration.go | 24 ++++++++++++++++++--- duration/duration_test.go | 4 ++-- template/common.go | 22 +++++++++---------- template/html/category_entries.html | 2 +- template/html/common/item_meta.html | 18 ++++++++-------- template/html/edit_feed.html | 2 +- template/html/entry.html | 2 +- template/html/feed_entries.html | 2 +- template/html/feeds.html | 2 +- template/html/history.html | 2 +- template/html/sessions.html | 2 +- template/html/starred.html | 2 +- template/html/unread.html | 2 +- template/html/users.html | 2 +- template/template.go | 19 +++++++++++++++-- template/views.go | 42 ++++++++++++++++++------------------- 16 files changed, 91 insertions(+), 58 deletions(-) diff --git a/duration/duration.go b/duration/duration.go index b31d139..fe84026 100644 --- a/duration/duration.go +++ b/duration/duration.go @@ -28,11 +28,29 @@ var ( // ElapsedTime returns in a human readable format the elapsed time // since the given datetime. -func ElapsedTime(translator *locale.Language, t time.Time) string { - if t.IsZero() || time.Now().Before(t) { +func ElapsedTime(translator *locale.Language, timezone string, t time.Time) string { + if t.IsZero() { return translator.Get(NotYet) } - diff := time.Since(t) + + 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) + } + + if now.Before(t) { + return translator.Get(NotYet) + } + + diff := now.Sub(t) // Duration in seconds s := diff.Seconds() // Duration in days diff --git a/duration/duration_test.go b/duration/duration_test.go index c9a4663..d1aae21 100644 --- a/duration/duration_test.go +++ b/duration/duration_test.go @@ -31,8 +31,8 @@ func TestElapsedTime(t *testing.T) { {time.Now().Add(-time.Hour * 24 * 365 * 3), fmt.Sprintf(Years, 3)}, } for i, tt := range dt { - if out := ElapsedTime(&locale.Language{}, tt.in); out != tt.out { - t.Errorf("%d. content mismatch for %v:exp=%q got=%q", i, tt.in, tt.out, out) + if out := ElapsedTime(&locale.Language{}, "Local", tt.in); out != tt.out { + t.Errorf(`%d. content mismatch for "%v": expected=%q got=%q`, i, tt.in, tt.out, out) } } } diff --git a/template/common.go b/template/common.go index 2d28407..61661e8 100644 --- a/template/common.go +++ b/template/common.go @@ -1,5 +1,5 @@ // Code generated by go generate; DO NOT EDIT. -// 2018-01-31 21:53:31.216689995 -0800 PST m=+0.040862073 +// 2018-02-04 14:28:15.225458631 -0800 PST m=+0.036040293 package template @@ -27,32 +27,32 @@ var templateCommonMap = map[string]string{
@@ -215,7 +215,7 @@ var templateCommonMap = map[string]string{ var templateCommonMapChecksums = map[string]string{ "entry_pagination": "f1465fa70f585ae8043b200ec9de5bf437ffbb0c19fb7aefc015c3555614ee27", - "item_meta": "5f8650332fcc24dd580cea3eb8e7d8ecbb0d794607185039d1f4def8db6a7460", + "item_meta": "4796b74adca0567f3dbf8bdf6ac8cda59f455ea34cb6d4a92c83660fa72a883d", "layout": "c7565e2cf904612e236bc1d7167c6c124ffe5d27348608eb5c2336606f266896", "pagination": "6ff462c2b2a53bc5448b651da017f40a39f1d4f16cef4b2f09784f0797286924", } diff --git a/template/html/category_entries.html b/template/html/category_entries.html index d7ce9d9..8910f22 100644 --- a/template/html/category_entries.html +++ b/template/html/category_entries.html @@ -27,7 +27,7 @@ {{ .Feed.Category.Title }} - {{ template "item_meta" . }} + {{ template "item_meta" dict "user" $.user "entry" . }} {{ end }} diff --git a/template/html/common/item_meta.html b/template/html/common/item_meta.html index 668a5d2..5b00d9a 100644 --- a/template/html/common/item_meta.html +++ b/template/html/common/item_meta.html @@ -2,32 +2,32 @@
diff --git a/template/html/edit_feed.html b/template/html/edit_feed.html index 888a2c0..e5bb60a 100644 --- a/template/html/edit_feed.html +++ b/template/html/edit_feed.html @@ -67,7 +67,7 @@
diff --git a/template/html/entry.html b/template/html/entry.html index 66d08fb..237acf4 100644 --- a/template/html/entry.html +++ b/template/html/entry.html @@ -59,7 +59,7 @@
- +
{{ if gt (len .entry.Content) 120 }} diff --git a/template/html/feed_entries.html b/template/html/feed_entries.html index ebd8ea7..bbc8d79 100644 --- a/template/html/feed_entries.html +++ b/template/html/feed_entries.html @@ -38,7 +38,7 @@ {{ .Feed.Category.Title }} - {{ template "item_meta" . }} + {{ template "item_meta" dict "user" $.user "entry" . }} {{ end }} diff --git a/template/html/feeds.html b/template/html/feeds.html index 5500c92..db89872 100644 --- a/template/html/feeds.html +++ b/template/html/feeds.html @@ -42,7 +42,7 @@ {{ domain .SiteURL }}
  • - {{ t "Last check:" }} + {{ t "Last check:" }}