1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Close #98. Merge pull request #121 from baustinanki/fix-chrono-sorting

Fix clocks module chronological sorting issue #98
This commit is contained in:
Chris Cummer 2018-06-01 13:35:04 -07:00
commit 6a9695018d
2 changed files with 8 additions and 2 deletions

View File

@ -25,7 +25,11 @@ func (clock *Clock) Date() string {
} }
func (clock *Clock) LocalTime() time.Time { func (clock *Clock) LocalTime() time.Time {
return time.Now().In(clock.Location) return clock.ToLocal(time.Now())
}
func (clock *Clock) ToLocal(t time.Time) time.Time {
return t.In(clock.Location)
} }
func (clock *Clock) Time() string { func (clock *Clock) Time() string {

View File

@ -2,6 +2,7 @@ package clocks
import ( import (
"sort" "sort"
"time"
) )
type ClockCollection struct { type ClockCollection struct {
@ -28,10 +29,11 @@ func (clocks *ClockCollection) SortedAlphabetically() {
} }
func (clocks *ClockCollection) SortedChronologically() { func (clocks *ClockCollection) SortedChronologically() {
now := time.Now()
sort.Slice(clocks.Clocks, func(i, j int) bool { sort.Slice(clocks.Clocks, func(i, j int) bool {
clock := clocks.Clocks[i] clock := clocks.Clocks[i]
other := clocks.Clocks[j] other := clocks.Clocks[j]
return clock.LocalTime().Before(other.LocalTime()) return clock.ToLocal(now).String() < other.ToLocal(now).String()
}) })
} }