Skip to content

Commit

Permalink
8310241: OffsetDateTime compareTo redundant computation
Browse files Browse the repository at this point in the history
Reviewed-by: naoto
  • Loading branch information
Roger Riggs committed Jun 29, 2023
1 parent d979662 commit 11fd34e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/java.base/share/classes/java/time/OffsetDateTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -1805,7 +1805,13 @@ public long toEpochSecond() {
*/
@Override
public int compareTo(OffsetDateTime other) {
int cmp = compareInstant(this, other);
int cmp = getOffset().compareTo(other.getOffset());
if (cmp != 0) {
cmp = Long.compare(toEpochSecond(), other.toEpochSecond());
if (cmp == 0) {
cmp = toLocalTime().getNano() - other.toLocalTime().getNano();
}
}
if (cmp == 0) {
cmp = toLocalDateTime().compareTo(other.toLocalDateTime());
}
Expand Down
3 changes: 2 additions & 1 deletion test/jdk/java/time/tck/java/time/TCKOffsetDateTime.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2017, 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 @@ -1381,6 +1381,7 @@ public void test_compareTo_hourDifference() {
assertEquals(a.compareTo(a) == 0, true);
assertEquals(b.compareTo(b) == 0, true);
assertEquals(a.toInstant().compareTo(b.toInstant()) == 0, true);
assertEquals(OffsetDateTime.timeLineOrder().compare(a,b) == 0, true);
}

@Test
Expand Down

1 comment on commit 11fd34e

@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.