Java Code Examples for java.time.Duration#getNano()

The following examples show how to use java.time.Duration#getNano() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GaussianWavePV.java    From phoebus with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public double[] compute()
{
    final double[] value = new double[shape.length];

    final Duration dist = Duration.between(start, Instant.now());
    final double t = dist.getSeconds() + dist.getNano()*1e-9;
    final double periods = period > 0 ? t / period : 0.0;
    final int i0 = (int) ((periods - (int)periods) * value.length);

    // value[..] = shape[i0 to end] concatenated with shape[0 to i0-1]
    final int rest = value.length - i0;
    System.arraycopy(shape, i0, value, 0, rest);
    System.arraycopy(shape, 0, value, rest, i0);
    return value;
}
 
Example 2
Source File: TimeDuration.java    From diirt with MIT License 5 votes vote down vote up
/**
 * Returns the number of times the given duration is present in this duration.
 *
 * @param dividendDuration the duration to be devided
 * @param divisorDuration the divisor
 * @return the result of the division
 */
public static int dividedBy(Duration dividendDuration, Duration divisorDuration) {
    // (a + b)/(c + d) = 1 / ((c + d) / a) + 1 / ((c + d) / b)
    // XXX This will not have the precision it should
    double thisDuration = (double) dividendDuration.getSeconds() * (double) NANOSEC_IN_SEC + (double) dividendDuration.getNano();
    double otherDuration = (double) divisorDuration.getSeconds() * (double) NANOSEC_IN_SEC + (double) divisorDuration.getNano();
    return (int) (thisDuration / otherDuration);
}
 
Example 3
Source File: MongoHealthCheckRequester.java    From hesperides with GNU General Public License v3.0 5 votes vote down vote up
static MongoHealthCheckResult performHealthCheck(MongoTemplate mongoTemplate) {
    Instant start = Instant.now();
    Document result = mongoTemplate.executeCommand("{ buildInfo: 1 }");
    Instant end = Instant.now();
    Duration duration = Duration.between(start, end);
    return new MongoHealthCheckResult(
            result.getString("version"),
            duration.getSeconds() * 1000 + duration.getNano() / 1000000.0);
}
 
Example 4
Source File: TimePeriod.java    From java-timeseries with MIT License 5 votes vote down vote up
/**
 * The total amount of time in this time period measured in seconds, the base SI unit of time.
 *
 * @return the total amount of time in this time period measured in seconds.
 */
public double totalSeconds() {
  final double nanoSecondsPerSecond = 1E9;
  Duration thisDuration = this.timeUnit.getDuration();
  double seconds = thisDuration.getSeconds() * this.length;
  double nanos = thisDuration.getNano();
  nanos = (nanos * this.length);
  nanos = (nanos / nanoSecondsPerSecond);
  return seconds + nanos;
}
 
Example 5
Source File: UUIDGenerator.java    From tutorials with MIT License 5 votes vote down vote up
private static long get64MostSignificantBitsForVersion1() {
    LocalDateTime start = LocalDateTime.of(1582, 10, 15, 0, 0, 0);
    Duration duration = Duration.between(start, LocalDateTime.now());
    long seconds = duration.getSeconds();
    long nanos = duration.getNano();
    long timeForUuidIn100Nanos = seconds * 10000000 + nanos * 100;
    long least12SignificatBitOfTime = (timeForUuidIn100Nanos & 0x000000000000FFFFL) >> 4;
    long version = 1 << 12;
    return (timeForUuidIn100Nanos & 0xFFFFFFFFFFFF0000L) + version + least12SignificatBitOfTime;
}
 
Example 6
Source File: DurationFormatter.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
public static StringBuffer format(Duration d, StringBuffer b) {
    if (ValuesChecker.isMaxDuration(d)) return b.append("∞"); // NOI18N
        
    long s = d.getSeconds();
    if (s > 0) formatSeconds(s, b);

    int n = d.getNano();
    return b.append(DURATION_MS_FORMAT.format(n / 1000000f)).append(" ms"); // NOI18N
}
 
Example 7
Source File: TimeDuration.java    From phoebus with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the number of times the given duration is present in this duration.
 *
 * @param dividendDuration the duration to be devided
 * @param divisorDuration the divisor
 * @return the result of the division
 */
public static int dividedBy(Duration dividendDuration, Duration divisorDuration) {
    // (a + b)/(c + d) = 1 / ((c + d) / a) + 1 / ((c + d) / b)
    // XXX This will not have the precision it should
    double thisDuration = (double) dividendDuration.getSeconds() * (double) NANOSEC_IN_SEC + (double) dividendDuration.getNano();
    double otherDuration = (double) divisorDuration.getSeconds() * (double) NANOSEC_IN_SEC + (double) divisorDuration.getNano();
    return (int) (thisDuration / otherDuration);
}
 
Example 8
Source File: DurationCodec.java    From r2dbc-mysql with Apache License 2.0 5 votes vote down vote up
private void encodeTo(ParameterWriter writer) {
    boolean isNegative = this.value.isNegative();
    Duration abs = this.value.abs();
    long totalSeconds = abs.getSeconds();
    int hours = (int) (totalSeconds / SECONDS_OF_HOUR);
    int minutes = (int) ((totalSeconds % SECONDS_OF_HOUR) / SECONDS_OF_MINUTE);
    int seconds = (int) (totalSeconds % SECONDS_OF_MINUTE);
    int micros = abs.getNano() / NANOS_OF_MICRO;

    if (hours < 0 || minutes < 0 || seconds < 0 || micros < 0) {
        throw new IllegalStateException(String.format("Too large duration %s, abs value overflowing to %02d:%02d:%02d.%06d", value, hours, minutes, seconds, micros));
    }

    encodeTime(writer, isNegative, hours, minutes, seconds, micros);
}
 
Example 9
Source File: StringFormatUtils.java    From datakernel with Apache License 2.0 5 votes vote down vote up
public static String formatDuration(Duration value) {
	if (value.isZero()) {
		return "0 seconds";
	}
	String result = "";
	long days, hours, minutes, seconds, nano, milli;
	days = value.toDays();
	if (days != 0) {
		result += days + " days ";
	}
	hours = value.toHours() - days * 24;
	if (hours != 0) {
		result += hours + " hours ";
	}
	minutes = value.toMinutes() - days * 1440 - hours * 60;
	if (minutes != 0) {
		result += minutes + " minutes ";
	}
	seconds = value.getSeconds() - days * 86400 - hours * 3600 - minutes * 60;
	if (seconds != 0) {
		result += seconds + " seconds ";
	}
	nano = value.getNano();
	milli = (nano - nano % 1000000) / 1000000;
	if (milli != 0) {
		result += milli + " millis ";
	}
	nano = nano % 1000000;
	if (nano != 0) {
		result += nano + " nanos ";
	}
	return result.trim();
}
 
Example 10
Source File: TimeAxis.java    From phoebus with Eclipse Public License 1.0 5 votes vote down vote up
/** Scale Duration by floating point number
 *  @param start Start of a duration
 *  @param end End of a duration
 *  @param factor Scaling factor
 *  @return Scaled Duration
 */
final private static Duration scaledDuration(final Instant start, final Instant end, final double factor)
{
    final Duration duration = Duration.between(start, end);
    final double scaled = (duration.getSeconds() + 1e-9*duration.getNano()) * factor;
    final int seconds = (int)scaled;
    final int nano = (int) ((scaled - seconds) * 1e9);
    return Duration.ofSeconds(seconds, nano);
}
 
Example 11
Source File: CypherDuration.java    From vertexium with Apache License 2.0 5 votes vote down vote up
public CypherDuration(Period period, Duration duration) {
    this.negative = (period.isNegative() || duration.isNegative());

    if (period.isNegative()) {
        period = period.negated();
    }
    if (duration.isNegative()) {
        duration = duration.negated();
    }

    this.nanos = ((duration.getSeconds() * NANOS_IN_SECOND) + duration.getNano()) % NANOS_IN_DAY;
    this.period = period.minusDays(this.nanos > 0 ? 1 : 0);
}
 
Example 12
Source File: ExchangeableTradingTimes.java    From java-trader with Apache License 2.0 5 votes vote down vote up
/**
 * 返回开市以来的时间(毫秒)
 */
public int getTradingTime(LocalDateTime marketTime) {
    if ( marketTime.isBefore(marketTimes[0]) || compareTimeNoNanos(marketTime,marketTimes[marketTimes.length-1])>0) {
        return -1;
    }
    int result = 0;
    for(int i=0;i<marketTimes.length;i+=2) {
        LocalDateTime marketTimeStageBegin = marketTimes[i];
        LocalDateTime marketTimeStageEnd = marketTimes[i+1];
        if ( compareTimeNoNanos(marketTime, marketTimeStageBegin)<=0 ) {
            break;
        }
        Duration d = null;
        int compareResult = compareTimeNoNanos(marketTime, marketTimeStageEnd);
        if ( compareResult<=0 ) {
            d = DateUtil.between(marketTimeStageBegin, marketTime);
        }else {
            d = DateUtil.between(marketTimeStageBegin, marketTimeStageEnd);
        }
        long millis = d.getSeconds()*1000+d.getNano()/1000000;
        result += millis;
        if ( compareResult<=0 ) {
            break;
        }
    }
    return result;
}
 
Example 13
Source File: DurationCodec.java    From r2dbc-mysql with Apache License 2.0 5 votes vote down vote up
private void encodeTo(ParameterWriter writer) {
    boolean isNegative = this.value.isNegative();
    Duration abs = this.value.abs();
    long totalSeconds = abs.getSeconds();
    int hours = (int) (totalSeconds / SECONDS_OF_HOUR);
    int minutes = (int) ((totalSeconds % SECONDS_OF_HOUR) / SECONDS_OF_MINUTE);
    int seconds = (int) (totalSeconds % SECONDS_OF_MINUTE);
    int micros = abs.getNano() / NANOS_OF_MICRO;

    if (hours < 0 || minutes < 0 || seconds < 0 || micros < 0) {
        throw new IllegalStateException(String.format("Too large duration %s, abs value overflowing to %02d:%02d:%02d.%06d", value, hours, minutes, seconds, micros));
    }

    encodeTime(writer, isNegative, hours, minutes, seconds, micros);
}
 
Example 14
Source File: TimeUtils.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public static Duration multiply(Duration duration, double factor) {
    final long nanosPerSecond = ChronoUnit.SECONDS.getDuration().toNanos();
    final long nanos = (long) (duration.getNano() * factor);
    return Duration.ofSeconds(Math.addExact((long) (duration.getSeconds() * factor), Math.floorDiv(nanos, nanosPerSecond)),
                              Math.floorMod(nanos, nanosPerSecond));
}
 
Example 15
Source File: TimeUtils.java    From IGUANA with GNU Affero General Public License v3.0 4 votes vote down vote up
public static double durationInMilliseconds(Instant start, Instant end) {
	Duration duration = Duration.between(start, end);
	return ((long)duration.getNano() + duration.getSeconds() * 1000000000 /*ns*/) / 1000000d /*ms*/;
}
 
Example 16
Source File: ValuesConverter.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
public static long durationToMicros(Duration duration) {
    long micros = duration.getSeconds() * 1000000;
    micros += duration.getNano() / 1000;
    return micros;
}
 
Example 17
Source File: DurationHandle.java    From joyrpc with Apache License 2.0 4 votes vote down vote up
@Override
public void wrap(final Duration duration) {
    this.seconds = duration.getSeconds();
    this.nano = duration.getNano();
}
 
Example 18
Source File: DurationHandle.java    From alibaba-rsocket-broker with Apache License 2.0 4 votes vote down vote up
public DurationHandle(Duration o) {
    this.seconds = o.getSeconds();
    this.nanos = o.getNano();
}
 
Example 19
Source File: TimeDuration.java    From diirt with MIT License 2 votes vote down vote up
/**
 *  Convert the entire duration to seconds represented in a double
 *
 * @param duration the duration to be converted
 * @return the duration in seconds
 */
public static double toSecondsDouble(Duration duration){
    return duration.getSeconds() + (duration.getNano() / (double) NANOSEC_IN_SEC);
}
 
Example 20
Source File: ValuesChecker.java    From visualvm with GNU General Public License v2.0 votes vote down vote up
public static boolean isMaxDuration(Duration val) { return 9223372036854775l == val.getSeconds() && 807000000 == val.getNano(); }