commit d6f26b54a6b0cf86623fc653eb97d4e840023f14 Author: upstream Date: Sun Sep 27 12:38:39 2026 +0200 Upstream olebedev/when state of the touched files before 978901adc2 diff --git a/rules/context.go b/rules/context.go new file mode 100644 index 0000000..e0a9e2b --- /dev/null +++ b/rules/context.go @@ -0,0 +1,68 @@ +package rules + +import "time" + +type Context struct { + Text string + + // accumulator of relative values + Duration time.Duration + + // Aboslute values + Year, Month, Weekday, Day, Hour, Minute, Second *int + + Location *time.Location +} + +func (c *Context) Time(t time.Time) (time.Time, error) { + if t.IsZero() { + t = time.Now() + } + + if c.Duration != 0 { + t = t.Add(c.Duration) + } + + if c.Year != nil { + t = time.Date(*c.Year, t.Month(), t.Day(), t.Hour(), + t.Minute(), t.Second(), t.Nanosecond(), t.Location()) + } + + if c.Month != nil { + t = time.Date(t.Year(), time.Month(*c.Month), t.Day(), + t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), t.Location()) + } + + if c.Weekday != nil { + diff := int(time.Weekday(*c.Weekday) - t.Weekday()) + t = time.Date(t.Year(), t.Month(), t.Day()+diff, t.Hour(), + t.Minute(), t.Second(), t.Nanosecond(), t.Location()) + } + + if c.Day != nil { + t = time.Date(t.Year(), t.Month(), *c.Day, t.Hour(), + t.Minute(), t.Second(), t.Nanosecond(), t.Location()) + } + + if c.Hour != nil { + t = time.Date(t.Year(), t.Month(), t.Day(), *c.Hour, + t.Minute(), t.Second(), t.Nanosecond(), t.Location()) + } + + if c.Minute != nil { + t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), + *c.Minute, t.Second(), t.Nanosecond(), t.Location()) + } + + if c.Second != nil { + t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), + t.Minute(), *c.Second, t.Nanosecond(), t.Location()) + } + + if c.Location != nil { + t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), + t.Minute(), t.Second(), t.Nanosecond(), c.Location) + } + + return t, nil +} diff --git a/rules/en/exact_month_date_test.go b/rules/en/exact_month_date_test.go new file mode 100644 index 0000000..38b3dff --- /dev/null +++ b/rules/en/exact_month_date_test.go @@ -0,0 +1,39 @@ +package en_test + +import ( + "testing" + "time" + + "github.com/olebedev/when" + "github.com/olebedev/when/rules" + "github.com/olebedev/when/rules/en" +) + +func TestExactMonthDate(t *testing.T) { + w := when.New(nil) + w.Add(en.ExactMonthDate(rules.Override)) + + fixtok := []Fixture{ + {"third of march", 0, "third of march", 1368 * time.Hour}, + {"march third", 0, "march third", 1368 * time.Hour}, + {"march 3rd", 0, "march 3rd", 1368 * time.Hour}, + {"3rd march", 0, "3rd march", 1368 * time.Hour}, + {"march 3", 0, "march 3", 1368 * time.Hour}, + {"1 september", 0, "1 september", 5736 * time.Hour}, + {"1 sept", 0, "1 sept", 5736 * time.Hour}, + {"1 sept.", 0, "1 sept.", 5736 * time.Hour}, + {"1st of september", 0, "1st of september", 5736 * time.Hour}, + {"sept. 1st", 0, "sept. 1st", 5736 * time.Hour}, + {"march 7th", 0, "march 7th", 1464 * time.Hour}, + {"october 21st", 0, "october 21st", 6936 * time.Hour}, + {"twentieth of december", 0, "twentieth of december", 8376 * time.Hour}, + {"march 10th", 0, "march 10th", 1536 * time.Hour}, + {"jan. 4", 0, "jan. 4", -48 * time.Hour}, + {"february", 0, "february", 744 * time.Hour}, + {"october", 0, "october", 6576 * time.Hour}, + {"jul.", 0, "jul.", 4368 * time.Hour}, + {"june", 0, "june", 3648 * time.Hour}, + } + + ApplyFixtures(t, "en.ExactMonthDate", w, fixtok) +}