Skip to content

Commit

Permalink
feat(cron): show dates when needed (#795)
Browse files Browse the repository at this point in the history
  • Loading branch information
favonia committed Jul 10, 2024
1 parent 2351a1b commit d1850b1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
3 changes: 2 additions & 1 deletion cmd/ddns/ddns.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"os"
"time"

"github.com/favonia/cloudflare-ddns/internal/config"
"github.com/favonia/cloudflare-ddns/internal/cron"
Expand Down Expand Up @@ -161,7 +162,7 @@ func realMain() int { //nolint:funlen
}

// Display the remaining time interval
cron.PrintCountdown(ppfmt, "Checking the IP addresses", next)
cron.PrintCountdown(ppfmt, "Checking the IP addresses", time.Now(), next)

// Wait for the next signal or the alarm, whichever comes first
if !sig.SleepUntil(ppfmt, next) {
Expand Down
20 changes: 17 additions & 3 deletions internal/cron/countdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,22 @@ const (
intervalHugeGap time.Duration = time.Minute * 10
)

func PrintCountdown(ppfmt pp.PP, activity string, target time.Time) {
interval := time.Until(target)
func DescribeIntuitively(now, target time.Time) string {
now = now.In(time.Local)
target = target.In(time.Local)

switch {
case now.Year() != target.Year():
return target.In(time.Local).Format("02 Jan 15:04 2006")
case now.YearDay() != target.YearDay():
return target.In(time.Local).Format("02 Jan 15:04")
default:
return target.In(time.Local).Format("15:04")
}
}

func PrintCountdown(ppfmt pp.PP, activity string, now, target time.Time) {
interval := target.Sub(now)

switch {
case interval < -intervalLargeGap:
Expand All @@ -28,7 +42,7 @@ func PrintCountdown(ppfmt pp.PP, activity string, target time.Time) {
ppfmt.Infof(pp.EmojiAlarm, "%s in about %v (%v) . . .",
activity,
interval.Round(intervalUnit),
target.In(time.Local).Format(time.Kitchen),
DescribeIntuitively(now, target),
)
}
}
41 changes: 38 additions & 3 deletions internal/cron/countdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,40 @@ import (
"github.com/favonia/cloudflare-ddns/internal/pp"
)

func TestDescribeIntuitively(t *testing.T) {
t.Parallel()

now := time.Now().In(time.Local)
nextYear := now.AddDate(1, 0, 0)
diffDay := now.AddDate(0, 0, 1)
if diffDay.Year() != now.Year() {
diffDay = now.AddDate(0, 0, -1)
}

for name, tc := range map[string]struct {
time time.Time
output string
}{
"now": {
now,
now.Format("15:04"),
},
"1day": {
diffDay,
diffDay.Format("02 Jan 15:04"),
},
"1year": {
nextYear,
nextYear.Format("02 Jan 15:04 2006"),
},
} {
t.Run(name, func(t *testing.T) {
t.Parallel()
require.Equal(t, tc.output, cron.DescribeIntuitively(now, tc.time))
})
}
}

func TestPrintCountdown(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -69,9 +103,10 @@ func TestPrintCountdown(t *testing.T) {
var buf strings.Builder
pp := pp.New(&buf)

target := time.Now().Add(interval)
cron.PrintCountdown(pp, activity, target)
require.Equal(t, tc.output(target.Format(time.Kitchen)), buf.String())
now := time.Now()
target := now.Add(interval)
cron.PrintCountdown(pp, activity, now, target)
require.Equal(t, tc.output(cron.DescribeIntuitively(now, target)), buf.String())
})
}
}
Expand Down

0 comments on commit d1850b1

Please sign in to comment.