Skip to content
This repository was archived by the owner on Dec 3, 2023. It is now read-only.

Commit 9bfb54c

Browse files
authored
fix: fix Timestamp.of(java.sql.Timestamp) pre-epoch on exact second (#179)
* fix: Timestamp.of(java.sql.Timestamp) pre-epoch on exact second * fix: review comments * fix: review comments * fix: make all testOf method consistent
1 parent 6d5632c commit 9bfb54c

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

google-cloud-core/src/main/java/com/google/cloud/Timestamp.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,13 @@ public static Timestamp now() {
121121
public static Timestamp of(java.sql.Timestamp timestamp) {
122122
int nanos = timestamp.getNanos();
123123

124-
// A pre-epoch timestamp will be off by one second because of the way that integer division
125-
// works. For example, -1001 / 1000 == -1. In this case of timestamps, we want this result to be
126-
// -2. This causes any pre-epoch timestamp to be off by 1 second - fix this by adjusting the
127-
// seconds value by 1 if the timestamp < 0.
124+
// A pre-epoch timestamp can be off by one second because of the way that integer division
125+
// works. For example, -1001 / 1000 == -1. In this case, we want this result to be -2. This
126+
// causes any pre-epoch timestamp to be off by 1 second - fix this by subtracting 1 from the
127+
// seconds value if the seconds value is less than zero and is not divisible by 1000.
128128
// TODO: replace with Math.floorDiv when we drop Java 7 support
129129
long seconds = timestamp.getTime() / 1000;
130-
if (seconds < 0) {
130+
if (seconds < 0 && timestamp.getTime() % 1000 != 0) {
131131
--seconds;
132132
}
133133

google-cloud-core/src/test/java/com/google/cloud/TimestampTest.java

+23-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static com.google.common.testing.SerializableTester.reserializeAndAssert;
2020
import static com.google.common.truth.Truth.assertThat;
21+
import static org.junit.Assert.assertEquals;
2122
import static org.junit.Assert.assertNotNull;
2223

2324
import com.google.common.testing.EqualsTester;
@@ -81,27 +82,43 @@ public void ofDate() {
8182
}
8283

8384
@Test
84-
public void ofSqlTimestamp() {
85+
public void testOf() {
8586
String expectedTimestampString = "1970-01-01T00:00:12.345000000Z";
8687
java.sql.Timestamp input = new java.sql.Timestamp(12345);
8788
Timestamp timestamp = Timestamp.of(input);
88-
assertThat(timestamp.toString()).isEqualTo(expectedTimestampString);
89+
assertEquals(timestamp.toString(), expectedTimestampString);
8990
}
9091

9192
@Test
92-
public void ofSqlTimestampPreEpoch() {
93+
public void testOf_exactSecond() {
94+
String expectedTimestampString = "1970-01-01T00:00:12Z";
95+
java.sql.Timestamp input = new java.sql.Timestamp(12000);
96+
Timestamp timestamp = Timestamp.of(input);
97+
assertEquals(timestamp.toString(), expectedTimestampString);
98+
}
99+
100+
@Test
101+
public void testOf_preEpoch() {
93102
String expectedTimestampString = "1969-12-31T23:59:47.655000000Z";
94103
java.sql.Timestamp input = new java.sql.Timestamp(-12345);
95104
Timestamp timestamp = Timestamp.of(input);
96-
assertThat(timestamp.toString()).isEqualTo(expectedTimestampString);
105+
assertEquals(timestamp.toString(), expectedTimestampString);
97106
}
98107

99108
@Test
100-
public void ofSqlTimestampOnEpoch() {
109+
public void testOf_onEpoch() {
101110
String expectedTimestampString = "1970-01-01T00:00:00Z";
102111
java.sql.Timestamp input = new java.sql.Timestamp(0);
103112
Timestamp timestamp = Timestamp.of(input);
104-
assertThat(timestamp.toString()).isEqualTo(expectedTimestampString);
113+
assertEquals(timestamp.toString(), expectedTimestampString);
114+
}
115+
116+
@Test
117+
public void testOf_preEpochExactSecond() {
118+
String expectedTimestampString = "1969-12-31T23:59:59Z";
119+
java.sql.Timestamp input = new java.sql.Timestamp(-1000);
120+
Timestamp timestamp = Timestamp.of(input);
121+
assertEquals(timestamp.toString(), expectedTimestampString);
105122
}
106123

107124
@Test

0 commit comments

Comments
 (0)