From 82c35926652b46220c4aa7273175b8438848bb3a Mon Sep 17 00:00:00 2001 From: Aviad Lichtenstadt Date: Thu, 17 Jan 2019 16:38:02 +0200 Subject: [PATCH 1/2] Make today/tomorrow/yesterday at the beginning of the day We sero out hour/minutes/seconds --- rules/en/casual_date.go | 10 +++++++++- rules/en/casual_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/rules/en/casual_date.go b/rules/en/casual_date.go index d1b5144..734a86c 100644 --- a/rules/en/casual_date.go +++ b/rules/en/casual_date.go @@ -24,15 +24,23 @@ func CasualDate(s rules.Strategy) rules.Rule { c.Minute = pointer.ToInt(0) } case strings.Contains(lower, "today"): - // c.Hour = pointer.ToInt(18) + c.Hour = pointer.ToInt(0) + c.Minute = pointer.ToInt(0) + c.Second = pointer.ToInt(0) case strings.Contains(lower, "tomorrow"), strings.Contains(lower, "tmr"): if c.Duration == 0 || overwrite { c.Duration += time.Hour * 24 } + c.Hour = pointer.ToInt(0) + c.Minute = pointer.ToInt(0) + c.Second = pointer.ToInt(0) case strings.Contains(lower, "yesterday"): if c.Duration == 0 || overwrite { c.Duration -= time.Hour * 24 } + c.Hour = pointer.ToInt(0) + c.Minute = pointer.ToInt(0) + c.Second = pointer.ToInt(0) case strings.Contains(lower, "last night"): if (c.Hour == nil && c.Duration == 0) || overwrite { c.Hour = pointer.ToInt(23) diff --git a/rules/en/casual_test.go b/rules/en/casual_test.go index 533807f..2019c74 100644 --- a/rules/en/casual_test.go +++ b/rules/en/casual_test.go @@ -1,12 +1,14 @@ package en_test import ( + "github.com/demisto/when/rules/common" "testing" "time" "github.com/olebedev/when" "github.com/olebedev/when/rules" "github.com/olebedev/when/rules/en" + "github.com/stretchr/testify/assert" ) func TestCasualDate(t *testing.T) { @@ -24,6 +26,42 @@ func TestCasualDate(t *testing.T) { ApplyFixtures(t, "en.CasualDate", w, fixt) } +func TestTodayTomorrowYesterday(t *testing.T) { + var w = when.New(nil) + w.Add(en.All...) + w.Add(common.All...) + now := time.Now() + r, err := w.Parse("today", now) + assert.EqualValues(t, now.Year(), r.Time.Year()) + assert.EqualValues(t, now.Month(), r.Time.Month()) + assert.EqualValues(t, now.Day(), r.Time.Day()) + assert.EqualValues(t, 0, r.Time.Minute()) + assert.EqualValues(t, 0, r.Time.Hour()) + assert.EqualValues(t, 0, r.Time.Second()) + r, err = w.Parse("yesterday", now) + if err != nil { + t.Fail() + } + assert.EqualValues(t, now.Year(), r.Time.Year()) + assert.EqualValues(t, now.Month(), r.Time.Month()) + assert.EqualValues(t, now.Day()-1, r.Time.Day()) + assert.EqualValues(t, 0, r.Time.Minute()) + assert.EqualValues(t, 0, r.Time.Hour()) + assert.EqualValues(t, 0, r.Time.Second()) + + r, err = w.Parse("tomorrow", now) + if err != nil { + t.Fail() + } + assert.EqualValues(t, now.Year(), r.Time.Year()) + assert.EqualValues(t, now.Month(), r.Time.Month()) + assert.EqualValues(t, now.Day()+1, r.Time.Day()) + assert.EqualValues(t, 0, r.Time.Minute()) + assert.EqualValues(t, 0, r.Time.Hour()) + assert.EqualValues(t, 0, r.Time.Second()) + +} + func TestCasualTime(t *testing.T) { fixt := []Fixture{ {"The Deadline was this morning ", 17, "this morning", 8 * time.Hour}, From 1447fa5fe16c5ca566a499194933642bd47bf593 Mon Sep 17 00:00:00 2001 From: Aviad Lichtenstadt Date: Wed, 20 Nov 2019 00:11:12 +0200 Subject: [PATCH 2/2] Update rules/en/casual_test.go Co-Authored-By: Oleg Lebedev --- rules/en/casual_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/en/casual_test.go b/rules/en/casual_test.go index 2019c74..636549a 100644 --- a/rules/en/casual_test.go +++ b/rules/en/casual_test.go @@ -1,7 +1,7 @@ package en_test import ( - "github.com/demisto/when/rules/common" + "github.com/olebedev/when/rules/common" "testing" "time"