Repository: favonia/cloudflare-ddns
Pull request #795 (2024-07-10 08:53:31)

feat(cron): show dates when needed

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/favonia/cloudflare-ddns/pull/795?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=favonia) Report
Attention: Patch coverage is `92.85714%` with `1 line` in your changes missing coverage. Please review.
> Project coverage is 90.44%. Comparing base [(`2351a1b`)](https://app.codecov.io/gh/favonia/cloudflare-ddns/commit/2351a1b1e79ea13305f9bad49559c8f99034a14e?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=favonia) to head [(`ff2aaf9`)](https://app.codecov.io/gh/favonia/cloudflare-ddns/commit/ff2aaf93971430dba23eeeb6cbee480d9eb37488?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=favonia).

| [Files](https://app.codecov.io/gh/favonia/cloudflare-ddns/pull/795?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=favonia) | Patch % | Lines |
|---|---|---|
| [cmd/ddns/ddns.go](https://app.codecov.io/gh/favonia/cloudflare-ddns/pull/795?src=pr&el=tree&filepath=cmd%2Fddns%2Fddns.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=favonia#diff-Y21kL2RkbnMvZGRucy5nbw==) | 0.00% | [1 Missing :warning: ](https://app.codecov.io/gh/favonia/cloudflare-ddns/pull/795?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=favonia) |

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main     #795      +/-   ##
==========================================
+ Coverage   90.40%   90.44%   +0.04%     
==========================================
  Files          52       52              
  Lines        2199     2209      +10     
==========================================
+ Hits         1988     1998      +10     
  Misses        200      200              
  Partials       11       11              
```

| [Flag](https://app.codecov.io/gh/favonia/cloudflare-ddns/pull/795/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=favonia) | Coverage Δ | |
|---|---|---|
| [unittests](https://app.codecov.io/gh/favonia/cloudflare-ddns/pull/795/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=favonia) | `90.44% <92.85%> (+0.04%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=favonia#carryforward-flags-in-the-pull-request-comment) to find out more.

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/favonia/cloudflare-ddns/pull/795?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=favonia).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=favonia).


Patch:
diff --git a/cmd/ddns/ddns.go b/cmd/ddns/ddns.go
index 4e5123f..b898fb4 100644
--- a/cmd/ddns/ddns.go
+++ b/cmd/ddns/ddns.go
@@ -5,6 +5,7 @@ import (
 	"context"
 	"fmt"
 	"os"
+	"time"
 
 	"github.com/favonia/cloudflare-ddns/internal/config"
 	"github.com/favonia/cloudflare-ddns/internal/cron"
@@ -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) {
diff --git a/internal/cron/countdown.go b/internal/cron/countdown.go
index 109cebb..f717cc8 100644
--- a/internal/cron/countdown.go
+++ b/internal/cron/countdown.go
@@ -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:
@@ -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),
 		)
 	}
 }


Test patch:
diff --git a/internal/cron/countdown_test.go b/internal/cron/countdown_test.go
index f6a0c90..92ceb28 100644
--- a/internal/cron/countdown_test.go
+++ b/internal/cron/countdown_test.go
@@ -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()
 
@@ -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())
 			})
 		}
 	}