aboutsummaryrefslogtreecommitdiffstats
path: root/worker/lib/daterange_test.go
diff options
context:
space:
mode:
authorKoni Marti <koni.marti@gmail.com>2022-11-15 21:24:52 +0100
committerRobin Jarry <robin@jarry.cc>2022-12-02 22:59:44 +0100
commitd547dca676553a83c077b34f735c9d2ff5d41dcd (patch)
tree0d65703bc29711eda99a9ed8119304bbcd7958e7 /worker/lib/daterange_test.go
parentce7fbd27ee8f41adfa8e33002ccf965c6a917e5a (diff)
downloadaerc-d547dca676553a83c077b34f735c9d2ff5d41dcd.tar.gz
daterange: support relative terms
Support relative terms when writing date ranges in the search and filter commands with the -d flag. Syntax is inspired by the notmuch search terms. Terms can be written with spaces or underscores for a better readability, so both "this_week" and "this week" are allowed. Terms are not case-sensitive. Some terms can be prefixed with either "this" or "last" where applicable ("this" is assumed by default if omitted): - "today", "yesterday" - ("this"|"last") "year", "month", "week" - all weekdays (e.g. "Tuesday", "last_wed") - all months (e.g. "January", "last_feb") Note that "month" should always be spelled out to prevent a possible ambiguity with "Monday". Weekdays and months do not need to be written out completely, i.e. "February..March" and "Feb..Mar" are both understood. Relative date terms can be used with the <N (year|month|week|day)> syntax where N is a positive integer indicating the number of time units in the past from today. The units can be abbreviated with a single letter, e.g. "1w 1d.." is the same as "1 week 1 day..". More examples: :filter -d yesterday :filter -d last_monday.. :filter -d mon..sat :filter -d 1y1m1w1d.. :search -d this_week "PATCH aerc" Signed-off-by: Koni Marti <koni.marti@gmail.com> Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'worker/lib/daterange_test.go')
-rw-r--r--worker/lib/daterange_test.go42
1 files changed, 42 insertions, 0 deletions
diff --git a/worker/lib/daterange_test.go b/worker/lib/daterange_test.go
index 983f99a0..807e7ac7 100644
--- a/worker/lib/daterange_test.go
+++ b/worker/lib/daterange_test.go
@@ -1,6 +1,7 @@
package lib_test
import (
+ "reflect"
"testing"
"time"
@@ -53,3 +54,44 @@ func TestParseDateRange(t *testing.T) {
}
}
}
+
+func TestParseRelativeDate(t *testing.T) {
+ tests := []struct {
+ s string
+ want lib.RelDate
+ }{
+ {
+ s: "5 weeks 1 day",
+ want: lib.RelDate{Year: 0, Month: 0, Day: 5*7 + 1},
+ },
+ {
+ s: "5_weeks 1_day",
+ want: lib.RelDate{Year: 0, Month: 0, Day: 5*7 + 1},
+ },
+ {
+ s: "5weeks1day",
+ want: lib.RelDate{Year: 0, Month: 0, Day: 5*7 + 1},
+ },
+ {
+ s: "5w1d",
+ want: lib.RelDate{Year: 0, Month: 0, Day: 5*7 + 1},
+ },
+ {
+ s: "5y4m3w1d",
+ want: lib.RelDate{Year: 5, Month: 4, Day: 3*7 + 1},
+ },
+ }
+
+ for _, test := range tests {
+ da, err := lib.ParseRelativeDate(test.s)
+ if err != nil {
+ t.Errorf("ParseRelativeDate return error for %s: %v",
+ test.s, err)
+ }
+
+ if !reflect.DeepEqual(da, test.want) {
+ t.Errorf("results don't match. expected %v, got %v",
+ test.want, da)
+ }
+ }
+}