refactor today to function
This commit is contained in:
parent
31ebd26f9e
commit
bbf594f37d
|
@ -21,7 +21,7 @@ func NewToday(conf *configuration.Configuration) (*Today, error) {
|
||||||
return &Today{}, err
|
return &Today{}, err
|
||||||
}
|
}
|
||||||
reqs := process.ListReqs{
|
reqs := process.ListReqs{
|
||||||
Due: task.Today,
|
Due: task.Today(),
|
||||||
IncludeBefore: true,
|
IncludeBefore: true,
|
||||||
ApplyUpdates: true,
|
ApplyUpdates: true,
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,7 @@ func NewTomorrow(conf *configuration.Configuration) (*Tomorrow, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
reqs := process.ListReqs{
|
reqs := process.ListReqs{
|
||||||
Due: task.Today.Add(1),
|
Due: task.Today().Add(1),
|
||||||
ApplyUpdates: true,
|
ApplyUpdates: true,
|
||||||
}
|
}
|
||||||
lister := process.NewList(local, reqs)
|
lister := process.NewList(local, reqs)
|
||||||
|
|
|
@ -21,7 +21,7 @@ func NewWeek(conf *configuration.Configuration) (*Week, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
reqs := process.ListReqs{
|
reqs := process.ListReqs{
|
||||||
Due: task.Today.Add(7),
|
Due: task.Today().Add(7),
|
||||||
IncludeBefore: true,
|
IncludeBefore: true,
|
||||||
ApplyUpdates: true,
|
ApplyUpdates: true,
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,7 +50,7 @@ func FormatTaskTable(tasks []*task.LocalTask, cols []Column) string {
|
||||||
if t.LocalStatus == task.STATUS_UPDATED {
|
if t.LocalStatus == task.STATUS_UPDATED {
|
||||||
updated = append(updated, "u")
|
updated = append(updated, "u")
|
||||||
}
|
}
|
||||||
if !t.Due.IsZero() && task.Today.After(t.Due) {
|
if !t.Due.IsZero() && task.Today().After(t.Due) {
|
||||||
updated = append(updated, "o")
|
updated = append(updated, "o")
|
||||||
}
|
}
|
||||||
line = append(line, strings.Join(updated, " "))
|
line = append(line, strings.Join(updated, " "))
|
||||||
|
|
|
@ -54,7 +54,7 @@ func Run(inboxProc *process.Inbox, recurProc *process.Recur, logger log.Logger)
|
||||||
logger = logger.WithField("func", "run")
|
logger = logger.WithField("func", "run")
|
||||||
inboxTicker := time.NewTicker(30 * time.Second)
|
inboxTicker := time.NewTicker(30 * time.Second)
|
||||||
recurTicker := time.NewTicker(time.Hour)
|
recurTicker := time.NewTicker(time.Hour)
|
||||||
oldToday := task.Today
|
oldToday := task.Today()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
@ -69,14 +69,12 @@ func Run(inboxProc *process.Inbox, recurProc *process.Recur, logger log.Logger)
|
||||||
logger.WithField("result", result).Info("finished processing inbox")
|
logger.WithField("result", result).Info("finished processing inbox")
|
||||||
}
|
}
|
||||||
case <-recurTicker.C:
|
case <-recurTicker.C:
|
||||||
year, month, day := time.Now().Date()
|
if oldToday.Equal(task.Today()) {
|
||||||
task.Today = task.NewDate(year, int(month), day)
|
|
||||||
if oldToday.Equal(task.Today) {
|
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
oldToday = task.NewDate(year, int(month), day)
|
oldToday = task.Today()
|
||||||
result, err := recurProc.Process()
|
result, err := recurProc.Process()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.WithErr(err).Error("failed generating recurring tasks")
|
logger.WithErr(err).Error("failed generating recurring tasks")
|
||||||
|
|
|
@ -47,7 +47,7 @@ func (recur *Recur) Process() (*RecurResult, error) {
|
||||||
return &RecurResult{}, fmt.Errorf("%w: %v", ErrRecurProcess, err)
|
return &RecurResult{}, fmt.Errorf("%w: %v", ErrRecurProcess, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
rDate := task.Today.AddDays(recur.daysAhead)
|
rDate := task.Today().AddDays(recur.daysAhead)
|
||||||
var count int
|
var count int
|
||||||
for _, t := range tasks {
|
for _, t := range tasks {
|
||||||
if t.RecursOn(rDate) {
|
if t.RecursOn(rDate) {
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package process_test
|
package process_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"ewintr.nl/go-kit/test"
|
"ewintr.nl/go-kit/test"
|
||||||
"ewintr.nl/gte/internal/process"
|
"ewintr.nl/gte/internal/process"
|
||||||
|
@ -12,7 +14,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRecurProcess(t *testing.T) {
|
func TestRecurProcess(t *testing.T) {
|
||||||
task.Today = task.NewDate(2021, 5, 14)
|
strFormat := "2006-01-02"
|
||||||
|
todayStr := time.Now().Format(strFormat)
|
||||||
|
nextMonthStr := time.Now().Add(30 * 24 * time.Hour).Format(strFormat)
|
||||||
|
tomorrowStr := task.Today().Add(1).String()
|
||||||
for _, tc := range []struct {
|
for _, tc := range []struct {
|
||||||
name string
|
name string
|
||||||
recurMsgs []*mstore.Message
|
recurMsgs []*mstore.Message
|
||||||
|
@ -27,17 +32,17 @@ func TestRecurProcess(t *testing.T) {
|
||||||
name: "one of two recurring",
|
name: "one of two recurring",
|
||||||
recurMsgs: []*mstore.Message{
|
recurMsgs: []*mstore.Message{
|
||||||
{
|
{
|
||||||
Subject: "not recurring",
|
Subject: "recurring",
|
||||||
Body: "recur: 2021-05-20, daily\nid: xxx-xxx\nversion: 1",
|
Body: fmt.Sprintf("recur: %s, daily\nid: xxx-xxx\nversion: 1", todayStr),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Subject: "recurring",
|
Subject: "not recurring",
|
||||||
Body: "recur: 2021-05-10, daily\nid: xxx-xxx\nversion: 1",
|
Body: fmt.Sprintf("recur: %s, daily\nid: xxx-xxx\nversion: 1", nextMonthStr),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expCount: 1,
|
expCount: 1,
|
||||||
expMsgs: []*msend.Message{
|
expMsgs: []*msend.Message{
|
||||||
{Subject: "2021-05-15 (saturday) - recurring"},
|
{Subject: fmt.Sprintf("%s - recurring", tomorrowStr)},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
|
|
|
@ -11,11 +11,9 @@ const (
|
||||||
DateFormat = "2006-01-02 (Monday)"
|
DateFormat = "2006-01-02 (Monday)"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Today Date
|
func Today() Date {
|
||||||
|
|
||||||
func init() {
|
|
||||||
year, month, day := time.Now().Date()
|
year, month, day := time.Now().Date()
|
||||||
Today = NewDate(year, int(month), day)
|
return NewDate(year, int(month), day)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Weekdays []time.Weekday
|
type Weekdays []time.Weekday
|
||||||
|
@ -104,36 +102,10 @@ func NewDateFromString(date string) Date {
|
||||||
return Date{}
|
return Date{}
|
||||||
|
|
||||||
case "today":
|
case "today":
|
||||||
return Today
|
return Today()
|
||||||
|
|
||||||
case "tomorrow":
|
case "tomorrow":
|
||||||
return Today.AddDays(1)
|
return Today().AddDays(1)
|
||||||
|
|
||||||
case "day after tomorrow":
|
|
||||||
fallthrough
|
|
||||||
case "day-after-tomorrow":
|
|
||||||
return Today.AddDays(2)
|
|
||||||
|
|
||||||
case "this week":
|
|
||||||
fallthrough
|
|
||||||
case "this-week":
|
|
||||||
daysToAdd := findDaysToWeekday(Today.Weekday(), time.Friday)
|
|
||||||
return Today.Add(daysToAdd)
|
|
||||||
|
|
||||||
case "next week":
|
|
||||||
fallthrough
|
|
||||||
case "next-week":
|
|
||||||
daysToAdd := findDaysToWeekday(Today.Weekday(), time.Friday) + 7
|
|
||||||
return Today.Add(daysToAdd)
|
|
||||||
|
|
||||||
case "this sprint":
|
|
||||||
tDate := NewDate(2021, 1, 28) // a sprint end
|
|
||||||
for {
|
|
||||||
if tDate.After(Today) {
|
|
||||||
return tDate
|
|
||||||
}
|
|
||||||
tDate = tDate.AddDays(14)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
t, err := time.Parse("2006-01-02", fmt.Sprintf("%.10s", date))
|
t, err := time.Parse("2006-01-02", fmt.Sprintf("%.10s", date))
|
||||||
|
@ -145,9 +117,9 @@ func NewDateFromString(date string) Date {
|
||||||
if !ok {
|
if !ok {
|
||||||
return Date{}
|
return Date{}
|
||||||
}
|
}
|
||||||
daysToAdd := findDaysToWeekday(Today.Weekday(), newWeekday)
|
daysToAdd := findDaysToWeekday(Today().Weekday(), newWeekday)
|
||||||
|
|
||||||
return Today.Add(daysToAdd)
|
return Today().Add(daysToAdd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func findDaysToWeekday(current, wanted time.Weekday) int {
|
func findDaysToWeekday(current, wanted time.Weekday) int {
|
||||||
|
@ -188,11 +160,11 @@ func (d Date) Human() string {
|
||||||
switch {
|
switch {
|
||||||
case d.IsZero():
|
case d.IsZero():
|
||||||
return "-"
|
return "-"
|
||||||
case d.Equal(Today):
|
case d.Equal(Today()):
|
||||||
return "today"
|
return "today"
|
||||||
case d.Equal(Today.Add(1)):
|
case d.Equal(Today().Add(1)):
|
||||||
return "tomorrow"
|
return "tomorrow"
|
||||||
case d.After(Today) && Today.Add(8).After(d):
|
case d.After(Today()) && Today().Add(8).After(d):
|
||||||
return strings.ToLower(d.t.Format("Monday"))
|
return strings.ToLower(d.t.Format("Monday"))
|
||||||
default:
|
default:
|
||||||
return strings.ToLower(d.t.Format(DateFormat))
|
return strings.ToLower(d.t.Format(DateFormat))
|
||||||
|
|
|
@ -76,7 +76,6 @@ func TestWeekdaysUnique(t *testing.T) {
|
||||||
|
|
||||||
func TestNewDateFromString(t *testing.T) {
|
func TestNewDateFromString(t *testing.T) {
|
||||||
t.Run("no date", func(t *testing.T) {
|
t.Run("no date", func(t *testing.T) {
|
||||||
task.Today = task.NewDate(2021, 1, 30)
|
|
||||||
for _, tc := range []struct {
|
for _, tc := range []struct {
|
||||||
name string
|
name string
|
||||||
input string
|
input string
|
||||||
|
@ -99,7 +98,6 @@ func TestNewDateFromString(t *testing.T) {
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("digits", func(t *testing.T) {
|
t.Run("digits", func(t *testing.T) {
|
||||||
task.Today = task.NewDate(2021, 1, 30)
|
|
||||||
for _, tc := range []struct {
|
for _, tc := range []struct {
|
||||||
name string
|
name string
|
||||||
input string
|
input string
|
||||||
|
@ -124,7 +122,13 @@ func TestNewDateFromString(t *testing.T) {
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("day name", func(t *testing.T) {
|
t.Run("day name", func(t *testing.T) {
|
||||||
task.Today = task.NewDate(2021, 1, 30)
|
monday := task.Today().Add(1)
|
||||||
|
for {
|
||||||
|
if monday.Weekday() == time.Monday {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
monday = monday.Add(1)
|
||||||
|
}
|
||||||
for _, tc := range []struct {
|
for _, tc := range []struct {
|
||||||
name string
|
name string
|
||||||
input string
|
input string
|
||||||
|
@ -133,45 +137,30 @@ func TestNewDateFromString(t *testing.T) {
|
||||||
{
|
{
|
||||||
name: "dayname lowercase",
|
name: "dayname lowercase",
|
||||||
input: "monday",
|
input: "monday",
|
||||||
exp: task.NewDate(2021, 2, 1),
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "dayname capitalized",
|
name: "dayname capitalized",
|
||||||
input: "Monday",
|
input: "Monday",
|
||||||
exp: task.NewDate(2021, 2, 1),
|
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
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, monday, task.NewDateFromString(tc.input))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("relative days", func(t *testing.T) {
|
t.Run("relative days", func(t *testing.T) {
|
||||||
task.Today = task.NewDate(2021, 1, 30)
|
|
||||||
for _, tc := range []struct {
|
for _, tc := range []struct {
|
||||||
name string
|
name string
|
||||||
exp task.Date
|
exp task.Date
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "today",
|
name: "today",
|
||||||
exp: task.NewDate(2021, 1, 30),
|
exp: task.Today(),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "tomorrow",
|
name: "tomorrow",
|
||||||
exp: task.NewDate(2021, 1, 31),
|
exp: task.Today().Add(1),
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "day after tomorrow",
|
|
||||||
exp: task.NewDate(2021, 2, 1),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "this week",
|
|
||||||
exp: task.NewDate(2021, 2, 5),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "next week",
|
|
||||||
exp: task.NewDate(2021, 2, 12),
|
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
@ -179,37 +168,6 @@ func TestNewDateFromString(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("sprint", func(t *testing.T) {
|
|
||||||
for _, tc := range []struct {
|
|
||||||
name string
|
|
||||||
today task.Date
|
|
||||||
input string
|
|
||||||
exp task.Date
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "this sprint",
|
|
||||||
today: task.NewDate(2021, 1, 30),
|
|
||||||
input: "this sprint",
|
|
||||||
exp: task.NewDate(2021, 2, 11),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "jump week",
|
|
||||||
today: task.NewDate(2021, 2, 5),
|
|
||||||
input: "this sprint",
|
|
||||||
exp: task.NewDate(2021, 2, 11),
|
|
||||||
},
|
|
||||||
} {
|
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
|
||||||
task.Today = tc.today
|
|
||||||
test.Equals(t, tc.exp, task.NewDateFromString(tc.input))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("empty", func(t *testing.T) {
|
|
||||||
test.Equals(t, task.Date{}, task.NewDateFromString("test"))
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDateDaysBetween(t *testing.T) {
|
func TestDateDaysBetween(t *testing.T) {
|
||||||
|
@ -284,7 +242,7 @@ func TestDateString(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDateHuman(t *testing.T) {
|
func TestDateHuman(t *testing.T) {
|
||||||
monday := task.Today.Add(1)
|
monday := task.Today().Add(1)
|
||||||
for {
|
for {
|
||||||
if monday.Weekday() == time.Monday {
|
if monday.Weekday() == time.Monday {
|
||||||
break
|
break
|
||||||
|
@ -314,12 +272,12 @@ func TestDateHuman(t *testing.T) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "today",
|
name: "today",
|
||||||
date: task.Today,
|
date: task.Today(),
|
||||||
exp: "today",
|
exp: "today",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "tomorrow",
|
name: "tomorrow",
|
||||||
date: task.Today.Add(1),
|
date: task.Today().Add(1),
|
||||||
exp: "tomorrow",
|
exp: "tomorrow",
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
|
|
|
@ -209,7 +209,7 @@ func (t *Task) IsRecurrer() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Task) RecursToday() bool {
|
func (t *Task) RecursToday() bool {
|
||||||
return t.RecursOn(Today)
|
return t.RecursOn(Today())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Task) RecursOn(date Date) bool {
|
func (t *Task) RecursOn(date Date) bool {
|
||||||
|
|
Loading…
Reference in New Issue