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:
parent
3462923085
commit
7fd5af6110
27
pkg/stdlib/datetime/hour.go
Normal file
27
pkg/stdlib/datetime/hour.go
Normal 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
|
||||
}
|
43
pkg/stdlib/datetime/hour_test.go
Normal file
43
pkg/stdlib/datetime/hour_test.go
Normal 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)
|
||||
}
|
||||
}
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
27
pkg/stdlib/datetime/minute.go
Normal file
27
pkg/stdlib/datetime/minute.go
Normal 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
|
||||
}
|
43
pkg/stdlib/datetime/minute_test.go
Normal file
43
pkg/stdlib/datetime/minute_test.go
Normal 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)
|
||||
}
|
||||
}
|
27
pkg/stdlib/datetime/second.go
Normal file
27
pkg/stdlib/datetime/second.go
Normal 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
|
||||
}
|
43
pkg/stdlib/datetime/second_test.go
Normal file
43
pkg/stdlib/datetime/second_test.go
Normal 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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user