Skip to content

Commit 4f23d49

Browse files
committed
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.
1 parent ddd7fd5 commit 4f23d49

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

internal/webapi/cache.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020-2023 The Decred developers
1+
// Copyright (c) 2020-2024 The Decred developers
22
// Use of this source code is governed by an ISC
33
// license that can be found in the LICENSE file.
44

@@ -34,7 +34,7 @@ type cacheData struct {
3434
// the first time.
3535
Initialized bool
3636

37-
UpdateTime string
37+
UpdateTime time.Time
3838
PubKey string
3939
DatabaseSize string
4040
Voting int64
@@ -118,7 +118,7 @@ func (c *cache) update() error {
118118
defer c.mtx.Unlock()
119119

120120
c.data.Initialized = true
121-
c.data.UpdateTime = dateTime(time.Now().Unix())
121+
c.data.UpdateTime = time.Now()
122122
c.data.DatabaseSize = humanize.Bytes(dbSize)
123123
c.data.Voting = voting
124124
c.data.Voted = voted

internal/webapi/formatting.go

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2020-2024 The Decred developers
2+
// Use of this source code is governed by an ISC
3+
// license that can be found in the LICENSE file.
4+
15
package webapi
26

37
import (
@@ -9,6 +13,7 @@ import (
913

1014
"github.com/decred/dcrd/dcrutil/v4"
1115
"github.com/decred/slog"
16+
"github.com/dustin/go-humanize"
1217
)
1318

1419
func addressURL(blockExplorerURL string) func(string) string {
@@ -29,10 +34,19 @@ func blockURL(blockExplorerURL string) func(int64) string {
2934
}
3035
}
3136

37+
// dateTime returns a human readable representation of the provided unix
38+
// timestamp. It includes the local timezone of the server so use on public
39+
// webpages is not recommended.
3240
func dateTime(t int64) string {
3341
return time.Unix(t, 0).Format("2 Jan 2006 15:04:05 MST")
3442
}
3543

44+
// timeAgo compares the provided unix timestamp to the current time to return a
45+
// string like "3 minutes ago".
46+
func timeAgo(t time.Time) string {
47+
return humanize.Time(t)
48+
}
49+
3650
func stripWss(input string) string {
3751
input = strings.ReplaceAll(input, "wss://", "")
3852
input = strings.ReplaceAll(input, "/ws", "")

internal/webapi/templates/footer.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<footer class="row m-0">
66
<div class="col-md-8 col-12 d-flex justify-content-center align-items-center">
77
<p class="py-4 m-0">
8-
<strong>Stats&nbsp;updated:</strong>&nbsp;{{ .WebApiCache.UpdateTime }}
8+
<strong>Stats&nbsp;updated:</strong>&nbsp;{{ timeAgo .WebApiCache.UpdateTime }}
99
<br />
1010
<strong>Support:</strong>&nbsp;<a href="mailto:{{ .WebApiCfg.SupportEmail }}" rel="noopener noreferrer">{{ .WebApiCfg.SupportEmail }}</a>
1111
<br />

internal/webapi/webapi.go

+1
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ func (w *WebAPI) router(cookieSecret []byte, dcrd rpc.DcrdConnect, wallets rpc.W
210210
"addressURL": addressURL(explorerURL),
211211
"blockURL": blockURL(explorerURL),
212212
"dateTime": dateTime,
213+
"timeAgo": timeAgo,
213214
"stripWss": stripWss,
214215
"indentJSON": indentJSON(w.log),
215216
"atomsToDCR": atomsToDCR,

0 commit comments

Comments
 (0)