gte/internal/task/recur.go

77 lines
1.1 KiB
Go
Raw Normal View History

2021-01-23 12:26:26 +01:00
package task
2021-01-31 10:01:03 +01:00
import (
"strings"
"time"
)
2021-01-23 13:43:28 +01:00
type Period int
2021-01-23 12:26:26 +01:00
type Recurrer interface {
2021-01-31 10:01:03 +01:00
RecursOn(date Date) bool
2021-01-23 12:26:26 +01:00
FirstAfter(date Date) Date
2021-01-31 10:01:03 +01:00
String() string
}
func NewRecurrer(recurStr string) Recurrer {
terms := strings.Split(recurStr, ", ")
if len(terms) < 3 {
return nil
}
startDate, err := time.Parse("2006-01-02", terms[0])
if err != nil {
return nil
}
if terms[1] != "weekly" {
return nil
}
if terms[2] != "wednesday" {
return nil
}
year, month, date := startDate.Date()
return Weekly{
Start: NewDate(year, int(month), date),
Weekday: time.Wednesday,
}
2021-01-23 12:26:26 +01:00
}
2021-01-31 10:01:03 +01:00
// yyyy-mm-dd, weekly, wednesday
2021-01-23 12:26:26 +01:00
type Weekly struct {
Start Date
2021-01-31 10:01:03 +01:00
Weekday time.Weekday
}
func (w Weekly) RecursOn(date Date) bool {
if !w.Start.After(date) {
return false
}
return w.Weekday == date.Weekday()
2021-01-23 12:26:26 +01:00
}
2021-01-31 10:01:03 +01:00
func (w Weekly) FirstAfter(date Date) Date {
2021-01-23 13:43:28 +01:00
//sd := w.Start.Weekday()
2021-01-23 12:26:26 +01:00
return date
}
2021-01-31 10:01:03 +01:00
func (w Weekly) String() string {
return "2021-01-31, weekly, wednesday"
}
/*
2021-01-23 12:26:26 +01:00
type BiWeekly struct {
Start Date
Weekday Weekday
}
type RecurringTask struct {
Action string
Start Date
Recurrer Recurrer
}
2021-01-31 10:01:03 +01:00
*/