Skip to content

Commit b4a8c8b

Browse files
3timeslazyziflex
3timeslazy
authored andcommitted
Feature/#8 date diff (#175)
1 parent 9131c67 commit b4a8c8b

File tree

4 files changed

+273
-1
lines changed

4 files changed

+273
-1
lines changed

pkg/stdlib/datetime/diff.go

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package datetime
2+
3+
import (
4+
"context"
5+
"strings"
6+
7+
"github.com/MontFerret/ferret/pkg/runtime/core"
8+
"github.com/MontFerret/ferret/pkg/runtime/values"
9+
"github.com/pkg/errors"
10+
)
11+
12+
// DateDiff returns the difference between two dates in given time unit.
13+
// @params date1 (DateTime) - first DateTime.
14+
// @params date2 (DateTime) - second DateTime.
15+
// @params unit (String) - time unit to return the difference in.
16+
// @params asFloat (Boolean, optional) - if true amount of unit will be as float.
17+
// @return (Int, Float) - difference between date1 and date2.
18+
func DateDiff(_ context.Context, args ...core.Value) (core.Value, error) {
19+
err := core.ValidateArgs(args, 3, 4)
20+
if err != nil {
21+
return values.None, err
22+
}
23+
24+
err = core.ValidateValueTypePairs(
25+
core.PairValueType{Value: args[0], Types: sliceDateTime},
26+
core.PairValueType{Value: args[1], Types: sliceDateTime},
27+
core.PairValueType{Value: args[2], Types: sliceStringType},
28+
)
29+
if err != nil {
30+
return values.None, err
31+
}
32+
33+
date1 := args[0].(values.DateTime)
34+
date2 := args[1].(values.DateTime)
35+
unit := args[2].(values.String)
36+
isFloat := values.NewBoolean(false)
37+
38+
if len(args) == 4 {
39+
err = core.ValidateType(args[3], core.BooleanType)
40+
if err != nil {
41+
return values.None, err
42+
}
43+
isFloat = args[3].(values.Boolean)
44+
}
45+
46+
if date1.Equal(date2.Time) {
47+
if isFloat {
48+
return values.NewFloat(0), nil
49+
}
50+
return values.NewInt(0), nil
51+
}
52+
53+
var nsecDiff int64
54+
55+
if date1.After(date2.Time) {
56+
nsecDiff = date1.Time.Sub(date2.Time).Nanoseconds()
57+
} else {
58+
nsecDiff = date2.Time.Sub(date1.Time).Nanoseconds()
59+
}
60+
61+
unitDiff, err := nsecToUnit(float64(nsecDiff), unit.String())
62+
if err != nil {
63+
return values.None, err
64+
}
65+
66+
if !isFloat {
67+
return values.NewInt(int(unitDiff)), nil
68+
}
69+
70+
return values.NewFloat(unitDiff), nil
71+
}
72+
73+
func nsecToUnit(nsec float64, unit string) (float64, error) {
74+
switch strings.ToLower(unit) {
75+
case "y", "year", "years":
76+
return nsec / 31536e12, nil
77+
case "m", "month", "months":
78+
return nsec / 26784e11, nil
79+
case "w", "week", "weeks":
80+
return nsec / 6048e11, nil
81+
case "d", "day", "days":
82+
return nsec / 864e11, nil
83+
case "h", "hour", "hours":
84+
return nsec / 36e11, nil
85+
case "i", "minute", "minutes":
86+
return nsec / 6e10, nil
87+
case "s", "second", "seconds":
88+
return nsec / 1e9, nil
89+
case "f", "millisecond", "milliseconds":
90+
return nsec / 1e6, nil
91+
}
92+
return -1, errors.Errorf("no such unit '%s'", unit)
93+
}

pkg/stdlib/datetime/diff_test.go

+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package datetime_test
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/MontFerret/ferret/pkg/runtime/values"
8+
9+
"github.com/MontFerret/ferret/pkg/runtime/core"
10+
"github.com/MontFerret/ferret/pkg/stdlib/datetime"
11+
)
12+
13+
var (
14+
isFloat = values.NewBoolean(true)
15+
beginingEpoch = values.NewDateTime(time.Time{})
16+
)
17+
18+
func TestDiff(t *testing.T) {
19+
tcs := []*testCase{
20+
&testCase{
21+
Name: "when less then 3 arguments",
22+
Expected: values.NewInt(1),
23+
Args: []core.Value{beginingEpoch},
24+
ShouldErr: true,
25+
},
26+
&testCase{
27+
Name: "when more then 4 arguments",
28+
Expected: values.NewInt(1),
29+
Args: []core.Value{beginingEpoch, beginingEpoch, beginingEpoch, beginingEpoch, beginingEpoch},
30+
ShouldErr: true,
31+
},
32+
&testCase{
33+
Name: "when wrong type argument",
34+
Expected: values.NewInt(1),
35+
Args: []core.Value{beginingEpoch, beginingEpoch, beginingEpoch},
36+
ShouldErr: true,
37+
},
38+
&testCase{
39+
Name: "when the difference is 1 year and 1 month (int)",
40+
Expected: values.NewInt(1),
41+
Args: []core.Value{
42+
beginingEpoch,
43+
values.NewDateTime(
44+
beginingEpoch.AddDate(1, 1, 0),
45+
),
46+
values.NewString("y"),
47+
},
48+
},
49+
&testCase{
50+
Name: "when the difference is 1 year and 1 month (float)",
51+
Expected: values.NewFloat(1.084931506849315),
52+
Args: []core.Value{
53+
beginingEpoch,
54+
values.NewDateTime(
55+
beginingEpoch.AddDate(1, 1, 0),
56+
),
57+
values.NewString("year"),
58+
isFloat,
59+
},
60+
},
61+
&testCase{
62+
Name: "when date1 after date2 (int)",
63+
Expected: values.NewInt(2),
64+
Args: []core.Value{
65+
beginingEpoch,
66+
values.NewDateTime(
67+
beginingEpoch.Add(-time.Hour * 48),
68+
),
69+
values.NewString("d"),
70+
},
71+
},
72+
&testCase{
73+
Name: "when date1 after date2 (float)",
74+
Expected: values.NewFloat(2),
75+
Args: []core.Value{
76+
beginingEpoch,
77+
values.NewDateTime(
78+
beginingEpoch.Add(-time.Hour * 48),
79+
),
80+
values.NewString("d"),
81+
isFloat,
82+
},
83+
},
84+
&testCase{
85+
Name: "when dates are equal (int)",
86+
Expected: values.NewInt(0),
87+
Args: []core.Value{
88+
beginingEpoch,
89+
beginingEpoch,
90+
values.NewString("i"),
91+
},
92+
},
93+
&testCase{
94+
Name: "when dates are equal (float)",
95+
Expected: values.NewFloat(0),
96+
Args: []core.Value{
97+
beginingEpoch,
98+
beginingEpoch,
99+
values.NewString("y"),
100+
isFloat,
101+
},
102+
},
103+
}
104+
105+
bigUnits := map[string][3]int{
106+
"y": [3]int{1, 0, 0}, "year": [3]int{1, 0, 0}, "years": [3]int{1, 0, 0},
107+
"m": [3]int{0, 1, 0}, "month": [3]int{0, 1, 0}, "months": [3]int{0, 1, 0},
108+
"w": [3]int{0, 0, 7}, "week": [3]int{0, 0, 7}, "weeks": [3]int{0, 0, 7},
109+
"d": [3]int{0, 0, 1}, "day": [3]int{0, 0, 1}, "days": [3]int{0, 0, 1},
110+
}
111+
112+
for unit, dates := range bigUnits {
113+
tcs = append(tcs,
114+
&testCase{
115+
Name: "When difference is 1 " + unit + " (int)",
116+
Expected: values.NewInt(1),
117+
Args: []core.Value{
118+
beginingEpoch,
119+
values.NewDateTime(
120+
beginingEpoch.AddDate(dates[0], dates[1], dates[2]),
121+
),
122+
values.NewString(unit),
123+
},
124+
},
125+
&testCase{
126+
Name: "When difference is 1 " + unit + " (float)",
127+
Expected: values.NewFloat(1),
128+
Args: []core.Value{
129+
beginingEpoch,
130+
values.NewDateTime(
131+
beginingEpoch.AddDate(dates[0], dates[1], dates[2]),
132+
),
133+
values.NewString(unit),
134+
isFloat,
135+
},
136+
},
137+
)
138+
}
139+
140+
units := map[string]time.Duration{
141+
"h": time.Hour, "hour": time.Hour, "hours": time.Hour,
142+
"i": time.Minute, "minute": time.Minute, "minutes": time.Minute,
143+
"s": time.Second, "second": time.Second, "seconds": time.Second,
144+
"f": time.Millisecond, "millisecond": time.Millisecond, "milliseconds": time.Millisecond,
145+
}
146+
147+
for unit, durn := range units {
148+
tcs = append(tcs,
149+
&testCase{
150+
Name: "When difference is 1 " + unit + " (int)",
151+
Expected: values.NewInt(1),
152+
Args: []core.Value{
153+
beginingEpoch,
154+
values.NewDateTime(
155+
beginingEpoch.Add(durn),
156+
),
157+
values.NewString(unit),
158+
},
159+
},
160+
&testCase{
161+
Name: "When difference is 1 " + unit + " (int)",
162+
Expected: values.NewFloat(1),
163+
Args: []core.Value{
164+
beginingEpoch,
165+
values.NewDateTime(
166+
beginingEpoch.Add(durn),
167+
),
168+
values.NewString(unit),
169+
isFloat,
170+
},
171+
},
172+
)
173+
}
174+
175+
for _, tc := range tcs {
176+
tc.Do(t, datetime.DateDiff)
177+
}
178+
}

pkg/stdlib/datetime/helpers_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
type testCase struct {
1515
Name string
1616
Expected core.Value
17-
TimeArg time.Time
1817
Args []core.Value
1918
ShouldErr bool
2019
}
@@ -32,6 +31,7 @@ func (tc *testCase) Do(t *testing.T, fn core.Function) {
3231
So(err, ShouldBeNil)
3332
}
3433

34+
So(actual.Type(), ShouldEqual, expected.Type())
3535
So(actual.Compare(expected), ShouldEqual, 0)
3636
})
3737
}

pkg/stdlib/datetime/lib.go

+1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ func NewLib() map[string]core.Function {
2121
"DATE_FORMAT": DateFormat,
2222
"DATE_ADD": DateAdd,
2323
"DATE_SUBTRACT": DateSubtract,
24+
"DATE_DIFF": DateDiff,
2425
}
2526
}

0 commit comments

Comments
 (0)