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

DATE_HOUR, DATE_MINUTE and DATE_SECOND functions (#156)

This commit is contained in:
3timeslazy 2018-11-04 18:06:38 +03:00 committed by Tim Voronov
parent 3462923085
commit 7fd5af6110
7 changed files with 213 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"
)
// DateHour returns the hour of date as a number.
// @params date (DateTime) - source DateTime.
// @return (Int) - a hour number.
func DateHour(_ 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
}
hour := args[0].(values.DateTime).Hour()
return values.NewInt(hour), 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 TestDateHour(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 hour",
Expected: values.NewInt(7),
Args: []core.Value{mustDefaultLayoutDt("1999-02-07T07:04:05Z")},
},
&testCase{
Name: "When 15th day",
Expected: values.NewInt(15),
Args: []core.Value{mustDefaultLayoutDt("1629-02-28T15:04:05Z")},
},
}
for _, tc := range tcs {
tc.Do(t, datetime.DateHour)
}
}

View File

@ -10,5 +10,8 @@ func NewLib() map[string]core.Function {
"DATE_YEAR": DateYear,
"DATE_MONTH": DateMonth,
"DATE_DAY": DateDay,
"DATE_HOUR": DateHour,
"DATE_MINUTE": DateMinute,
"DATE_SECOND": DateSecond,
}
}

View File

@ -0,0 +1,27 @@
package datetime
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
// DateMinute returns the minute of date as a number.
// @params date (DateTime) - source DateTime.
// @return (Int) - a minute number.
func DateMinute(_ 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
}
min := args[0].(values.DateTime).Minute()
return values.NewInt(min), 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 TestDateMinute(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 4th minute",
Expected: values.NewInt(4),
Args: []core.Value{mustDefaultLayoutDt("1999-02-07T15:04:05Z")},
},
&testCase{
Name: "When 59th minute",
Expected: values.NewInt(59),
Args: []core.Value{mustDefaultLayoutDt("1629-02-28T15:59:05Z")},
},
}
for _, tc := range tcs {
tc.Do(t, datetime.DateMinute)
}
}

View File

@ -0,0 +1,27 @@
package datetime
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
// DateSecond returns the second of date as a number.
// @params date (DateTime) - source DateTime.
// @return (Int) - a second number.
func DateSecond(_ 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
}
sec := args[0].(values.DateTime).Second()
return values.NewInt(sec), 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 TestDateSecond(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 5th second",
Expected: values.NewInt(5),
Args: []core.Value{mustDefaultLayoutDt("1999-02-07T15:04:05Z")},
},
&testCase{
Name: "When 59th second",
Expected: values.NewInt(59),
Args: []core.Value{mustDefaultLayoutDt("1629-02-28T15:59:59Z")},
},
}
for _, tc := range tcs {
tc.Do(t, datetime.DateSecond)
}
}