From 9f59ee1f9fa91769729c6f2a138686b67b47cd40 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Tue, 15 May 2018 13:14:35 -0700 Subject: [PATCH] Clock is now actually a clock and not a snapshot in time --- clocks/clock.go | 30 ++++++++++++++++++++++++++---- clocks/clock_collection.go | 2 +- clocks/display.go | 4 ++-- clocks/widget.go | 16 ++++++---------- 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/clocks/clock.go b/clocks/clock.go index 519b42fe..2ce17e5f 100644 --- a/clocks/clock.go +++ b/clocks/clock.go @@ -1,11 +1,33 @@ package clocks -import( +import ( "time" + + "github.com/senorprogrammer/wtf/wtf" ) type Clock struct { - Label string - LocalTime time.Time - Timezone string + Label string + Location *time.Location +} + +func NewClock(label string, timeLoc *time.Location) Clock { + clock := Clock{ + Label: label, + Location: timeLoc, + } + + return clock +} + +func (clock *Clock) Date() string { + return clock.LocalTime().Format(wtf.SimpleDateFormat) +} + +func (clock *Clock) LocalTime() time.Time { + return time.Now().In(clock.Location) +} + +func (clock *Clock) Time() string { + return clock.LocalTime().Format(wtf.SimpleTimeFormat) } diff --git a/clocks/clock_collection.go b/clocks/clock_collection.go index 64d2a6ac..d81b18ff 100644 --- a/clocks/clock_collection.go +++ b/clocks/clock_collection.go @@ -32,6 +32,6 @@ func (clocks *ClockCollection) SortedChronologically() { clock := clocks.Clocks[i] other := clocks.Clocks[j] - return clock.LocalTime.Before(other.LocalTime) + return clock.LocalTime().Before(other.LocalTime()) }) } diff --git a/clocks/display.go b/clocks/display.go index 4dd16b77..1cf632e9 100644 --- a/clocks/display.go +++ b/clocks/display.go @@ -18,8 +18,8 @@ func (widget *Widget) display(clocks []Clock) { " [%s]%-12s %-10s %7s[white]\n", wtf.RowColor("clocks", idx), clock.Label, - clock.LocalTime.Format(wtf.SimpleTimeFormat), - clock.LocalTime.Format(wtf.SimpleDateFormat), + clock.Time(), + clock.Date(), ) } diff --git a/clocks/widget.go b/clocks/widget.go index 8263c0de..e53e76ec 100644 --- a/clocks/widget.go +++ b/clocks/widget.go @@ -12,6 +12,8 @@ var Config *config.Config type Widget struct { wtf.TextWidget + + clockColl ClockCollection } func NewWidget() *Widget { @@ -19,6 +21,8 @@ func NewWidget() *Widget { TextWidget: wtf.NewTextWidget(" 🕗 World Clocks ", "clocks", false), } + widget.clockColl = widget.buildClockCollection(Config.UMap("wtf.mods.clocks.locations")) + return &widget } @@ -29,10 +33,8 @@ func (widget *Widget) Refresh() { return } - clockColl := widget.buildClockCollection(Config.UMap("wtf.mods.clocks.locations")) - widget.View.Clear() - widget.display(clockColl.Sorted()) + widget.display(widget.clockColl.Sorted()) widget.RefreshedAt = time.Now() } @@ -47,13 +49,7 @@ func (widget *Widget) buildClockCollection(locData map[string]interface{}) Clock continue } - clock := Clock{ - Label: label, - LocalTime: time.Now().In(timeLoc), - Timezone: locStr.(string), - } - - clockColl.Clocks = append(clockColl.Clocks, clock) + clockColl.Clocks = append(clockColl.Clocks, NewClock(label, timeLoc)) } return clockColl