Skip to content

Commit f076fbc

Browse files
author
duke
committedDec 23, 2023
Automatic merge of jdk:master into master
2 parents e28052c + 4fc6b0f commit f076fbc

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed
 

‎src/java.sql/share/classes/java/sql/Timestamp.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -542,7 +542,7 @@ public LocalDateTime toLocalDateTime() {
542542
*/
543543
public static Timestamp from(Instant instant) {
544544
try {
545-
Timestamp stamp = new Timestamp(instant.getEpochSecond() * MILLIS_PER_SECOND);
545+
Timestamp stamp = new Timestamp(Math.multiplyExact(instant.getEpochSecond(), MILLIS_PER_SECOND));
546546
stamp.nanos = instant.getNano();
547547
return stamp;
548548
} catch (ArithmeticException ex) {

‎test/jdk/java/sql/testng/test/sql/TimestampTests.java

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -642,6 +642,31 @@ public void test52(long value, String ts) {
642642
assertEquals(ts1.toString(), ts, "ts1.toString() != ts");
643643
}
644644

645+
@Test
646+
public void test53() {
647+
// The latest Instant that can be converted to a Timestamp.
648+
Instant instant1 = Instant.ofEpochSecond(Long.MAX_VALUE / 1000, 999_999_999);
649+
assertEquals(Timestamp.from(instant1).toInstant(), instant1);
650+
651+
// One nanosecond more, and converting it gets an overflow.
652+
Instant instant2 = instant1.plusNanos(1);
653+
expectThrows(IllegalArgumentException.class, () -> Timestamp.from(instant2));
654+
655+
// The earliest Instant that can be converted to a Timestamp.
656+
Instant instant3 = Instant.ofEpochSecond(Long.MIN_VALUE / 1000, 0);
657+
assertEquals(Timestamp.from(instant3).toInstant(), instant3);
658+
659+
// One nanosecond less, and converting it gets an overflow.
660+
Instant instant4 = instant3.minusNanos(1);
661+
expectThrows(IllegalArgumentException.class, () -> Timestamp.from(instant4));
662+
663+
// The latest possible Instant will certainly overflow.
664+
expectThrows(IllegalArgumentException.class, () -> Timestamp.from(Instant.MAX));
665+
666+
// The earliest possible Instant will certainly overflow.
667+
expectThrows(IllegalArgumentException.class, () -> Timestamp.from(Instant.MIN));
668+
}
669+
645670
/*
646671
* DataProvider used to provide Timestamps which are not valid and are used
647672
* to validate that an IllegalArgumentException will be thrown from the

0 commit comments

Comments
 (0)
Please sign in to comment.