mirror of
https://github.com/MontFerret/ferret.git
synced 2025-01-08 03:13:15 +02:00
Added support of parsing DateTime with a custom layout (#721)
* Added support of parsing DateTime with a custom layout
This commit is contained in:
parent
4e94caa55f
commit
b0377c1bcc
@ -9,23 +9,32 @@ import (
|
|||||||
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DATE converts RFC3339 date time string to DateTime object.
|
// DATE parses a formatted string and returns DateTime object it represents.
|
||||||
// @param {String} time - String in RFC3339 format.
|
// @param {String} time - String representation of DateTime.
|
||||||
|
// @param {String} [layout = "2006-01-02T15:04:05Z07:00"] - String layout.
|
||||||
// @return {DateTime} - New DateTime object derived from timeString.
|
// @return {DateTime} - New DateTime object derived from timeString.
|
||||||
func Date(_ context.Context, args ...core.Value) (core.Value, error) {
|
func Date(_ context.Context, args ...core.Value) (core.Value, error) {
|
||||||
err := core.ValidateArgs(args, 1, 1)
|
if err := core.ValidateArgs(args, 1, 2); err != nil {
|
||||||
if err != nil {
|
|
||||||
return values.None, err
|
return values.None, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = core.ValidateType(args[0], types.String)
|
if err := core.ValidateType(args[0], types.String); err != nil {
|
||||||
if err != nil {
|
|
||||||
return values.None, err
|
return values.None, err
|
||||||
}
|
}
|
||||||
|
|
||||||
timeStrings := args[0].(values.String)
|
str := args[0].(values.String)
|
||||||
|
layout := values.DefaultTimeLayout
|
||||||
|
|
||||||
|
if len(args) > 1 {
|
||||||
|
if err := core.ValidateType(args[1], types.String); err != nil {
|
||||||
|
return values.None, err
|
||||||
|
}
|
||||||
|
|
||||||
|
layout = values.ToString(args[1]).String()
|
||||||
|
}
|
||||||
|
|
||||||
|
t, err := time.Parse(layout, str.String())
|
||||||
|
|
||||||
t, err := time.Parse(values.DefaultTimeLayout, timeStrings.String())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return values.None, err
|
return values.None, err
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package datetime_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/MontFerret/ferret/pkg/runtime/core"
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
||||||
"github.com/MontFerret/ferret/pkg/runtime/values"
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
||||||
@ -10,29 +11,32 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestDate(t *testing.T) {
|
func TestDate(t *testing.T) {
|
||||||
|
now := time.Now()
|
||||||
|
|
||||||
tcs := []*testCase{
|
tcs := []*testCase{
|
||||||
&testCase{
|
{
|
||||||
Name: "When more than 1 arguments",
|
Name: "When more than 2 arguments",
|
||||||
Expected: values.None,
|
Expected: values.None,
|
||||||
Args: []core.Value{
|
Args: []core.Value{
|
||||||
values.NewString("string"),
|
values.NewString(time.Now().Format(time.RFC3339)),
|
||||||
values.NewInt(0),
|
values.NewString(time.RFC3339),
|
||||||
|
values.NewString("foo"),
|
||||||
},
|
},
|
||||||
ShouldErr: true,
|
ShouldErr: true,
|
||||||
},
|
},
|
||||||
&testCase{
|
{
|
||||||
Name: "When 0 arguments",
|
Name: "When 0 arguments",
|
||||||
Expected: values.None,
|
Expected: values.None,
|
||||||
Args: []core.Value{},
|
Args: []core.Value{},
|
||||||
ShouldErr: true,
|
ShouldErr: true,
|
||||||
},
|
},
|
||||||
&testCase{
|
{
|
||||||
Name: "When argument isn't DateTime",
|
Name: "When first argument isn't string",
|
||||||
Expected: values.None,
|
Expected: values.None,
|
||||||
Args: []core.Value{values.NewInt(0)},
|
Args: []core.Value{values.NewInt(0)},
|
||||||
ShouldErr: true,
|
ShouldErr: true,
|
||||||
},
|
},
|
||||||
&testCase{
|
{
|
||||||
Name: "When incorrect timeStrings",
|
Name: "When incorrect timeStrings",
|
||||||
Expected: values.None,
|
Expected: values.None,
|
||||||
Args: []core.Value{
|
Args: []core.Value{
|
||||||
@ -40,13 +44,31 @@ func TestDate(t *testing.T) {
|
|||||||
},
|
},
|
||||||
ShouldErr: true,
|
ShouldErr: true,
|
||||||
},
|
},
|
||||||
&testCase{
|
{
|
||||||
Name: "When correct timeString in RFC3339 format",
|
Name: "When string is in default format",
|
||||||
Expected: mustDefaultLayoutDt("1999-02-07T15:04:05Z"),
|
Expected: mustDefaultLayoutDt("1999-02-07T15:04:05Z"),
|
||||||
Args: []core.Value{
|
Args: []core.Value{
|
||||||
values.NewString("1999-02-07T15:04:05Z"),
|
values.NewString("1999-02-07T15:04:05Z"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "When second argument isn't string",
|
||||||
|
Expected: values.None,
|
||||||
|
Args: []core.Value{
|
||||||
|
values.NewString("1999-02-07T15:04:05Z"),
|
||||||
|
values.NewInt(1),
|
||||||
|
},
|
||||||
|
ShouldErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "When string is in custom format",
|
||||||
|
Expected: mustLayoutDt(time.RFC822, now.Format(time.RFC822)),
|
||||||
|
Args: []core.Value{
|
||||||
|
values.NewString(now.Format(time.RFC822)),
|
||||||
|
values.NewString(time.RFC822),
|
||||||
|
},
|
||||||
|
ShouldErr: false,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range tcs {
|
for _, tc := range tcs {
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
|
|
||||||
// DATE_FORMAT format date according to the given format string.
|
// DATE_FORMAT format date according to the given format string.
|
||||||
// @param {DateTime} date - Source DateTime object.
|
// @param {DateTime} date - Source DateTime object.
|
||||||
|
// @param {String} format - String format.
|
||||||
// @return {String} - Formatted date.
|
// @return {String} - Formatted date.
|
||||||
func DateFormat(_ context.Context, args ...core.Value) (core.Value, error) {
|
func DateFormat(_ context.Context, args ...core.Value) (core.Value, error) {
|
||||||
err := core.ValidateArgs(args, 2, 2)
|
err := core.ValidateArgs(args, 2, 2)
|
||||||
|
Loading…
Reference in New Issue
Block a user