Skip to content

Commit

Permalink
Added support of parsing DateTime with a custom layout (#721)
Browse files Browse the repository at this point in the history
* Added support of parsing DateTime with a custom layout
  • Loading branch information
ziflex committed Jan 3, 2022
1 parent 4e94caa commit b0377c1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
25 changes: 17 additions & 8 deletions pkg/stdlib/datetime/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,32 @@ import (
"github.com/MontFerret/ferret/pkg/runtime/values/types"
)

// DATE converts RFC3339 date time string to DateTime object.
// @param {String} time - String in RFC3339 format.
// DATE parses a formatted string and returns DateTime object it represents.
// @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.
func Date(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 1)
if err != nil {
if err := core.ValidateArgs(args, 1, 2); err != nil {
return values.None, err
}

err = core.ValidateType(args[0], types.String)
if err != nil {
if err := core.ValidateType(args[0], types.String); err != nil {
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 {
return values.None, err
}
Expand Down
42 changes: 32 additions & 10 deletions pkg/stdlib/datetime/date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package datetime_test

import (
"testing"
"time"

"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
Expand All @@ -10,43 +11,64 @@ import (
)

func TestDate(t *testing.T) {
now := time.Now()

tcs := []*testCase{
&testCase{
Name: "When more than 1 arguments",
{
Name: "When more than 2 arguments",
Expected: values.None,
Args: []core.Value{
values.NewString("string"),
values.NewInt(0),
values.NewString(time.Now().Format(time.RFC3339)),
values.NewString(time.RFC3339),
values.NewString("foo"),
},
ShouldErr: true,
},
&testCase{
{
Name: "When 0 arguments",
Expected: values.None,
Args: []core.Value{},
ShouldErr: true,
},
&testCase{
Name: "When argument isn't DateTime",
{
Name: "When first argument isn't string",
Expected: values.None,
Args: []core.Value{values.NewInt(0)},
ShouldErr: true,
},
&testCase{
{
Name: "When incorrect timeStrings",
Expected: values.None,
Args: []core.Value{
values.NewString("bla-bla"),
},
ShouldErr: true,
},
&testCase{
Name: "When correct timeString in RFC3339 format",
{
Name: "When string is in default format",
Expected: mustDefaultLayoutDt("1999-02-07T15:04:05Z"),
Args: []core.Value{
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 {
Expand Down
1 change: 1 addition & 0 deletions pkg/stdlib/datetime/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

// DATE_FORMAT format date according to the given format string.
// @param {DateTime} date - Source DateTime object.
// @param {String} format - String format.
// @return {String} - Formatted date.
func DateFormat(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 2, 2)
Expand Down

0 comments on commit b0377c1

Please sign in to comment.