8310232: java.time.Clock$TickClock.millis() fails in runtime when tick is 1 microsecond

Reviewed-by: iris, rriggs, jpai
This commit is contained in:
Naoto Sato 2023-06-29 16:15:02 +00:00
parent 26efff7586
commit a995aa6cd1
2 changed files with 11 additions and 3 deletions

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -759,7 +759,7 @@ public abstract class Clock implements InstantSource {
@Override @Override
public long millis() { public long millis() {
long millis = baseClock.millis(); long millis = baseClock.millis();
return millis - Math.floorMod(millis, tickNanos / 1000_000L); return tickNanos < 1000_000L ? millis : millis - Math.floorMod(millis, tickNanos / 1000_000L);
} }
@Override @Override
public Instant instant() { public Instant instant() {

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -94,4 +94,12 @@ public class TestClock_Tick {
assertEquals(test.toString(), "TickClock[SystemClock[Z],PT0.5S]"); assertEquals(test.toString(), "TickClock[SystemClock[Z],PT0.5S]");
} }
//-----------------------------------------------------------------------
// Ensure divide-by-zero will not be thrown
// @bug 8310232
public void test_millis() {
var test = Clock.tick(Clock.systemUTC(), Duration.ofNanos(1000));
test.millis();
}
} }