human date strings

This commit is contained in:
Erik Winter 2022-06-06 10:21:26 +02:00
parent 70829d898a
commit 31ebd26f9e
2 changed files with 55 additions and 6 deletions

View File

@ -185,15 +185,18 @@ func (d Date) String() string {
} }
func (d Date) Human() string { func (d Date) Human() string {
if d.t.IsZero() { switch {
case d.IsZero():
return "-" return "-"
} case d.Equal(Today):
return "today"
if Today.Add(7).After(d) { case d.Equal(Today.Add(1)):
return "tomorrow"
case d.After(Today) && Today.Add(8).After(d):
return strings.ToLower(d.t.Format("Monday")) return strings.ToLower(d.t.Format("Monday"))
default:
return strings.ToLower(d.t.Format(DateFormat))
} }
return strings.ToLower(d.t.Format(DateFormat))
} }
func (d Date) IsZero() bool { func (d Date) IsZero() bool {

View File

@ -283,6 +283,52 @@ func TestDateString(t *testing.T) {
} }
} }
func TestDateHuman(t *testing.T) {
monday := task.Today.Add(1)
for {
if monday.Weekday() == time.Monday {
break
}
monday = monday.Add(1)
}
for _, tc := range []struct {
name string
date task.Date
exp string
}{
{
name: "zero",
date: task.NewDate(0, 0, 0),
exp: "-",
},
{
name: "weekday",
date: monday,
exp: "monday",
},
{
name: "default",
date: task.NewDate(2020, 1, 1),
exp: "2020-01-01 (wednesday)",
},
{
name: "today",
date: task.Today,
exp: "today",
},
{
name: "tomorrow",
date: task.Today.Add(1),
exp: "tomorrow",
},
} {
t.Run(tc.name, func(t *testing.T) {
test.Equals(t, tc.exp, tc.date.Human())
})
}
}
func TestDateIsZero(t *testing.T) { func TestDateIsZero(t *testing.T) {
test.Equals(t, true, task.Date{}.IsZero()) test.Equals(t, true, task.Date{}.IsZero())
test.Equals(t, false, task.NewDate(2021, 6, 24).IsZero()) test.Equals(t, false, task.NewDate(2021, 6, 24).IsZero())