Skip to content
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

ToValue doesn't convert time.Time to JS Date #170

Closed
slinkydeveloper opened this issue Aug 4, 2020 · 5 comments
Closed

ToValue doesn't convert time.Time to JS Date #170

slinkydeveloper opened this issue Aug 4, 2020 · 5 comments

Comments

@slinkydeveloper
Copy link

As in the issue title, I stumbled on this issue where i'm trying to set a time.Time as variable in the engine:

func TestDate(t *testing.T) {
	vm := goja.New()
	vm.Set("time", time.Now())
	res, err := vm.RunString("time.getFullYear()")
	require.NoError(t, err)
	require.Equal(t, 2020, res.ToInteger())
}
=== RUN   TestDate
    filter_engine_test.go:44: 
        	Error Trace:	filter_engine_test.go:44
        	Error:      	Received unexpected error:
        	            	TypeError: Object has no member 'getFullYear' at <eval>:1:17(3)
        	Test:       	TestDate
--- FAIL: TestDate (0.00s)
@dop251
Copy link
Owner

dop251 commented Aug 4, 2020

Please see #72

@dop251 dop251 closed this as completed Aug 4, 2020
@slinkydeveloper
Copy link
Author

Thank you!

@slinkydeveloper
Copy link
Author

slinkydeveloper commented Aug 4, 2020

I'm trying to use vm.New(vm.GlobalObject().Get("Date").ToObject(vm), vm.ToValue(t.Unix())) from golang side to create a Date (so i can then feed the runtime with Set), but that doesn't seem to work. Or better, i get a date which is empty... Is there something i'm overlooking?

@dop251
Copy link
Owner

dop251 commented Aug 4, 2020

Sorry, I forgot that Unix() returns seconds and the Date constructor requires milliseconds. So, you need t.UnixNano()/1e6

@slinkydeveloper
Copy link
Author

That works fine, thanks! What about documenting it somewhere (like in the readme)? I can pr it if you want to

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants