diff --git a/pkg/stdlib/datetime/add_subtract.go b/pkg/stdlib/datetime/add_subtract.go index fc80b5aa..7131455e 100644 --- a/pkg/stdlib/datetime/add_subtract.go +++ b/pkg/stdlib/datetime/add_subtract.go @@ -2,12 +2,9 @@ package datetime import ( "context" - "strings" - "time" "github.com/MontFerret/ferret/pkg/runtime/core" "github.com/MontFerret/ferret/pkg/runtime/values" - "github.com/pkg/errors" ) var ( @@ -40,12 +37,14 @@ func DateAdd(_ context.Context, args ...core.Value) (core.Value, error) { return values.None, err } - dt, err := addUnit(date, int(amount), unit.String()) + u, err := UnitFromString(unit.String()) if err != nil { return values.None, err } - return dt, nil + tm := AddUnit(date.Time, int(amount), u) + + return values.NewDateTime(tm), nil } // DateSubtract subtract amount given in unit to date. @@ -68,12 +67,14 @@ func DateSubtract(_ context.Context, args ...core.Value) (core.Value, error) { return values.None, err } - dt, err := addUnit(date, -1*int(amount), unit.String()) + u, err := UnitFromString(unit.String()) if err != nil { return values.None, err } - return dt, nil + tm := AddUnit(date.Time, -1*int(amount), u) + + return values.NewDateTime(tm), nil } func getArgs(args []core.Value) (values.DateTime, values.Int, values.String, error) { @@ -97,25 +98,3 @@ func getArgs(args []core.Value) (values.DateTime, values.Int, values.String, err return date, amount, unit, nil } - -func addUnit(dt values.DateTime, amount int, unit string) (values.DateTime, error) { - switch strings.ToLower(unit) { - case "y", "year", "years": - return values.NewDateTime(dt.AddDate(amount*1, 0, 0)), nil - case "m", "month", "months": - return values.NewDateTime(dt.AddDate(0, amount*1, 0)), nil - case "w", "week", "weeks": - return values.NewDateTime(dt.AddDate(0, 0, amount*7)), nil - case "d", "day", "days": - return values.NewDateTime(dt.AddDate(0, 0, amount*1)), nil - case "h", "hour", "hours": - return values.NewDateTime(dt.Add(time.Duration(amount) * time.Hour)), nil - case "i", "minute", "minutes": - return values.NewDateTime(dt.Add(time.Duration(amount) * time.Minute)), nil - case "s", "second", "seconds": - return values.NewDateTime(dt.Add(time.Duration(amount) * time.Second)), nil - case "f", "millisecond", "milliseconds": - return values.NewDateTime(dt.Add(time.Duration(amount) * time.Millisecond)), nil - } - return values.DateTime{}, errors.Errorf("no such unit '%s'", unit) -} diff --git a/pkg/stdlib/datetime/compare.go b/pkg/stdlib/datetime/compare.go new file mode 100644 index 00000000..7519b1e5 --- /dev/null +++ b/pkg/stdlib/datetime/compare.go @@ -0,0 +1,66 @@ +package datetime + +import ( + "github.com/pkg/errors" + + "context" + + "github.com/MontFerret/ferret/pkg/runtime/core" + "github.com/MontFerret/ferret/pkg/runtime/values" +) + +// DateCompare check if two partial dates match. +// @params date1, date2 (DateTime) - comparable dates. +// @params unitRangeStart (String) - unit to start from. +// @params unitRangeEnd (String, Optional) - unit to end with. +// Error will be returned if unitRangeStart unit less that unitRangeEnd. +// @return (Boolean) - true if the dates match, else false. +func DateCompare(_ context.Context, args ...core.Value) (core.Value, error) { + err := core.ValidateArgs(args, 3, 4) + if err != nil { + return values.None, err + } + + err = core.ValidateValueTypePairs( + core.PairValueType{Value: args[0], Types: sliceDateTime}, + core.PairValueType{Value: args[1], Types: sliceDateTime}, + core.PairValueType{Value: args[2], Types: sliceStringType}, + ) + if err != nil { + return values.None, err + } + + date1 := args[0].(values.DateTime) + date2 := args[1].(values.DateTime) + rangeStart := args[2].(values.String) + rangeEnd := values.NewString("millisecond") + + if len(args) == 4 { + if err = core.ValidateType(args[3], core.StringType); err != nil { + return values.None, err + } + rangeEnd = args[3].(values.String) + } + + unitStart, err := UnitFromString(rangeStart.String()) + if err != nil { + return values.None, err + } + + unitEnd, err := UnitFromString(rangeEnd.String()) + if err != nil { + return values.None, err + } + + if unitStart < unitEnd { + return values.None, errors.Errorf("start unit less that end unit") + } + + for u := unitEnd; u <= unitStart; u++ { + if IsDatesEqual(date1.Time, date2.Time, u) { + return values.NewBoolean(true), nil + } + } + + return values.NewBoolean(false), nil +} diff --git a/pkg/stdlib/datetime/compare_test.go b/pkg/stdlib/datetime/compare_test.go new file mode 100644 index 00000000..534d29c5 --- /dev/null +++ b/pkg/stdlib/datetime/compare_test.go @@ -0,0 +1,107 @@ +package datetime_test + +import ( + "testing" + + "github.com/MontFerret/ferret/pkg/runtime/core" + "github.com/MontFerret/ferret/pkg/runtime/values" + "github.com/MontFerret/ferret/pkg/stdlib/datetime" +) + +func TestDateCompare(t *testing.T) { + expectedTrue := values.NewBoolean(true) + expectedFalse := values.NewBoolean(false) + + tcs := []*testCase{ + &testCase{ + Name: "When less than 3 arguments", + Expected: values.None, + Args: []core.Value{values.NewInt(0), values.NewInt(0)}, + ShouldErr: true, + }, + &testCase{ + Name: "When more than 4 arguments", + Expected: values.None, + Args: []core.Value{ + values.NewInt(0), values.NewInt(0), values.NewInt(0), + values.NewInt(0), values.NewInt(0), + }, + ShouldErr: true, + }, + &testCase{ + Name: "when wrong type of arguments", + Expected: values.None, + Args: []core.Value{ + values.NewCurrentDateTime(), + values.NewCurrentDateTime(), + values.NewInt(0), + }, + ShouldErr: true, + }, + &testCase{ + Name: "when wrong type of optional argument", + Expected: values.None, + Args: []core.Value{ + values.NewCurrentDateTime(), + values.NewCurrentDateTime(), + values.NewString("year"), + values.NewInt(0), + }, + ShouldErr: true, + }, + &testCase{ + Name: "when start unit less that end unit", + Expected: values.None, + Args: []core.Value{ + values.NewCurrentDateTime(), + values.NewCurrentDateTime(), + values.NewString("day"), + values.NewString("year"), + }, + ShouldErr: true, + }, + &testCase{ + Name: "when years are equal", + Expected: expectedTrue, + Args: []core.Value{ + values.NewCurrentDateTime(), + values.NewCurrentDateTime(), + values.NewString("year"), + }, + }, + &testCase{ + Name: "when years are not equal", + Expected: expectedFalse, + Args: []core.Value{ + mustLayoutDt("2006-01-02", "1999-02-07"), + mustLayoutDt("2006-01-02", "2000-02-07"), + values.NewString("year"), + values.NewString("year"), + }, + }, + &testCase{ + Name: "when months are equal", + Expected: expectedTrue, + Args: []core.Value{ + mustLayoutDt("2006-01-02", "1999-02-07"), + mustLayoutDt("2006-01-02", "2000-02-09"), + values.NewString("year"), + values.NewString("days"), + }, + }, + &testCase{ + Name: "when days are equal", + Expected: expectedTrue, + Args: []core.Value{ + values.NewCurrentDateTime(), + values.NewCurrentDateTime(), + values.NewString("days"), + values.NewString("days"), + }, + }, + } + + for _, tc := range tcs { + tc.Do(t, datetime.DateCompare) + } +} diff --git a/pkg/stdlib/datetime/diff.go b/pkg/stdlib/datetime/diff.go index 743d9ebd..d40bd8e4 100644 --- a/pkg/stdlib/datetime/diff.go +++ b/pkg/stdlib/datetime/diff.go @@ -2,11 +2,9 @@ package datetime import ( "context" - "strings" "github.com/MontFerret/ferret/pkg/runtime/core" "github.com/MontFerret/ferret/pkg/runtime/values" - "github.com/pkg/errors" ) // DateDiff returns the difference between two dates in given time unit. @@ -71,23 +69,9 @@ func DateDiff(_ context.Context, args ...core.Value) (core.Value, error) { } func nsecToUnit(nsec float64, unit string) (float64, error) { - switch strings.ToLower(unit) { - case "y", "year", "years": - return nsec / 31536e12, nil - case "m", "month", "months": - return nsec / 26784e11, nil - case "w", "week", "weeks": - return nsec / 6048e11, nil - case "d", "day", "days": - return nsec / 864e11, nil - case "h", "hour", "hours": - return nsec / 36e11, nil - case "i", "minute", "minutes": - return nsec / 6e10, nil - case "s", "second", "seconds": - return nsec / 1e9, nil - case "f", "millisecond", "milliseconds": - return nsec / 1e6, nil + u, err := UnitFromString(unit) + if err != nil { + return -1, err } - return -1, errors.Errorf("no such unit '%s'", unit) + return nsec / u.Nanosecond(), nil } diff --git a/pkg/stdlib/datetime/unit.go b/pkg/stdlib/datetime/unit.go new file mode 100644 index 00000000..580f0c71 --- /dev/null +++ b/pkg/stdlib/datetime/unit.go @@ -0,0 +1,113 @@ +package datetime + +import ( + "strings" + "time" + + "github.com/pkg/errors" +) + +// Unit specifies an unit of time (Millsecond, Second...). +type Unit int + +const ( + Millisecond Unit = iota + Second + Minute + Hour + Day + Week + Month + Year +) + +var nanoseconds = []float64{ + 1e6, + 1e9, + 6e10, + 36e11, + 864e11, + 6048e11, + 26784e11, + 31536e12, +} + +// Nanosecond returns representation of an Unit +// in nanosconds +func (u Unit) Nanosecond() float64 { + return nanoseconds[u] +} + +// IsDatesEqual check if two partial dates match. +// This case the day means not the amount of days in Time, +// but the day of the month. +// The same rules applied to each unit. +func IsDatesEqual(tm1, tm2 time.Time, u Unit) bool { + switch u { + case Millisecond: + tm1Msec := tm1.Nanosecond() / 1e6 + tm2Msec := tm2.Nanosecond() / 1e6 + return tm1Msec == tm2Msec + case Second: + return tm1.Second() == tm2.Second() + case Minute: + return tm1.Minute() == tm2.Minute() + case Hour: + return tm1.Hour() == tm2.Hour() + case Day: + return tm1.Day() == tm2.Day() + case Week: + tm1Wk := tm1.Day() / 7 + tm2Wk := tm2.Day() / 7 + return tm1Wk == tm2Wk + case Month: + return tm1.Month() == tm2.Month() + case Year: + return tm1.Year() == tm2.Year() + } + return false +} + +// AddUnit add amount given in u to tm +func AddUnit(tm time.Time, amount int, u Unit) (res time.Time) { + if u < Day { + return tm.Add(time.Duration(amount) * time.Duration(int64(u.Nanosecond()))) + } + + switch u { + case Day: + res = tm.AddDate(0, 0, amount*1) + case Week: + res = tm.AddDate(0, 0, amount*7) + case Month: + res = tm.AddDate(0, amount*1, 0) + case Year: + res = tm.AddDate(amount*1, 0, 0) + } + + return +} + +// UnitFromString returns true and an Unit object if +// Unit with that name exists. Returns false, otherwise. +func UnitFromString(s string) (Unit, error) { + switch strings.ToLower(s) { + case "y", "year", "years": + return Year, nil + case "m", "month", "months": + return Month, nil + case "w", "week", "weeks": + return Week, nil + case "d", "day", "days": + return Day, nil + case "h", "hour", "hours": + return Hour, nil + case "i", "minute", "minutes": + return Minute, nil + case "s", "second", "seconds": + return Second, nil + case "f", "millisecond", "milliseconds": + return Millisecond, nil + } + return -1, errors.Errorf("no such unit '%s'", s) +}