From b0fb5469c0ae81e204d96956f31a169f9dbc5d8c Mon Sep 17 00:00:00 2001 From: jholdstock Date: Fri, 25 Oct 2024 12:30:01 +0100 Subject: [PATCH] webapi: Footer update time relative not absolute. Using a relative time rather than absolute requires far less effort for a reader to understand, e.g. compare "25 Oct 2024 11:12:49 UTC" to "3 minutes ago". As a nice side-effect this also fixes a minor privacy issue where using the full absolute timestamp would expose the server timezone to the public. --- internal/webapi/cache.go | 6 +++--- internal/webapi/formatting.go | 14 ++++++++++++++ internal/webapi/templates/footer.html | 2 +- internal/webapi/webapi.go | 1 + 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/internal/webapi/cache.go b/internal/webapi/cache.go index b36418ae..7ce2681a 100644 --- a/internal/webapi/cache.go +++ b/internal/webapi/cache.go @@ -1,4 +1,4 @@ -// Copyright (c) 2020-2023 The Decred developers +// Copyright (c) 2020-2024 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. @@ -34,7 +34,7 @@ type cacheData struct { // the first time. Initialized bool - UpdateTime string + UpdateTime time.Time PubKey string DatabaseSize string Voting int64 @@ -118,7 +118,7 @@ func (c *cache) update() error { defer c.mtx.Unlock() c.data.Initialized = true - c.data.UpdateTime = dateTime(time.Now().Unix()) + c.data.UpdateTime = time.Now() c.data.DatabaseSize = humanize.Bytes(dbSize) c.data.Voting = voting c.data.Voted = voted diff --git a/internal/webapi/formatting.go b/internal/webapi/formatting.go index 30219fc4..8d8d013d 100644 --- a/internal/webapi/formatting.go +++ b/internal/webapi/formatting.go @@ -1,3 +1,7 @@ +// Copyright (c) 2020-2024 The Decred developers +// Use of this source code is governed by an ISC +// license that can be found in the LICENSE file. + package webapi import ( @@ -9,6 +13,7 @@ import ( "github.com/decred/dcrd/dcrutil/v4" "github.com/decred/slog" + "github.com/dustin/go-humanize" ) func addressURL(blockExplorerURL string) func(string) string { @@ -29,10 +34,19 @@ func blockURL(blockExplorerURL string) func(int64) string { } } +// dateTime returns a human readable representation of the provided unix +// timestamp. It includes the local timezone of the server so use on public +// webpages is not recommended. func dateTime(t int64) string { return time.Unix(t, 0).Format("2 Jan 2006 15:04:05 MST") } +// timeAgo compares the provided unix timestamp to the current time to return a +// string like "3 minutes ago". +func timeAgo(t time.Time) string { + return humanize.Time(t) +} + func stripWss(input string) string { input = strings.ReplaceAll(input, "wss://", "") input = strings.ReplaceAll(input, "/ws", "") diff --git a/internal/webapi/templates/footer.html b/internal/webapi/templates/footer.html index 59846a53..dc0f48ab 100644 --- a/internal/webapi/templates/footer.html +++ b/internal/webapi/templates/footer.html @@ -5,7 +5,7 @@