Skip to content

Instantly share code, notes, and snippets.

@pmahoney
Created October 18, 2012 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmahoney/3913524 to your computer and use it in GitHub Desktop.
Save pmahoney/3913524 to your computer and use it in GitHub Desktop.
RubyTime opPlusNanos maybe
public class Time {
private final long timeInMillis;
private final long nsec;
public Time(long timeInMillis, long nsec) {
this.timeInMillis = timeInMillis;
this.nsec = nsec;
}
@Override
public String toString() {
return "" + timeInMillis + " ms, " + nsec + " ns";
}
public long getTimeInMillis() {
return timeInMillis;
}
public Time origPlusNanos(long adjustNanos) {
double currentNanos = getTimeInMillis() * 1000000 + nsec;
double newNanos = currentNanos + adjustNanos;
double newMillisPart = newNanos / 1000000;
double newNanosPart = newNanos % 1000000;
return new Time((long)newMillisPart, (long)newNanosPart);
}
public Time modPlusNanos(long adjustNanos) {
long adjustMillis = adjustNanos / 1000000;
double currentNanos = getTimeInMillis() * 1000000 + nsec;
double newNanos = currentNanos + adjustNanos;
long newMillisPart = getTimeInMillis() + adjustMillis;
double newNanosPart = newNanos % 1000000;
return new Time(newMillisPart, (long)newNanosPart);
}
private static final long NANOS_PER_MILLI = 1000000;
public Time myPlusNanos(long adjustNanos) {
// split the argument into nanos and millis parts
long splitNanos = adjustNanos % NANOS_PER_MILLI;
long splitMillis = adjustNanos / NANOS_PER_MILLI;
if (splitNanos < 0) {
splitNanos += NANOS_PER_MILLI;
splitMillis -= 1;
}
final long newNanosPart = (splitNanos + nsec) % NANOS_PER_MILLI;
final long overflowMillis = (splitNanos + nsec) / NANOS_PER_MILLI;
final long newMillisPart = splitMillis + overflowMillis + getTimeInMillis();
return new Time(newMillisPart, newNanosPart);
}
}
public class TimeTest {
public static Time adjust(Time time, long adjustNanos) {
return time.myPlusNanos(adjustNanos);
}
@Test
public void test() {
{
Time time = new Time(0, 0);
assertEquals("0 ms, 1 ns", adjust(time, 1).toString());
assertEquals("0 ms, 100 ns", adjust(time, 100).toString());
assertEquals("0 ms, 100000 ns", adjust(time, 100000).toString());
assertEquals("1 ms, 0 ns", adjust(time, 1000000).toString());
}
{
Time time = new Time(0, 999999);
assertEquals("1 ms, 0 ns", adjust(time, 1).toString());
assertEquals("1 ms, 99 ns", adjust(time, 100).toString());
assertEquals("1 ms, 99999 ns", adjust(time, 100000).toString());
assertEquals("1 ms, 999999 ns", adjust(time, 1000000).toString());
}
{
Time time = new Time(1000000000000L, 0);
assertEquals("1000000000000 ms, 1 ns", adjust(time, 1).toString());
assertEquals("1000000000000 ms, 100 ns", adjust(time, 100).toString());
assertEquals("1000000000000 ms, 100000 ns", adjust(time, 100000).toString());
assertEquals("1000000000001 ms, 0 ns", adjust(time, 1000000).toString());
}
{
Time time = new Time(1, 1);
assertEquals("0 ms, 1 ns", adjust(time, -1000000).toString());
assertEquals("1 ms, 0 ns", adjust(time, -1).toString());
assertEquals("0 ms, 999999 ns", adjust(time, -2).toString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment