Skip to content

Commit

Permalink
clock.go: Preserve timezone (fix 0bdd6e2 oz#57); fix -w with -when (fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jnd-au committed Oct 15, 2024
1 parent 5e7ae52 commit 5dece4c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
32 changes: 22 additions & 10 deletions clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,37 @@ import "time"
// Clock keeps track of the current time.
type Clock struct {
t time.Time
isRealTime bool
}

// Make a new Clock.
//
// When "sec" is not t0, the returned Clock is set to this time,
// assuming "sec" is a UNIX timestamp (cf. time.Unix).
func NewClock(sec int64) *Clock {
// A new Clock in the local timezone at the current time now.
func NewClockNow() *Clock {
clock := new(Clock)
clock.t = time.Now()
clock.isRealTime = true
return clock
}

if sec != 0 {
clock.t = time.Unix(sec, 0)
} else {
clock.t = time.Now()
}
// A new Clock with the given time and timezone.
func NewClockTime(t time.Time) *Clock {
clock := new(Clock)
clock.t = t
clock.isRealTime = false
return clock
}

// A new Clock in the local timezone at the `time.Unix` timestamp.
func NewClockUnixTimestamp(sec int64) *Clock {
clock := new(Clock)
clock.t = time.Unix(sec, 0)
clock.isRealTime = false
return clock
}

// AddDays adds n days to the current date.
func (c *Clock) AddDays(n int) {
c.t = c.t.AddDate(0, 0, n)
c.isRealTime = false
}

// AddDays adds n days to the current date and clears the minutes
Expand All @@ -40,6 +51,7 @@ func (c *Clock) AddHours(n int) {
c.t.Location(),
)
c.t = c.t.Add(time.Hour * time.Duration(n))
c.isRealTime = false
}

// Get the wrapped time.Time struct
Expand Down
12 changes: 6 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
openInTimeAndDateDotCom(m.clock.Time())

case match(key, m.keymaps.Now):
m.clock = *NewClock(0)
m.clock = *NewClockNow()

case match(key, m.keymaps.ToggleDate):
m.showDates = !m.showDates
Expand All @@ -139,8 +139,8 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

case tickMsg:
if m.watch {
m.clock = *NewClock(0)
if m.watch && m.clock.isRealTime {
m.clock = *NewClockNow()
}
return m, tick()
}
Expand All @@ -153,7 +153,7 @@ func main() {

exitQuick := flag.Bool("q", false, "exit immediately")
showVersion := flag.Bool("v", false, "show version")
when := flag.Int64("when", 0, "time in seconds since unix epoch")
when := flag.Int64("when", 0, "time in seconds since unix epoch (disables -w)")
doSearch := flag.Bool("list", false, "list zones by name")
military := flag.Bool("m", false, "use 24-hour time")
watch := flag.Bool("w", false, "watch live, set time to now every minute")
Expand Down Expand Up @@ -183,15 +183,15 @@ func main() {
var initialModel = model{
zones: config.Zones,
keymaps: config.Keymaps,
clock: *NewClock(0),
clock: *NewClockNow(),
showDates: false,
isMilitary: *military,
watch: *watch,
showHelp: false,
}

if *when != 0 {
initialModel.clock = *NewClock(*when)
initialModel.clock = *NewClockUnixTimestamp(*when)
}

initialModel.interactive = !*exitQuick && isatty.IsTerminal(os.Stdout.Fd())
Expand Down
14 changes: 7 additions & 7 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ var (
)
utcMinuteAfterMidnightModel = model{
zones: DefaultZones[len(DefaultZones) - 1:],
clock: *NewClock(utcMinuteAfterMidnightTime.Unix()),
clock: *NewClockTime(utcMinuteAfterMidnightTime),
isMilitary: true,
showDates: true,
}
)

func getTimestampWithHour(hour int) int64 {
func getTimestampWithHour(hour int) time.Time {
if hour == -1 {
hour = time.Now().Hour()
}
Expand All @@ -59,7 +59,7 @@ func getTimestampWithHour(hour int) int64 {
0, // Seconds set to 0
0, // Nanoseconds set to 0
time.Now().Location(),
).Unix()
)
}

func stripAnsiControlSequences(s string) string {
Expand Down Expand Up @@ -94,7 +94,7 @@ func TestUpdateIncHour(t *testing.T) {
m := model{
zones: DefaultZones,
keymaps: NewDefaultConfig().Keymaps,
clock: *NewClock(getTimestampWithHour(test.startHour)),
clock: *NewClockTime(getTimestampWithHour(test.startHour)),
}

db := m.clock.Time().Day()
Expand Down Expand Up @@ -137,7 +137,7 @@ func TestUpdateDecHour(t *testing.T) {
m := model{
zones: DefaultZones,
keymaps: NewDefaultConfig().Keymaps,
clock: *NewClock(getTimestampWithHour(test.startHour)),
clock: *NewClockTime(getTimestampWithHour(test.startHour)),
}
nextState, cmd := m.Update(msg)
if cmd != nil {
Expand All @@ -162,7 +162,7 @@ func TestUpdateQuitMsg(t *testing.T) {
m := model{
zones: DefaultZones,
keymaps: NewDefaultConfig().Keymaps,
clock: *NewClock(getTimestampWithHour(-1)),
clock: *NewClock(),
}
_, cmd := m.Update(msg)
if cmd == nil {
Expand All @@ -176,7 +176,7 @@ func TestUpdateQuitMsg(t *testing.T) {
func TestMilitaryTime(t *testing.T) {
m := model{
zones: DefaultZones,
clock: *NewClock(getTimestampWithHour(-1)),
clock: *NewClock(),
isMilitary: true,
showDates: true,
}
Expand Down

0 comments on commit 5dece4c

Please sign in to comment.