下面列出了java.time.Duration#getNano ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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;
}
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);
}
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);
}
/**
* 返回开市以来的时间(毫秒)
*/
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;
}
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);
}
/** 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);
}
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();
}
/**
* 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);
}
/**
* 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);
}
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
}
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;
}
/**
* 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;
}
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);
}
public DurationHandle(Duration o) {
this.seconds = o.getSeconds();
this.nanos = o.getNano();
}
@Override
public void wrap(final Duration duration) {
this.seconds = duration.getSeconds();
this.nano = duration.getNano();
}
public static double durationInMilliseconds(Instant start, Instant end) {
Duration duration = Duration.between(start, end);
return ((long)duration.getNano() + duration.getSeconds() * 1000000000 /*ns*/) / 1000000d /*ms*/;
}
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));
}
public static long durationToMicros(Duration duration) {
long micros = duration.getSeconds() * 1000000;
micros += duration.getNano() / 1000;
return micros;
}
/**
* 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);
}
public static boolean isMaxDuration(Duration val) { return 9223372036854775l == val.getSeconds() && 807000000 == val.getNano(); }