1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-14 11:23:02 +02:00

Feature/#8 date diff (#175)

This commit is contained in:
3timeslazy 2018-11-14 00:58:40 +03:00 committed by Tim Voronov
parent 9131c676d9
commit b4a8c8ba3c
4 changed files with 273 additions and 1 deletions

View File

@ -0,0 +1,93 @@
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.
// @params date1 (DateTime) - first DateTime.
// @params date2 (DateTime) - second DateTime.
// @params unit (String) - time unit to return the difference in.
// @params asFloat (Boolean, optional) - if true amount of unit will be as float.
// @return (Int, Float) - difference between date1 and date2.
func DateDiff(_ 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)
unit := args[2].(values.String)
isFloat := values.NewBoolean(false)
if len(args) == 4 {
err = core.ValidateType(args[3], core.BooleanType)
if err != nil {
return values.None, err
}
isFloat = args[3].(values.Boolean)
}
if date1.Equal(date2.Time) {
if isFloat {
return values.NewFloat(0), nil
}
return values.NewInt(0), nil
}
var nsecDiff int64
if date1.After(date2.Time) {
nsecDiff = date1.Time.Sub(date2.Time).Nanoseconds()
} else {
nsecDiff = date2.Time.Sub(date1.Time).Nanoseconds()
}
unitDiff, err := nsecToUnit(float64(nsecDiff), unit.String())
if err != nil {
return values.None, err
}
if !isFloat {
return values.NewInt(int(unitDiff)), nil
}
return values.NewFloat(unitDiff), nil
}
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
}
return -1, errors.Errorf("no such unit '%s'", unit)
}

View File

@ -0,0 +1,178 @@
package datetime_test
import (
"testing"
"time"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/stdlib/datetime"
)
var (
isFloat = values.NewBoolean(true)
beginingEpoch = values.NewDateTime(time.Time{})
)
func TestDiff(t *testing.T) {
tcs := []*testCase{
&testCase{
Name: "when less then 3 arguments",
Expected: values.NewInt(1),
Args: []core.Value{beginingEpoch},
ShouldErr: true,
},
&testCase{
Name: "when more then 4 arguments",
Expected: values.NewInt(1),
Args: []core.Value{beginingEpoch, beginingEpoch, beginingEpoch, beginingEpoch, beginingEpoch},
ShouldErr: true,
},
&testCase{
Name: "when wrong type argument",
Expected: values.NewInt(1),
Args: []core.Value{beginingEpoch, beginingEpoch, beginingEpoch},
ShouldErr: true,
},
&testCase{
Name: "when the difference is 1 year and 1 month (int)",
Expected: values.NewInt(1),
Args: []core.Value{
beginingEpoch,
values.NewDateTime(
beginingEpoch.AddDate(1, 1, 0),
),
values.NewString("y"),
},
},
&testCase{
Name: "when the difference is 1 year and 1 month (float)",
Expected: values.NewFloat(1.084931506849315),
Args: []core.Value{
beginingEpoch,
values.NewDateTime(
beginingEpoch.AddDate(1, 1, 0),
),
values.NewString("year"),
isFloat,
},
},
&testCase{
Name: "when date1 after date2 (int)",
Expected: values.NewInt(2),
Args: []core.Value{
beginingEpoch,
values.NewDateTime(
beginingEpoch.Add(-time.Hour * 48),
),
values.NewString("d"),
},
},
&testCase{
Name: "when date1 after date2 (float)",
Expected: values.NewFloat(2),
Args: []core.Value{
beginingEpoch,
values.NewDateTime(
beginingEpoch.Add(-time.Hour * 48),
),
values.NewString("d"),
isFloat,
},
},
&testCase{
Name: "when dates are equal (int)",
Expected: values.NewInt(0),
Args: []core.Value{
beginingEpoch,
beginingEpoch,
values.NewString("i"),
},
},
&testCase{
Name: "when dates are equal (float)",
Expected: values.NewFloat(0),
Args: []core.Value{
beginingEpoch,
beginingEpoch,
values.NewString("y"),
isFloat,
},
},
}
bigUnits := map[string][3]int{
"y": [3]int{1, 0, 0}, "year": [3]int{1, 0, 0}, "years": [3]int{1, 0, 0},
"m": [3]int{0, 1, 0}, "month": [3]int{0, 1, 0}, "months": [3]int{0, 1, 0},
"w": [3]int{0, 0, 7}, "week": [3]int{0, 0, 7}, "weeks": [3]int{0, 0, 7},
"d": [3]int{0, 0, 1}, "day": [3]int{0, 0, 1}, "days": [3]int{0, 0, 1},
}
for unit, dates := range bigUnits {
tcs = append(tcs,
&testCase{
Name: "When difference is 1 " + unit + " (int)",
Expected: values.NewInt(1),
Args: []core.Value{
beginingEpoch,
values.NewDateTime(
beginingEpoch.AddDate(dates[0], dates[1], dates[2]),
),
values.NewString(unit),
},
},
&testCase{
Name: "When difference is 1 " + unit + " (float)",
Expected: values.NewFloat(1),
Args: []core.Value{
beginingEpoch,
values.NewDateTime(
beginingEpoch.AddDate(dates[0], dates[1], dates[2]),
),
values.NewString(unit),
isFloat,
},
},
)
}
units := map[string]time.Duration{
"h": time.Hour, "hour": time.Hour, "hours": time.Hour,
"i": time.Minute, "minute": time.Minute, "minutes": time.Minute,
"s": time.Second, "second": time.Second, "seconds": time.Second,
"f": time.Millisecond, "millisecond": time.Millisecond, "milliseconds": time.Millisecond,
}
for unit, durn := range units {
tcs = append(tcs,
&testCase{
Name: "When difference is 1 " + unit + " (int)",
Expected: values.NewInt(1),
Args: []core.Value{
beginingEpoch,
values.NewDateTime(
beginingEpoch.Add(durn),
),
values.NewString(unit),
},
},
&testCase{
Name: "When difference is 1 " + unit + " (int)",
Expected: values.NewFloat(1),
Args: []core.Value{
beginingEpoch,
values.NewDateTime(
beginingEpoch.Add(durn),
),
values.NewString(unit),
isFloat,
},
},
)
}
for _, tc := range tcs {
tc.Do(t, datetime.DateDiff)
}
}

View File

@ -14,7 +14,6 @@ import (
type testCase struct {
Name string
Expected core.Value
TimeArg time.Time
Args []core.Value
ShouldErr bool
}
@ -32,6 +31,7 @@ func (tc *testCase) Do(t *testing.T, fn core.Function) {
So(err, ShouldBeNil)
}
So(actual.Type(), ShouldEqual, expected.Type())
So(actual.Compare(expected), ShouldEqual, 0)
})
}

View File

@ -21,5 +21,6 @@ func NewLib() map[string]core.Function {
"DATE_FORMAT": DateFormat,
"DATE_ADD": DateAdd,
"DATE_SUBTRACT": DateSubtract,
"DATE_DIFF": DateDiff,
}
}