human date strings
This commit is contained in:
parent
70829d898a
commit
31ebd26f9e
|
@ -185,15 +185,18 @@ func (d Date) String() string {
|
|||
}
|
||||
|
||||
func (d Date) Human() string {
|
||||
if d.t.IsZero() {
|
||||
switch {
|
||||
case d.IsZero():
|
||||
return "-"
|
||||
}
|
||||
|
||||
if Today.Add(7).After(d) {
|
||||
case d.Equal(Today):
|
||||
return "today"
|
||||
case d.Equal(Today.Add(1)):
|
||||
return "tomorrow"
|
||||
case d.After(Today) && Today.Add(8).After(d):
|
||||
return strings.ToLower(d.t.Format("Monday"))
|
||||
}
|
||||
|
||||
default:
|
||||
return strings.ToLower(d.t.Format(DateFormat))
|
||||
}
|
||||
}
|
||||
|
||||
func (d Date) IsZero() bool {
|
||||
|
|
|
@ -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) {
|
||||
test.Equals(t, true, task.Date{}.IsZero())
|
||||
test.Equals(t, false, task.NewDate(2021, 6, 24).IsZero())
|
||||
|
|
Loading…
Reference in New Issue