Skip to content

Commit 0e56f7d

Browse files
committed
Working on tz sorting
1 parent 105e8c0 commit 0e56f7d

File tree

5 files changed

+66
-7
lines changed

5 files changed

+66
-7
lines changed

core/args.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func ParseFlags(startConfig Config, appVersion string) (Config, time.Time, bool,
1616
// Check for any changes
1717
var changed bool
1818
// Define configuration flags
19-
var timezones, symbols, tics, stretch, inline, colorize, hours12, live string
19+
var timezones, symbols, tics, stretch, inline, colorize, hours12, live, sorting string
2020
flag.StringVar(
2121
&timezones,
2222
"timezones",
@@ -69,6 +69,15 @@ func ParseFlags(startConfig Config, appVersion string) (Config, time.Time, bool,
6969
"",
7070
"indicates whether to display time live (quit via 'q' or 'Ctrl+C') (one of: true, false)",
7171
)
72+
flag.StringVar(
73+
&sorting,
74+
"",
75+
SortingModeDefault,
76+
"indicates how to sort the timezones (one of: "+
77+
SortingModeNone+", "+
78+
SortingModeOffset+", "+
79+
SortingModeName+")",
80+
)
7281

7382
// Define direct flags
7483
var requestTime string
@@ -166,6 +175,13 @@ func ParseFlags(startConfig Config, appVersion string) (Config, time.Time, bool,
166175
return startConfig, rt, changed, fmt.Errorf("invalid value for live: %s", live)
167176
}
168177
}
178+
if sorting != "" {
179+
changed = true
180+
if isValidSortingMode(sorting) {
181+
return startConfig, rt, changed, fmt.Errorf("invalid sorting mode: %s", sorting)
182+
}
183+
startConfig.Sorting = sorting
184+
}
169185

170186
// Handle direct flags
171187
if requestTime != "" {

core/configuration.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ type Config struct {
3838

3939
// Indicates whether to continuously update.
4040
Live bool `json:"live"`
41+
42+
// Defines the mode for sorting the timezones.
43+
Sorting string `json:"sorting"`
4144
}
4245

4346
// Location describes a timezone the user wants to display.

core/plot.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,15 +330,22 @@ func PlotTime(plt Plotter, cfg Config, t time.Time) error {
330330

331331
// createTimeInfos creates the time info strings for all locations.
332332
func createTimeInfos(cfg Config, t time.Time) (timeInfos []string, times []*time.Location, err error) {
333-
// Init
334-
timeInfos = make([]string, len(cfg.Timezones)+1)
335-
336-
// Prepare timeZones to plot
333+
// Sort timezones and convert
337334
timeZones := make([]*time.Location, len(cfg.Timezones)+1)
338335
descriptions := make([]string, len(cfg.Timezones)+1)
336+
sortedTimeZones := sortLocations(cfg.Timezones, cfg.Sorting)
337+
338+
// Prepare timeZones to plot
339339
timeZones[0] = time.Local
340340
descriptions[0] = "Local"
341-
for i, tz := range cfg.Timezones {
341+
sortedZones := cfg.Timezones
342+
switch cfg.Sorting {
343+
case SortingModeOffset:
344+
sortedZones = sortByOffset(cfg.Timezones)
345+
case SortingModeName:
346+
sortedZones = sortByName(cfg.Timezones)
347+
}
348+
for i, tz := range sortedZones {
342349
// Get timezone
343350
loc, err := time.LoadLocation(tz.TZ)
344351
if err != nil {
@@ -350,6 +357,7 @@ func createTimeInfos(cfg Config, t time.Time) (timeInfos []string, times []*time
350357
}
351358
descriptionLength := maxStringLength(descriptions)
352359

360+
timeInfos = make([]string, len(cfg.Timezones)+1)
353361
for i := range timeZones {
354362
// Prepare location and time infos
355363
timeInfo := fmt.Sprintf("%-*s", descriptionLength, descriptions[i])

core/sorting.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package core
2+
3+
import "sort"
4+
5+
// Define sorting modes
6+
const (
7+
// SortingModeNone keeps the order of the timezones as they are defined.
8+
SortingModeNone = "none"
9+
// SortingModeOffset sorts the timezones by their offset.
10+
SortingModeOffset = "offset"
11+
// SortingModeName sorts the timezones by their name.
12+
SortingModeName = "name"
13+
// SortingModeDefault is the default sorting mode.
14+
SortingModeDefault = SortingModeNone
15+
)
16+
17+
// isValidSortingMode checks if the given sorting mode is defined and valid.
18+
func isValidSortingMode(mode string) bool {
19+
switch mode {
20+
case SortingModeNone, SortingModeOffset, SortingModeName:
21+
return true
22+
default:
23+
return false
24+
}
25+
}
26+
27+
// sortByOffset sorts the given locations by their offset.
28+
func sortByOffset(locations []Location) {
29+
sort.Slice(locations, func(i, j int) bool {
30+
return locations[i].
31+
})
32+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/merschformann/gotz
22

3-
go 1.17
3+
go 1.23.0
44

55
require (
66
github.com/adrg/xdg v0.4.0

0 commit comments

Comments
 (0)