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
|
||||
}
|
||||
reqs := process.ListReqs{
|
||||
Due: task.Today,
|
||||
Due: task.Today(),
|
||||
IncludeBefore: true,
|
||||
ApplyUpdates: true,
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@ func NewTomorrow(conf *configuration.Configuration) (*Tomorrow, error) {
|
|||
}
|
||||
|
||||
reqs := process.ListReqs{
|
||||
Due: task.Today.Add(1),
|
||||
Due: task.Today().Add(1),
|
||||
ApplyUpdates: true,
|
||||
}
|
||||
lister := process.NewList(local, reqs)
|
||||
|
|
|
@ -21,7 +21,7 @@ func NewWeek(conf *configuration.Configuration) (*Week, error) {
|
|||
}
|
||||
|
||||
reqs := process.ListReqs{
|
||||
Due: task.Today.Add(7),
|
||||
Due: task.Today().Add(7),
|
||||
IncludeBefore: true,
|
||||
ApplyUpdates: true,
|
||||
}
|
||||
|
|
|
@ -50,7 +50,7 @@ func FormatTaskTable(tasks []*task.LocalTask, cols []Column) string {
|
|||
if t.LocalStatus == task.STATUS_UPDATED {
|
||||
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")
|
||||
}
|
||||
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")
|
||||
inboxTicker := time.NewTicker(30 * time.Second)
|
||||
recurTicker := time.NewTicker(time.Hour)
|
||||
oldToday := task.Today
|
||||
oldToday := task.Today()
|
||||
|
||||
for {
|
||||
select {
|
||||
|
@ -69,14 +69,12 @@ func Run(inboxProc *process.Inbox, recurProc *process.Recur, logger log.Logger)
|
|||
logger.WithField("result", result).Info("finished processing inbox")
|
||||
}
|
||||
case <-recurTicker.C:
|
||||
year, month, day := time.Now().Date()
|
||||
task.Today = task.NewDate(year, int(month), day)
|
||||
if oldToday.Equal(task.Today) {
|
||||
if oldToday.Equal(task.Today()) {
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
oldToday = task.NewDate(year, int(month), day)
|
||||
oldToday = task.Today()
|
||||
result, err := recurProc.Process()
|
||||
if err != nil {
|
||||
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)
|
||||
}
|
||||
|
||||
rDate := task.Today.AddDays(recur.daysAhead)
|
||||
rDate := task.Today().AddDays(recur.daysAhead)
|
||||
var count int
|
||||
for _, t := range tasks {
|
||||
if t.RecursOn(rDate) {
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
package process_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"ewintr.nl/go-kit/test"
|
||||
"ewintr.nl/gte/internal/process"
|
||||
|
@ -12,7 +14,10 @@ import (
|
|||
)
|
||||
|
||||
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 {
|
||||
name string
|
||||
recurMsgs []*mstore.Message
|
||||
|
@ -27,17 +32,17 @@ func TestRecurProcess(t *testing.T) {
|
|||
name: "one of two recurring",
|
||||
recurMsgs: []*mstore.Message{
|
||||
{
|
||||
Subject: "not recurring",
|
||||
Body: "recur: 2021-05-20, daily\nid: xxx-xxx\nversion: 1",
|
||||
Subject: "recurring",
|
||||
Body: fmt.Sprintf("recur: %s, daily\nid: xxx-xxx\nversion: 1", todayStr),
|
||||
},
|
||||
{
|
||||
Subject: "recurring",
|
||||
Body: "recur: 2021-05-10, daily\nid: xxx-xxx\nversion: 1",
|
||||
Subject: "not recurring",
|
||||
Body: fmt.Sprintf("recur: %s, daily\nid: xxx-xxx\nversion: 1", nextMonthStr),
|
||||
},
|
||||
},
|
||||
expCount: 1,
|
||||
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)"
|
||||
)
|
||||
|
||||
var Today Date
|
||||
|
||||
func init() {
|
||||
func Today() Date {
|
||||
year, month, day := time.Now().Date()
|
||||
Today = NewDate(year, int(month), day)
|
||||
return NewDate(year, int(month), day)
|
||||
}
|
||||
|
||||
type Weekdays []time.Weekday
|
||||
|
@ -104,36 +102,10 @@ func NewDateFromString(date string) Date {
|
|||
return Date{}
|
||||
|
||||
case "today":
|
||||
return Today
|
||||
return Today()
|
||||
|
||||
case "tomorrow":
|
||||
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)
|
||||
}
|
||||
return Today().AddDays(1)
|
||||
}
|
||||
|
||||
t, err := time.Parse("2006-01-02", fmt.Sprintf("%.10s", date))
|
||||
|
@ -145,9 +117,9 @@ func NewDateFromString(date string) Date {
|
|||
if !ok {
|
||||
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 {
|
||||
|
@ -188,11 +160,11 @@ func (d Date) Human() string {
|
|||
switch {
|
||||
case d.IsZero():
|
||||
return "-"
|
||||
case d.Equal(Today):
|
||||
case d.Equal(Today()):
|
||||
return "today"
|
||||
case d.Equal(Today.Add(1)):
|
||||
case d.Equal(Today().Add(1)):
|
||||
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"))
|
||||
default:
|
||||
return strings.ToLower(d.t.Format(DateFormat))
|
||||
|
|
|
@ -76,7 +76,6 @@ func TestWeekdaysUnique(t *testing.T) {
|
|||
|
||||
func TestNewDateFromString(t *testing.T) {
|
||||
t.Run("no date", func(t *testing.T) {
|
||||
task.Today = task.NewDate(2021, 1, 30)
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
input string
|
||||
|
@ -99,7 +98,6 @@ func TestNewDateFromString(t *testing.T) {
|
|||
})
|
||||
|
||||
t.Run("digits", func(t *testing.T) {
|
||||
task.Today = task.NewDate(2021, 1, 30)
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
input string
|
||||
|
@ -124,7 +122,13 @@ func TestNewDateFromString(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 {
|
||||
name string
|
||||
input string
|
||||
|
@ -133,45 +137,30 @@ func TestNewDateFromString(t *testing.T) {
|
|||
{
|
||||
name: "dayname lowercase",
|
||||
input: "monday",
|
||||
exp: task.NewDate(2021, 2, 1),
|
||||
},
|
||||
{
|
||||
name: "dayname capitalized",
|
||||
input: "Monday",
|
||||
exp: task.NewDate(2021, 2, 1),
|
||||
},
|
||||
} {
|
||||
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) {
|
||||
task.Today = task.NewDate(2021, 1, 30)
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
exp task.Date
|
||||
}{
|
||||
{
|
||||
name: "today",
|
||||
exp: task.NewDate(2021, 1, 30),
|
||||
exp: task.Today(),
|
||||
},
|
||||
{
|
||||
name: "tomorrow",
|
||||
exp: task.NewDate(2021, 1, 31),
|
||||
},
|
||||
{
|
||||
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),
|
||||
exp: task.Today().Add(1),
|
||||
},
|
||||
} {
|
||||
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) {
|
||||
|
@ -284,7 +242,7 @@ func TestDateString(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestDateHuman(t *testing.T) {
|
||||
monday := task.Today.Add(1)
|
||||
monday := task.Today().Add(1)
|
||||
for {
|
||||
if monday.Weekday() == time.Monday {
|
||||
break
|
||||
|
@ -314,12 +272,12 @@ func TestDateHuman(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "today",
|
||||
date: task.Today,
|
||||
date: task.Today(),
|
||||
exp: "today",
|
||||
},
|
||||
{
|
||||
name: "tomorrow",
|
||||
date: task.Today.Add(1),
|
||||
date: task.Today().Add(1),
|
||||
exp: "tomorrow",
|
||||
},
|
||||
} {
|
||||
|
|
|
@ -209,7 +209,7 @@ func (t *Task) IsRecurrer() bool {
|
|||
}
|
||||
|
||||
func (t *Task) RecursToday() bool {
|
||||
return t.RecursOn(Today)
|
||||
return t.RecursOn(Today())
|
||||
}
|
||||
|
||||
func (t *Task) RecursOn(date Date) bool {
|
||||
|
|
Loading…
Reference in New Issue