more date names
This commit is contained in:
parent
428e263d46
commit
aef4dc1af2
|
@ -65,16 +65,50 @@ func NewDate(year, month, day int) Date {
|
||||||
func NewDateFromString(date string) Date {
|
func NewDateFromString(date string) Date {
|
||||||
date = strings.ToLower(strings.TrimSpace(date))
|
date = strings.ToLower(strings.TrimSpace(date))
|
||||||
|
|
||||||
if date == "no date" || date == "" {
|
switch date {
|
||||||
|
case "":
|
||||||
|
fallthrough
|
||||||
|
case "no date":
|
||||||
return Date{}
|
return Date{}
|
||||||
}
|
|
||||||
|
|
||||||
if date == "today" || date == "vandaag" {
|
case "today":
|
||||||
|
fallthrough
|
||||||
|
case "vandaag":
|
||||||
return Today
|
return Today
|
||||||
}
|
|
||||||
|
|
||||||
if date == "tomorrow" || date == "morgen" {
|
case "tomorrow":
|
||||||
|
fallthrough
|
||||||
|
case "morgen":
|
||||||
return Today.AddDays(1)
|
return Today.AddDays(1)
|
||||||
|
|
||||||
|
case "day after tomorrow":
|
||||||
|
fallthrough
|
||||||
|
case "overmorgen":
|
||||||
|
return Today.AddDays(2)
|
||||||
|
|
||||||
|
case "this week":
|
||||||
|
fallthrough
|
||||||
|
case "deze week":
|
||||||
|
daysToAdd := findDaysToWeekday(Today.Weekday(), time.Friday)
|
||||||
|
return Today.Add(daysToAdd)
|
||||||
|
|
||||||
|
case "next week":
|
||||||
|
fallthrough
|
||||||
|
case "volgende week":
|
||||||
|
daysToAdd := findDaysToWeekday(Today.Weekday(), time.Friday) + 7
|
||||||
|
return Today.Add(daysToAdd)
|
||||||
|
|
||||||
|
case "this sprint":
|
||||||
|
fallthrough
|
||||||
|
case "deze sprint":
|
||||||
|
aSprintStart := NewDate(2021, 1, 29)
|
||||||
|
daysToAdd := findDaysToWeekday(Today.Weekday(), time.Thursday)
|
||||||
|
diff := aSprintStart.DaysBetween(Today.AddDays(daysToAdd))
|
||||||
|
if diff%14 != 0 {
|
||||||
|
daysToAdd += 7
|
||||||
|
}
|
||||||
|
|
||||||
|
return Today.AddDays(daysToAdd)
|
||||||
}
|
}
|
||||||
|
|
||||||
t, err := time.Parse("2006-01-02", fmt.Sprintf("%.10s", date))
|
t, err := time.Parse("2006-01-02", fmt.Sprintf("%.10s", date))
|
||||||
|
@ -82,20 +116,42 @@ func NewDateFromString(date string) Date {
|
||||||
return Date{t: t}
|
return Date{t: t}
|
||||||
}
|
}
|
||||||
|
|
||||||
weekday := Today.Weekday()
|
|
||||||
newWeekday, ok := ParseWeekday(date)
|
newWeekday, ok := ParseWeekday(date)
|
||||||
if !ok {
|
if !ok {
|
||||||
return Date{}
|
return Date{}
|
||||||
}
|
}
|
||||||
daysToAdd := int(newWeekday) - int(weekday)
|
daysToAdd := findDaysToWeekday(Today.Weekday(), newWeekday)
|
||||||
if daysToAdd <= 0 {
|
|
||||||
daysToAdd += 7
|
|
||||||
}
|
|
||||||
|
|
||||||
return Today.Add(daysToAdd)
|
return Today.Add(daysToAdd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Date) String() string {
|
func findDaysToWeekday(current, wanted time.Weekday) int {
|
||||||
|
daysToAdd := int(wanted) - int(current)
|
||||||
|
if daysToAdd <= 0 {
|
||||||
|
daysToAdd += 7
|
||||||
|
}
|
||||||
|
|
||||||
|
return daysToAdd
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Date) DaysBetween(d2 Date) int {
|
||||||
|
tDate := d2
|
||||||
|
end := d
|
||||||
|
if !end.After(tDate) {
|
||||||
|
end = d2
|
||||||
|
tDate = d
|
||||||
|
}
|
||||||
|
|
||||||
|
days := 0
|
||||||
|
for {
|
||||||
|
if tDate.Add(days).Equal(end) {
|
||||||
|
return days
|
||||||
|
}
|
||||||
|
days++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Date) String() string {
|
||||||
if d.t.IsZero() {
|
if d.t.IsZero() {
|
||||||
return "no date"
|
return "no date"
|
||||||
}
|
}
|
||||||
|
@ -103,33 +159,33 @@ func (d *Date) String() string {
|
||||||
return strings.ToLower(d.t.Format(DateFormat))
|
return strings.ToLower(d.t.Format(DateFormat))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Date) IsZero() bool {
|
func (d Date) IsZero() bool {
|
||||||
return d.t.IsZero()
|
return d.t.IsZero()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Date) Time() time.Time {
|
func (d Date) Time() time.Time {
|
||||||
return d.t
|
return d.t
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Date) Weekday() time.Weekday {
|
func (d Date) Weekday() time.Weekday {
|
||||||
return d.t.Weekday()
|
return d.t.Weekday()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Date) Add(days int) Date {
|
func (d Date) Add(days int) Date {
|
||||||
year, month, day := d.t.Date()
|
year, month, day := d.t.Date()
|
||||||
return NewDate(year, int(month), day+days)
|
return NewDate(year, int(month), day+days)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Date) Equal(ud Date) bool {
|
func (d Date) Equal(ud Date) bool {
|
||||||
return d.t.Equal(ud.Time())
|
return d.t.Equal(ud.Time())
|
||||||
}
|
}
|
||||||
|
|
||||||
// After reports whether d is after ud
|
// After reports whether d is after ud
|
||||||
func (d *Date) After(ud Date) bool {
|
func (d Date) After(ud Date) bool {
|
||||||
return d.t.After(ud.Time())
|
return d.t.After(ud.Time())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Date) AddDays(amount int) Date {
|
func (d Date) AddDays(amount int) Date {
|
||||||
year, month, date := d.t.Date()
|
year, month, date := d.t.Date()
|
||||||
|
|
||||||
return NewDate(year, int(month), date+amount)
|
return NewDate(year, int(month), date+amount)
|
||||||
|
|
|
@ -44,7 +44,7 @@ func TestNewDateFromString(t *testing.T) {
|
||||||
exp: task.NewDate(2021, 2, 1),
|
exp: task.NewDate(2021, 2, 1),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "ducth dayname lowercase",
|
name: "dutch dayname lowercase",
|
||||||
input: "maandag",
|
input: "maandag",
|
||||||
exp: task.NewDate(2021, 2, 1),
|
exp: task.NewDate(2021, 2, 1),
|
||||||
},
|
},
|
||||||
|
@ -68,6 +68,46 @@ func TestNewDateFromString(t *testing.T) {
|
||||||
input: "morgen",
|
input: "morgen",
|
||||||
exp: task.NewDate(2021, 1, 31),
|
exp: task.NewDate(2021, 1, 31),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "day after tomorrow english",
|
||||||
|
input: "day after tomorrow",
|
||||||
|
exp: task.NewDate(2021, 2, 1),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "day after tomorrow dutch",
|
||||||
|
input: "overmorgen",
|
||||||
|
exp: task.NewDate(2021, 2, 1),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "this week english",
|
||||||
|
input: "this week",
|
||||||
|
exp: task.NewDate(2021, 2, 5),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "this week dutch",
|
||||||
|
input: "deze week",
|
||||||
|
exp: task.NewDate(2021, 2, 5),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "next week english",
|
||||||
|
input: "next week",
|
||||||
|
exp: task.NewDate(2021, 2, 12),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "next week dutch",
|
||||||
|
input: "volgende week",
|
||||||
|
exp: task.NewDate(2021, 2, 12),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "this sprint english",
|
||||||
|
input: "this sprint",
|
||||||
|
exp: task.NewDate(2021, 2, 11),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "this sprint dutch",
|
||||||
|
input: "deze sprint",
|
||||||
|
exp: task.NewDate(2021, 2, 11),
|
||||||
|
},
|
||||||
} {
|
} {
|
||||||
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