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 @@