diff --git a/core/date/suggest.go b/core/date/suggest.go index 57551f0e1..fb8eaa030 100644 --- a/core/date/suggest.go +++ b/core/date/suggest.go @@ -21,6 +21,20 @@ import ( "github.com/anyproto/anytype-heart/util/pbtypes" ) +var literals []string + +func init() { + literals = []string{"today", "now", "yesterday", "tomorrow"} + + for i := 0; i < 6; i++ { + literals = append(literals, strings.ToLower(time.Weekday(i).String())) + } + + for i := 1; i < 12; i++ { + literals = append(literals, strings.ToLower(time.Month(i).String())) + } +} + func EnrichRecordsWithDateSuggestions( ctx context.Context, spaceId, fullText string, @@ -92,10 +106,19 @@ func suggestDateForSearch(now time.Time, raw string) time.Time { return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) } + if len(raw) > 1 { + for _, variant := range literals { + if strings.Contains(variant, strings.ToLower(raw)) { + raw = variant + break + } + } + } + suggesters := []func() time.Time{ func() time.Time { var exprType naturaldate.ExprType - t, exprType, err := naturaldate.Parse(raw, now) + t, exprType, err := naturaldate.Parse(raw, now, naturaldate.WithDirection(naturaldate.Future)) if err != nil { return time.Time{} } diff --git a/core/date/suggest_test.go b/core/date/suggest_test.go index 2630d2f76..9ea584d3b 100644 --- a/core/date/suggest_test.go +++ b/core/date/suggest_test.go @@ -33,6 +33,22 @@ func Test_suggestDateForSearch(t *testing.T) { raw: "today", want: time.Date(2022, 5, 18, 0, 0, 0, 0, loc), }, + { + raw: "toda", + want: time.Date(2022, 5, 18, 0, 0, 0, 0, loc), + }, + { + raw: "to", + want: time.Date(2022, 5, 18, 0, 0, 0, 0, loc), + }, + { + raw: "yeste", + want: time.Date(2022, 5, 17, 0, 0, 0, 0, loc), + }, + { + raw: "NoVem", + want: time.Date(2022, 11, 18, 14, 56, 33, 0, loc), + }, { raw: "10 days from now", want: time.Date(2022, 5, 28, 14, 56, 33, 0, loc), diff --git a/util/dateutil/util.go b/util/dateutil/util.go index d432f9cd0..b4ab60d37 100644 --- a/util/dateutil/util.go +++ b/util/dateutil/util.go @@ -10,8 +10,8 @@ import ( const ( shortDateIdLayout = "2006-01-02" dateIdLayout = "2006-01-02-15-04-05Z-0700" - shortDateNameLayout = "Mon, Jan 02, 2006" - dateNameLayout = "Mon, Jan 02, 2006 3:04 PM" + shortDateNameLayout = "Mon, Jan 2, 2006" + dateNameLayout = "Mon, Jan 2, 2006 3:04 PM" ) type DateObject interface { diff --git a/util/dateutil/util_test.go b/util/dateutil/util_test.go index 032a82252..f4308d51b 100644 --- a/util/dateutil/util_test.go +++ b/util/dateutil/util_test.go @@ -17,17 +17,17 @@ func TestNewDateObject(t *testing.T) { { ts: time.Date(2024, time.November, 7, 12, 25, 59, 0, time.UTC), expectedId: "_date_2024-11-07-12-25-59Z_0000", - expectedName: "Thu, Nov 07, 2024 12:25 PM", + expectedName: "Thu, Nov 7, 2024 12:25 PM", }, { ts: time.Date(1998, time.January, 1, 0, 1, 1, 0, time.UTC), expectedId: "_date_1998-01-01-00-01-01Z_0000", - expectedName: "Thu, Jan 01, 1998 12:01 AM", + expectedName: "Thu, Jan 1, 1998 12:01 AM", }, { ts: time.Date(1998, time.January, 1, 0, 1, 1, 0, time.FixedZone("UTC", +4*60*60)), expectedId: "_date_1998-01-01-00-01-01Z_0400", - expectedName: "Thu, Jan 01, 1998 12:01 AM", + expectedName: "Thu, Jan 1, 1998 12:01 AM", }, { ts: time.Date(2124, time.December, 25, 23, 34, 0, 0, time.UTC), @@ -56,17 +56,17 @@ func TestNewDateObject(t *testing.T) { { ts: time.Date(2024, time.November, 7, 12, 25, 59, 0, time.UTC), expectedId: "_date_2024-11-07", - expectedName: "Thu, Nov 07, 2024", + expectedName: "Thu, Nov 7, 2024", }, { ts: time.Date(1998, time.January, 1, 0, 1, 1, 0, time.UTC), expectedId: "_date_1998-01-01", - expectedName: "Thu, Jan 01, 1998", + expectedName: "Thu, Jan 1, 1998", }, { ts: time.Date(1998, time.January, 1, 0, 1, 1, 0, time.FixedZone("UTC", +4*60*60)), expectedId: "_date_1998-01-01", - expectedName: "Thu, Jan 01, 1998", + expectedName: "Thu, Jan 1, 1998", }, { ts: time.Date(2124, time.December, 25, 23, 34, 0, 0, time.UTC),