-
-
Notifications
You must be signed in to change notification settings - Fork 925
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
Calling Java methods with a Time argument loses microseconds #3081
Comments
sounds like you're designing a native JRuby extention part maybe? ... in that case you're probably be able to handle this on you own ( the coercion needs to happen if there's a |
Thank you for your comment. I'm creating a Java library which works both for Java and JRuby. I could solve this! Apparently, First, I added public class DynamicColumnSetter
{
public void setNull() { ... }
public void set(boolean value) { ... }
public void set(long value) { ... }
public void set(double value) { ... }
public void set(String value) { ... }
public void set(Instant value) { ... }
public IRubyObject setRubyObject(IRubyObject rubyObject)
{
if (rubyObject instanceof RubyNil) {
setNull();
} else if (rubyObject instanceof RubyBoolean) {
RubyBoolean b = (RubyBoolean) rubyObject;
set(b.isTrue());
} else if (rubyObject instanceof RubyInteger) {
RubyInteger i = (RubyInteger) rubyObject;
set(i.getLongValue());
} else if (rubyObject instanceof RubyFloat) {
RubyFloat f = (RubyFloat) rubyObject;
set(f.getDoubleValue());
} else if (rubyObject instanceof RubyString) {
RubyString s = (RubyString) rubyObject;
set(s.asJavaString());
} else if (rubyObject instanceof RubyTime) {
RubyTime time = (RubyTime) rubyObject;
long msec = time.getDateTime().getMillis();
long nsec = time.getNSec();
long sec = msec / 1000 + nsec / 1000000000;
int nano = (int) ((msec % 1000) * 1000000 + nsec % 1000000000);
set(Instant.ofEpochSecond(sec, nano));
} else {
throw rubyObject.getRuntime().newTypeError("cannot convert instance of " + rubyObject.getMetaClass() + " to nil, true, false, Integer, Float, String, or Time");
}
return rubyObject.getRuntime().getNil();
}
} And added following lines to Ruby code: org.embulk.spi.util.dynamic.DynamicColumnSetter.module_eval do
alias_method(:set, :setRubyObject)
end With this way, both Java and Ruby code can call |
When I have this method in Java,
calling this method using following JRuby code shows
java: class java.util.Date
:On the other hand, following java method won't be called:
Ruby's Time object represents seconds and nanoseconds. However,
java.util.Date
represents only milliseconds. So, with the first natural ruby code, microsecond and nanosecond information is lost.I want to avoid this behavior when I implement a library in Java which deals with timestamp objects. An options is to require users of my library to use
java_method(:set, [org.jruby.RubyTime])
but I also want to avoid it because user's code becomes verbose.Is it possible to change this behavior so that
set(Time.now)
callsset(RubyTime v)
if this Java class has this overload?The text was updated successfully, but these errors were encountered: