-
-
Notifications
You must be signed in to change notification settings - Fork 303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/#8 datetime #153
Merged
Merged
Feature/#8 datetime #153
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b0e2c06
init datetime
6f82025
added test for datetime
09546ae
added lib.go
d870798
add helpers functions
1cb998e
made values.DefaultTimeLayout public
f3b1372
added DATE function
f5b7cb9
added DATE_DAYOFWEEK function
db2ab4b
added DATE_YEAR function
c5328e0
merge with MontFerre/ferret master
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package datetime | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/MontFerret/ferret/pkg/runtime/core" | ||
"github.com/MontFerret/ferret/pkg/runtime/values" | ||
) | ||
|
||
// Date convert RFC3339 date time string to DateTime object. | ||
// @params timeString (String) - string in RFC3339 format. | ||
// @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 { | ||
return values.None, err | ||
} | ||
|
||
err = core.ValidateType(args[0], core.StringType) | ||
if err != nil { | ||
return values.None, err | ||
} | ||
|
||
timeStrings := args[0].(values.String) | ||
|
||
t, err := time.Parse(values.DefaultTimeLayout, timeStrings.String()) | ||
if err != nil { | ||
return values.None, err | ||
} | ||
|
||
return values.NewDateTime(t), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
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 TestDate(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 incorrect timeStrings", | ||
Expected: values.None, | ||
Args: []core.Value{ | ||
values.NewString("bla-bla"), | ||
}, | ||
ShouldErr: true, | ||
}, | ||
&testCase{ | ||
Name: "When correct timeString in RFC3339 format", | ||
Expected: mustDefaultLayoutDt("1999-02-07T15:04:05Z"), | ||
Args: []core.Value{ | ||
values.NewString("1999-02-07T15:04:05Z"), | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
tc.Do(t, datetime.Date) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package datetime | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/MontFerret/ferret/pkg/runtime/values" | ||
|
||
"github.com/MontFerret/ferret/pkg/runtime/core" | ||
) | ||
|
||
// DateDayOfWeek returns number of the weekday from the date. Sunday is the 0th day of week. | ||
// @params date (DateTime) - source DateTime. | ||
// @return (Int) - return number of the weekday. | ||
func DateDayOfWeek(_ 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 | ||
} | ||
|
||
wday := args[0].(values.DateTime).Weekday() | ||
|
||
return values.NewInt(int(wday)), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 TestDateDayOfWeek(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 Sunday (0th day)", | ||
Expected: values.NewInt(0), | ||
Args: []core.Value{mustDefaultLayoutDt("1999-02-07T15:04:05Z")}, | ||
}, | ||
&testCase{ | ||
Name: "When Monday (1th day)", | ||
Expected: values.NewInt(1), | ||
Args: []core.Value{mustDefaultLayoutDt("1999-02-08T15:04:05Z")}, | ||
}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
tc.Do(t, datetime.DateDayOfWeek) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package datetime_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/MontFerret/ferret/pkg/runtime/core" | ||
"github.com/MontFerret/ferret/pkg/runtime/values" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
type testCase struct { | ||
Name string | ||
Expected core.Value | ||
TimeArg time.Time | ||
Args []core.Value | ||
ShouldErr bool | ||
} | ||
|
||
func (tc *testCase) Do(t *testing.T, fn core.Function) { | ||
Convey(tc.Name, t, func() { | ||
expected := tc.Expected | ||
|
||
actual, err := fn(context.Background(), tc.Args...) | ||
|
||
if tc.ShouldErr { | ||
So(err, ShouldBeError) | ||
expected = values.None | ||
} else { | ||
So(err, ShouldBeNil) | ||
} | ||
|
||
So(actual.Compare(expected), ShouldEqual, 0) | ||
}) | ||
} | ||
|
||
func mustDefaultLayoutDt(timeString string) values.DateTime { | ||
dt, err := defaultLayoutDt(timeString) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return dt | ||
} | ||
|
||
func mustLayoutDt(layout, value string) values.DateTime { | ||
dt, err := layoutDt(layout, value) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return dt | ||
} | ||
|
||
func defaultLayoutDt(timeString string) (values.DateTime, error) { | ||
return layoutDt(values.DefaultTimeLayout, timeString) | ||
} | ||
|
||
func layoutDt(layout, value string) (values.DateTime, error) { | ||
t, err := time.Parse(layout, value) | ||
if err != nil { | ||
return values.DateTime{}, err | ||
} | ||
return values.NewDateTime(t), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package datetime | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/MontFerret/ferret/pkg/runtime/core" | ||
"github.com/MontFerret/ferret/pkg/runtime/values" | ||
) | ||
|
||
// DateYear returns the year extracted from the given date. | ||
// @params date (DateTime) - source DateTime. | ||
// @return (Int) - a year number. | ||
func DateYear(_ 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 | ||
} | ||
|
||
year := args[0].(values.DateTime).Year() | ||
|
||
return values.NewInt(year), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 TestDateYear(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 1999th year", | ||
Expected: values.NewInt(1999), | ||
Args: []core.Value{mustDefaultLayoutDt("1999-02-07T15:04:05Z")}, | ||
}, | ||
&testCase{ | ||
Name: "When 1629th year", | ||
Expected: values.NewInt(1629), | ||
Args: []core.Value{mustDefaultLayoutDt("1629-02-08T15:04:05Z")}, | ||
}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
tc.Do(t, datetime.DateYear) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels that we got a pattern here which is needed to be extracted and reused in the future :)