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:
parent
612705ca3a
commit
3462923085
27
pkg/stdlib/datetime/day.go
Normal file
27
pkg/stdlib/datetime/day.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"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
43
pkg/stdlib/datetime/day_test.go
Normal file
43
pkg/stdlib/datetime/day_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 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)
|
||||||
|
}
|
||||||
|
}
|
@ -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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
27
pkg/stdlib/datetime/month.go
Normal file
27
pkg/stdlib/datetime/month.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"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
43
pkg/stdlib/datetime/month_test.go
Normal file
43
pkg/stdlib/datetime/month_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 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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user