Skip to content

Commit

Permalink
[stdlib/time] Add support for Time.parse()
Browse files Browse the repository at this point in the history
Signed-off-by: Flipez <code@brauser.io>
  • Loading branch information
Flipez committed Oct 31, 2022
1 parent d98a583 commit bee2c84
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
24 changes: 22 additions & 2 deletions docs/docs/builtins/Time.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
Formats the given unix timestamp with the given layout.

Go [date and time formats](https://gosamples.dev/date-time-format-cheatsheet/) are natively supported.
[Go date and time formats](https://gosamples.dev/date-time-format-cheatsheet/) are natively supported.
You can also use some but not all [formats present in many other languages](https://apidock.com/ruby/Time/strftime) which are not fully supported.
Take a look at [the source](https://github.com/Flipez/rocket-lang/blob/main/stdlib/time.go) to see which formatter are supported.
Take a look at [the source](https://github.com/Flipez/rocket-lang/blob/main/stdlib/time.go) to see which formatters are supported.


```js
Expand All @@ -23,6 +23,26 @@ Take a look at [the source](https://github.com/Flipez/rocket-lang/blob/main/stdl
```


### parse(STRING, STRING)
> Returns `STRING`
Parses a given string with the given format to a unix timestamp.

[Go date and time formats](https://gosamples.dev/date-time-format-cheatsheet/) are natively supported.
You can also use some but not all [formats present in many other languages](https://apidock.com/ruby/Time/strftime) which are not fully supported.
Take a look at [the source](https://github.com/Flipez/rocket-lang/blob/main/stdlib/time.go) to see which formatters are supported.


```js
🚀 » a = "2022-03-23"
» "2022-03-23"
🚀 » format = "2006-01-02"
» "2006-01-02"
🚀 » Time.parse(a, format)
» 1647993600
```


### sleep(INTEGER)
> Returns `NIL`
Expand Down
2 changes: 2 additions & 0 deletions object/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func TestTimeObjectMethods(t *testing.T) {
tests := []inputTestCase{
{`a = 1667144827; Time.format(a, "%a %b %e %M:%S %Y")`, "Sun Oct 30 47:07 2022"},
{`a = 1667144827; Time.format(a, "Mon Jan _2 04:05 2006")`, "Sun Oct 30 47:07 2022"},
{`a = "2022-03-23"; format = "2006-01-02"; Time.parse(a, format)`, 1647993600},
{`a = "2022-03-23"; format = "09-01-02"; Time.parse(a, format)`, `Error while parsing time: parsing time "2022-03-23" as "09-01-02": cannot parse "2022-03-23" as "09-"`},
}

testInput(t, tests)
Expand Down
37 changes: 35 additions & 2 deletions stdlib/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ func init() {
object.MethodLayout{
Description: `Formats the given unix timestamp with the given layout.
Go [date and time formats](https://gosamples.dev/date-time-format-cheatsheet/) are natively supported.
[Go date and time formats](https://gosamples.dev/date-time-format-cheatsheet/) are natively supported.
You can also use some but not all [formats present in many other languages](https://apidock.com/ruby/Time/strftime) which are not fully supported.
Take a look at [the source](https://github.com/Flipez/rocket-lang/blob/main/stdlib/time.go) to see which formatter are supported.`,
Take a look at [the source](https://github.com/Flipez/rocket-lang/blob/main/stdlib/time.go) to see which formatters are supported.`,
ArgPattern: object.Args(
object.Arg(object.INTEGER_OBJ),
object.Arg(object.STRING_OBJ),
Expand All @@ -78,6 +78,39 @@ Take a look at [the source](https://github.com/Flipez/rocket-lang/blob/main/stdl

return object.NewString(time.Format(convertTimeFormat(timeFormat)))
})
timeFunctions["parse"] = object.NewBuiltinFunction(
"parse",
object.MethodLayout{
Description: `Parses a given string with the given format to a unix timestamp.
[Go date and time formats](https://gosamples.dev/date-time-format-cheatsheet/) are natively supported.
You can also use some but not all [formats present in many other languages](https://apidock.com/ruby/Time/strftime) which are not fully supported.
Take a look at [the source](https://github.com/Flipez/rocket-lang/blob/main/stdlib/time.go) to see which formatters are supported.`,
ArgPattern: object.Args(
object.Arg(object.STRING_OBJ),
object.Arg(object.STRING_OBJ),
),
ReturnPattern: object.Args(
object.Arg(object.STRING_OBJ),
),
Example: `🚀 » a = "2022-03-23"
» "2022-03-23"
🚀 » format = "2006-01-02"
» "2006-01-02"
🚀 » Time.parse(a, format)
» 1647993600`,
},
func(_ object.Environment, args ...object.Object) object.Object {
timeString := args[0].(*object.String)
timeFormat := args[1].(*object.String)

timeParsed, err := time.Parse(convertTimeFormat(timeFormat), timeString.Value)
if err != nil {
return object.NewErrorFormat("Error while parsing time: %s", err)
}

return object.NewInteger(timeParsed.Unix())
})
timeFunctions["sleep"] = object.NewBuiltinFunction(
"sleep",
object.MethodLayout{
Expand Down

0 comments on commit bee2c84

Please sign in to comment.