Skip to content

Commit 4ba9f52

Browse files
committed
Merge branch 'lyricnz-feature/locale-dates'
2 parents 3b37cc3 + 1713392 commit 4ba9f52

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+5232
-3
lines changed

cointop/coins_table.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ func (ct *Cointop) GetCoinsTable() *table.Table {
291291
})
292292
case "last_updated":
293293
unix, _ := strconv.ParseInt(coin.LastUpdated, 10, 64)
294-
lastUpdated := time.Unix(unix, 0).Format("15:04:05 Jan 02")
294+
lastUpdated := humanize.FormatTime(time.Unix(unix, 0), "15:04:05 Jan 02")
295295
ct.SetTableColumnWidthFromString(header, lastUpdated)
296296
ct.SetTableColumnAlignLeft(header, false)
297297
rowCells = append(rowCells,

cointop/portfolio.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ func (ct *Cointop) GetPortfolioTable() *table.Table {
290290
})
291291
case "last_updated":
292292
unix, _ := strconv.ParseInt(coin.LastUpdated, 10, 64)
293-
lastUpdated := time.Unix(unix, 0).Format("15:04:05 Jan 02")
293+
lastUpdated := humanize.FormatTime(time.Unix(unix, 0), "15:04:05 Jan 02")
294294
ct.SetTableColumnWidthFromString(header, lastUpdated)
295295
ct.SetTableColumnAlignLeft(header, false)
296296
rowCells = append(rowCells,

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ require (
1010
github.com/fatih/color v1.13.0
1111
github.com/gen2brain/beeep v0.0.0-20210529141713-5586760f0cc1
1212
github.com/gliderlabs/ssh v0.3.3
13+
github.com/goodsign/monday v1.0.0
14+
github.com/jeandeaual/go-locale v0.0.0-20210323163322-5cf4ff553a8d
1315
github.com/maruel/panicparse v1.6.1
1416
github.com/mattn/go-runewidth v0.0.13
1517
github.com/miguelmota/go-coinmarketcap v0.1.8

go.sum

+31
Large diffs are not rendered by default.

pkg/humanize/humanize.go

+60
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@ import (
66
"os"
77
"strconv"
88
"strings"
9+
"time"
910

11+
"github.com/goodsign/monday"
12+
"github.com/jeandeaual/go-locale"
1013
"golang.org/x/text/language"
1114
"golang.org/x/text/message"
1215
)
1316

17+
var cachedSystemLocale = ""
18+
1419
// Numericf produces a string from of the given number with give fixed precision
1520
// in base 10 with thousands separators after every three orders of magnitude
1621
// using thousands and decimal separator according to LC_NUMERIC; defaulting "en".
@@ -29,6 +34,61 @@ func Monetaryf(value float64, precision int) string {
2934
return f(value, precision, "LC_MONETARY", false)
3035
}
3136

37+
// borrowed from go-locale/util.go
38+
func splitLocale(locale string) (string, string) {
39+
// Remove the encoding, if present
40+
formattedLocale := strings.Split(locale, ".")[0]
41+
// Normalize by replacing the hyphens with underscores
42+
formattedLocale = strings.Replace(formattedLocale, "-", "_", -1)
43+
44+
// Split at the underscore
45+
split := strings.Split(formattedLocale, "_")
46+
language := split[0]
47+
territory := ""
48+
if len(split) > 1 {
49+
territory = split[1]
50+
}
51+
52+
return language, territory
53+
}
54+
55+
// GetLocale returns the current locale as defined in IETF BCP 47 (e.g. "en-US").
56+
// The envvar provided is checked first (eg LC_TIME), before the platform-specific defaults.
57+
func getLocale(envvar string) string {
58+
userLocale := "en-US" // default language-REGION
59+
// First try looking up envar directly
60+
envlang, ok := os.LookupEnv(envvar)
61+
if ok {
62+
language, region := splitLocale(envlang)
63+
userLocale = language
64+
if len(region) > 0 {
65+
userLocale = strings.Join([]string{language, region}, "-")
66+
}
67+
} else {
68+
// Then use (cached) system-specific locale
69+
if cachedSystemLocale == "" {
70+
if loc, err := locale.GetLocale(); err == nil {
71+
userLocale = loc
72+
cachedSystemLocale = loc
73+
}
74+
} else {
75+
userLocale = cachedSystemLocale
76+
}
77+
}
78+
return userLocale
79+
}
80+
81+
// formatTimeExplicit formats the given time using the prescribed layout with the provided userLocale
82+
func formatTimeExplicit(time time.Time, layout string, userLocale string) string {
83+
mondayLocale := monday.Locale(strings.Replace(userLocale, "-", "_", 1))
84+
return monday.Format(time, layout, mondayLocale)
85+
}
86+
87+
// FormatTime is a dropin replacement time.Format(layout) that uses system locale + LC_TIME
88+
func FormatTime(time time.Time, layout string) string {
89+
return formatTimeExplicit(time, layout, getLocale("LC_TIME"))
90+
}
91+
3292
// f formats given value, with precision decimal places using thousands and decimal
3393
// separator according to language found in given locale environment variable e.
3494
// If fixed is true the decimal places are fixed to the given precision otherwise d is the

pkg/humanize/humanize_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package humanize
33
import (
44
"fmt"
55
"testing"
6+
"time"
67
)
78

89
// TestMonetary tests monetary formatting
@@ -51,3 +52,43 @@ func TestScaleNumeric(t *testing.T) {
5152
}
5253
}
5354
}
55+
56+
func TestFormatTime(t *testing.T) {
57+
testData := map[string]map[string]string{
58+
"en_GB": {
59+
"Monday 2 January 2006": "Wednesday 12 March 2014",
60+
"Jan 2006": "Mar 2014",
61+
"02 Jan 2006": "12 Mar 2014",
62+
"02/01/2006": "12/03/2014",
63+
},
64+
"en_US": {
65+
"Monday 2 January 2006": "Wednesday 12 March 2014",
66+
"Jan 2006": "Mar 2014",
67+
"02 Jan 2006": "12 Mar 2014",
68+
"02/01/2006": "12/03/2014", // ??
69+
},
70+
"fr_FR": {
71+
"Monday 2 January 2006": "mercredi 12 mars 2014",
72+
"Jan 2006": "mars 2014",
73+
"02 Jan 2006": "12 mars 2014",
74+
"02/01/2006": "12/03/2014",
75+
},
76+
"de_DE": {
77+
"Monday 2 January 2006": "Mittwoch 12 März 2014",
78+
"Jan 2006": "Mär 2014",
79+
"02 Jan 2006": "12 Mär 2014",
80+
"02/01/2006": "12/03/2014",
81+
},
82+
}
83+
84+
testTime := time.Date(2014, 3, 12, 0, 0, 0, 0, time.Local)
85+
for locale, tests := range testData {
86+
for layout, result := range tests {
87+
s := formatTimeExplicit(testTime, layout, locale)
88+
if s != result {
89+
t.Fatalf("Expected layout '%s' in locale %s to render '%s' but got '%s'", layout, locale, result, s)
90+
}
91+
92+
}
93+
}
94+
}

pkg/timedata/timedata.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"math"
55
"sort"
66
"time"
7+
8+
"github.com/cointop-sh/cointop/pkg/humanize"
79
)
810

911
// ResampleTimeSeriesData resamples the given [timestamp,value] data to numsteps between start-end (returns numSteps+1 points).
@@ -59,7 +61,8 @@ func BuildTimeSeriesLabels(data [][]float64) []string {
5961
}
6062
var labels []string
6163
for i := range data {
62-
labels = append(labels, time.UnixMilli(int64(data[i][0])).Format(timeFormat))
64+
labelTime := time.UnixMilli(int64(data[i][0]))
65+
labels = append(labels, humanize.FormatTime(labelTime, timeFormat))
6366
}
6467
return labels
6568
}

vendor/github.com/goodsign/monday/.gitignore

+24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/goodsign/monday/LICENCE

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/goodsign/monday/README.md

+161
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)