date parse tweaks
This commit is contained in:
parent
ea26758020
commit
44ce94acd7
|
@ -69,30 +69,24 @@ func NewDateFromString(date string) Date {
|
||||||
return Date{}
|
return Date{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if date == "today" || date == "vandaag" {
|
||||||
|
return Today
|
||||||
|
}
|
||||||
|
|
||||||
|
if date == "tomorrow" || date == "morgen" {
|
||||||
|
return Today.AddDays(1)
|
||||||
|
}
|
||||||
|
|
||||||
t, err := time.Parse("2006-01-02", fmt.Sprintf("%.10s", date))
|
t, err := time.Parse("2006-01-02", fmt.Sprintf("%.10s", date))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return Date{t: t}
|
return Date{t: t}
|
||||||
}
|
}
|
||||||
|
|
||||||
weekday := Today.Weekday()
|
weekday := Today.Weekday()
|
||||||
var newWeekday time.Weekday
|
newWeekday, ok := ParseWeekday(date)
|
||||||
switch {
|
if !ok {
|
||||||
case date == "monday" || date == "maandag":
|
return Date{}
|
||||||
newWeekday = time.Monday
|
|
||||||
case date == "tuesday" || date == "dinsdag":
|
|
||||||
newWeekday = time.Tuesday
|
|
||||||
case date == "wednesday" || date == "woensdag":
|
|
||||||
newWeekday = time.Wednesday
|
|
||||||
case date == "thursday" || date == "donderdag":
|
|
||||||
newWeekday = time.Thursday
|
|
||||||
case date == "friday" || date == "vrijdag":
|
|
||||||
newWeekday = time.Friday
|
|
||||||
case date == "saturday" || date == "zaterdag":
|
|
||||||
newWeekday = time.Saturday
|
|
||||||
case date == "sunday" || date == "zondag":
|
|
||||||
newWeekday = time.Sunday
|
|
||||||
}
|
}
|
||||||
|
|
||||||
daysToAdd := int(newWeekday) - int(weekday)
|
daysToAdd := int(newWeekday) - int(weekday)
|
||||||
if daysToAdd <= 0 {
|
if daysToAdd <= 0 {
|
||||||
daysToAdd += 7
|
daysToAdd += 7
|
||||||
|
|
|
@ -48,6 +48,26 @@ func TestNewDateFromString(t *testing.T) {
|
||||||
input: "maandag",
|
input: "maandag",
|
||||||
exp: task.NewDate(2021, 2, 1),
|
exp: task.NewDate(2021, 2, 1),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "today english",
|
||||||
|
input: "today",
|
||||||
|
exp: task.NewDate(2021, 1, 30),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "today dutch",
|
||||||
|
input: "vandaag",
|
||||||
|
exp: task.NewDate(2021, 1, 30),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "tomorrow english",
|
||||||
|
input: "tomorrow",
|
||||||
|
exp: task.NewDate(2021, 1, 31),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "tomorrow dutch",
|
||||||
|
input: "morgen",
|
||||||
|
exp: task.NewDate(2021, 1, 31),
|
||||||
|
},
|
||||||
} {
|
} {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
test.Equals(t, tc.exp, task.NewDateFromString(tc.input))
|
test.Equals(t, tc.exp, task.NewDateFromString(tc.input))
|
||||||
|
|
Loading…
Reference in New Issue