Skip to content

Commit

Permalink
8310232: java.time.Clock$TickClock.millis() fails in runtime when tic…
Browse files Browse the repository at this point in the history
…k is 1 microsecond

Reviewed-by: iris, rriggs, jpai
  • Loading branch information
naotoj committed Jun 29, 2023
1 parent 26efff7 commit a995aa6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/java.base/share/classes/java/time/Clock.java
Original file line number Diff line number Diff line change
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -759,7 +759,7 @@ public Clock withZone(ZoneId zone) {
@Override
public long 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
public Instant instant() {
Expand Down
10 changes: 9 additions & 1 deletion test/jdk/java/time/test/java/time/TestClock_Tick.java
Original file line number Diff line number Diff line change
@@ -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.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -94,4 +94,12 @@ public void test_toString() {
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();
}

}

1 comment on commit a995aa6

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.