Skip to content

Commit

Permalink
Use jnr-posix to call clock_gettime for current time. Fixes #4393.
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Jan 10, 2017
1 parent 101887c commit ebe1c7c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion core/pom.rb
Expand Up @@ -46,7 +46,7 @@
jar 'com.github.jnr:jnr-enxio:0.14', :exclusions => ['com.github.jnr:jnr-ffi']
jar 'com.github.jnr:jnr-x86asm:1.0.2', :exclusions => ['com.github.jnr:jnr-ffi']
jar 'com.github.jnr:jnr-unixsocket:0.15', :exclusions => ['com.github.jnr:jnr-ffi']
jar 'com.github.jnr:jnr-posix:3.0.33', :exclusions => ['com.github.jnr:jnr-ffi']
jar 'com.github.jnr:jnr-posix:3.0.34-SNAPSHOT', :exclusions => ['com.github.jnr:jnr-ffi']
jar 'com.github.jnr:jnr-constants:0.9.6', :exclusions => ['com.github.jnr:jnr-ffi']
jar 'com.github.jnr:jnr-ffi:2.1.2'
jar 'com.github.jnr:jffi:${jffi.version}'
Expand Down
2 changes: 1 addition & 1 deletion core/pom.xml
Expand Up @@ -135,7 +135,7 @@ DO NOT MODIFIY - GENERATED CODE
<dependency>
<groupId>com.github.jnr</groupId>
<artifactId>jnr-posix</artifactId>
<version>3.0.33</version>
<version>3.0.34-SNAPSHOT</version>
<exclusions>
<exclusion>
<artifactId>jnr-ffi</artifactId>
Expand Down
20 changes: 18 additions & 2 deletions core/src/main/java/org/jruby/RubyTime.java
Expand Up @@ -37,6 +37,8 @@
***** END LICENSE BLOCK *****/
package org.jruby;

import jnr.posix.POSIX;
import jnr.posix.Timespec;
import org.jcodings.specific.USASCIIEncoding;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
Expand Down Expand Up @@ -390,9 +392,23 @@ public RubyTime(Ruby runtime, RubyClass rubyClass, DateTime dt) {
@Override
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
DateTimeZone dtz = getLocalTimeZone(runtime);
DateTime dt = new DateTime(dtz);
POSIX posix = runtime.getPosix();
long millis;
long nanos;
if (posix.isNative()) {
Timespec ts = posix.allocateTimespec();
posix.clock_gettime(0, ts);
long sec = ts.sec();
long nsec = ts.nsec();
millis = sec * 1000 + nsec / 1_000_000;
nanos = nsec % 1_000_000;
} else {
millis = System.currentTimeMillis();
nanos = 0;
}
DateTime dt = new DateTime(millis, dtz);
RubyTime rt = new RubyTime(runtime, klass, dt);
rt.setNSec(0);
rt.setNSec(nanos);

return rt;
}
Expand Down

0 comments on commit ebe1c7c

Please sign in to comment.