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

Feature/#8 datetime (#154)

This commit is contained in:
3timeslazy 2018-11-01 23:58:22 +03:00 committed by Tim Voronov
parent 612705ca3a
commit 3462923085
5 changed files with 142 additions and 0 deletions

View File

@ -0,0 +1,27 @@
package datetime
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
// DateDay returns the day of date as a number.
// @params date (DateTime) - source DateTime.
// @return (Int) - a day number.
func DateDay(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 1)
if err != nil {
return values.None, err
}
err = core.ValidateType(args[0], core.DateTimeType)
if err != nil {
return values.None, err
}
day := args[0].(values.DateTime).Day()
return values.NewInt(day), nil
}

View File

@ -0,0 +1,43 @@
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 TestDateDay(t *testing.T) {
tcs := []*testCase{
&testCase{
Name: "When more than 1 arguments",
Expected: values.None,
Args: []core.Value{
values.NewString("string"),
values.NewInt(0),
},
ShouldErr: true,
},
&testCase{
Name: "When 0 arguments",
Expected: values.None,
Args: []core.Value{},
ShouldErr: true,
},
&testCase{
Name: "When 7th day",
Expected: values.NewInt(7),
Args: []core.Value{mustDefaultLayoutDt("1999-02-07T15:04:05Z")},
},
&testCase{
Name: "When 28th day",
Expected: values.NewInt(28),
Args: []core.Value{mustDefaultLayoutDt("1629-02-28T15:04:05Z")},
},
}
for _, tc := range tcs {
tc.Do(t, datetime.DateDay)
}
}

View File

@ -8,5 +8,7 @@ func NewLib() map[string]core.Function {
"DATE": Date, "DATE": Date,
"DATE_DAYOFWEEK": DateDayOfWeek, "DATE_DAYOFWEEK": DateDayOfWeek,
"DATE_YEAR": DateYear, "DATE_YEAR": DateYear,
"DATE_MONTH": DateMonth,
"DATE_DAY": DateDay,
} }
} }

View File

@ -0,0 +1,27 @@
package datetime
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
// DateMonth returns the month of date as a number.
// @params date (DateTime) - source DateTime.
// @return (Int) - a month number.
func DateMonth(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 1)
if err != nil {
return values.None, err
}
err = core.ValidateType(args[0], core.DateTimeType)
if err != nil {
return values.None, err
}
month := args[0].(values.DateTime).Month()
return values.NewInt(int(month)), nil
}

View File

@ -0,0 +1,43 @@
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 TestDateMonth(t *testing.T) {
tcs := []*testCase{
&testCase{
Name: "When more than 1 arguments",
Expected: values.None,
Args: []core.Value{
values.NewString("string"),
values.NewInt(0),
},
ShouldErr: true,
},
&testCase{
Name: "When 0 arguments",
Expected: values.None,
Args: []core.Value{},
ShouldErr: true,
},
&testCase{
Name: "When 2th month",
Expected: values.NewInt(2),
Args: []core.Value{mustDefaultLayoutDt("1999-02-07T15:04:05Z")},
},
&testCase{
Name: "When 12th month",
Expected: values.NewInt(12),
Args: []core.Value{mustDefaultLayoutDt("1999-12-07T15:04:05Z")},
},
}
for _, tc := range tcs {
tc.Do(t, datetime.DateMonth)
}
}