Skip to content

Commit

Permalink
Use gettimeofday to provide usec Time resolution. Fixes #4393.
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Jan 10, 2017
1 parent fa9060d commit a47fd03
Show file tree
Hide file tree
Showing 3 changed files with 22 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
22 changes: 20 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.Timeval;
import org.jcodings.specific.USASCIIEncoding;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
Expand Down Expand Up @@ -390,9 +392,25 @@ 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);
long msecs;
long nsecs;
POSIX posix = runtime.getPosix();
if (posix.isNative()) {
Timeval tv = posix.allocateTimeval();
posix.gettimeofday(tv);

long secs = tv.sec();
long usecs = tv.usec();

msecs = secs * 1000 + (usecs / 1000);
nsecs = usecs % 1000 * 1000;
} else {
msecs = System.currentTimeMillis();
nsecs = 0;
}
DateTime dt = new DateTime(msecs, dtz);
RubyTime rt = new RubyTime(runtime, klass, dt);
rt.setNSec(0);
rt.setNSec(nsecs);

return rt;
}
Expand Down

0 comments on commit a47fd03

Please sign in to comment.