Skip to content

Commit b2c3ac9

Browse files
committed
chore: demo new Timestamp test usign threeten in java-core
1 parent 7661f0a commit b2c3ac9

File tree

2 files changed

+66
-8
lines changed

2 files changed

+66
-8
lines changed

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,20 @@
2121
import com.google.api.core.ObsoleteApi;
2222
import com.google.protobuf.util.Timestamps;
2323
import java.io.Serializable;
24-
import java.time.Instant;
25-
import java.time.LocalDateTime;
26-
import java.time.ZoneOffset;
27-
import java.time.format.DateTimeFormatter;
28-
import java.time.format.DateTimeFormatterBuilder;
29-
import java.time.format.DateTimeParseException;
30-
import java.time.temporal.TemporalAccessor;
24+
//import java.time.Instant;
25+
//import java.time.LocalDateTime;
26+
//import java.time.ZoneOffset;
27+
//import java.time.format.DateTimeFormatter;
28+
//import java.time.format.DateTimeFormatterBuilder;
29+
//import java.time.format.DateTimeParseException;
30+
//import java.time.temporal.TemporalAccessor;
31+
import org.threeten.bp.Instant;
32+
import org.threeten.bp.LocalDateTime;
33+
import org.threeten.bp.ZoneOffset;
34+
import org.threeten.bp.format.DateTimeFormatter;
35+
import org.threeten.bp.format.DateTimeFormatterBuilder;
36+
import org.threeten.bp.format.DateTimeParseException;
37+
import org.threeten.bp.temporal.TemporalAccessor;
3138
import java.util.Date;
3239
import java.util.Objects;
3340
import java.util.concurrent.TimeUnit;

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

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,24 +237,75 @@ void parseTimestampWithoutTimeZoneOffset() {
237237

238238
@Test
239239
void parseTimestampWithTimeZoneOffset() {
240+
// Max values
240241
assertThat(Timestamp.parseTimestampDuration("0001-01-01T00:00:00-00:00"))
241242
.isEqualTo(Timestamp.MIN_VALUE);
242243
assertThat(Timestamp.parseTimestampDuration("9999-12-31T23:59:59.999999999-00:00"))
243244
.isEqualTo(Timestamp.MAX_VALUE);
245+
// Extreme values (close to min/max)
246+
assertThat(Timestamp.parseTimestampDuration("0001-01-01T00:00:00.000000001Z"))
247+
.isEqualTo(Timestamp.ofTimeSecondsAndNanos(Timestamp.MIN_VALUE.getSeconds(), 1));
248+
assertThat(Timestamp.parseTimestampDuration("9999-12-31T23:59:59.999999998Z"))
249+
.isEqualTo(Timestamp.ofTimeSecondsAndNanos(Timestamp.MAX_VALUE.getSeconds(), 999999998));
250+
// Common use cases
244251
assertThat(Timestamp.parseTimestampDuration("2020-07-10T14:03:00-07:00"))
245252
.isEqualTo(Timestamp.ofTimeSecondsAndNanos(1594414980, 0));
246253
assertThat(Timestamp.parseTimestampDuration("2020-12-06T19:21:12.123+05:30"))
247254
.isEqualTo(Timestamp.ofTimeSecondsAndNanos(1607262672, 123000000));
248-
// we also confirm that parsing a timestamp with nano precision will behave the same as the
255+
// We also confirm that parsing a timestamp with nano precision will behave the same as the
249256
// threeten counterpart
250257
assertThat(Timestamp.parseTimestampDuration("2020-12-06T19:21:12.123+05:30"))
251258
.isEqualTo(Timestamp.parseTimestamp("2020-12-06T19:21:12.123+05:30"));
259+
// Timestamps with fractional seconds at nanosecond level
260+
assertThat(Timestamp.parseTimestampDuration("2020-12-06T19:21:12.123456789+05:30"))
261+
.isEqualTo(Timestamp.ofTimeSecondsAndNanos(1607262672, 123456789));
262+
// Fractional seconds beyond nanos should throw an exception
263+
assertThrows(
264+
org.threeten.bp.format.DateTimeParseException.class,
265+
// DateTimeParseException.class,
266+
() -> Timestamp.parseTimestampDuration("2020-12-06T19:21:12.123456789321+05:30"));
267+
// Missing components (should throw exceptions)
268+
assertThrows(
269+
// DateTimeParseException.class,
270+
org.threeten.bp.format.DateTimeParseException.class,
271+
() -> Timestamp.parseTimestampDuration("2020-12-06"));
272+
// Whitespace should not be supported
273+
assertThrows(
274+
// DateTimeParseException.class,
275+
org.threeten.bp.format.DateTimeParseException.class,
276+
() -> Timestamp.parseTimestampDuration(" 2020-12-06T19:21:12.123+05:30 "));
277+
// It should be case-insensitive
278+
assertThat(Timestamp.parseTimestampDuration("2020-07-10t14:03:00-07:00"))
279+
.isEqualTo(Timestamp.ofTimeSecondsAndNanos(1594414980, 0));
280+
// Invalid time zone offsets
281+
assertThrows(
282+
// DateTimeParseException.class,
283+
org.threeten.bp.format.DateTimeParseException.class,
284+
() -> Timestamp.parseTimestampDuration("2020-12-06T19:21:12.123+25:00"));
285+
assertThrows(
286+
// DateTimeParseException.class,
287+
org.threeten.bp.format.DateTimeParseException.class,
288+
() -> Timestamp.parseTimestampDuration("2020-12-06T19:21:12.123-25:00"));
289+
// Int values for SecondOfMinute should be between 0 and 59
290+
assertThrows(
291+
// DateTimeParseException.class,
292+
org.threeten.bp.format.DateTimeParseException.class,
293+
() -> Timestamp.parseTimestampDuration("2016-12-31T23:59:60Z"));
252294
}
253295

254296
@Test
255297
void parseTimestampWithZoneString() {
298+
// Valid RFC 3339 timestamps with time zone names
256299
assertThat(Timestamp.parseTimestampDuration("2020-12-06T08:51:12.123America/Toronto"))
257300
.isEqualTo(Timestamp.ofTimeSecondsAndNanos(1607262672, 123000000));
301+
assertThat(Timestamp.parseTimestampDuration("2023-04-10T22:42:10.123456789Europe/London"))
302+
.isEqualTo(Timestamp.ofTimeSecondsAndNanos(1681162930, 123456789));
303+
304+
// Invalid time zone names
305+
assertThrows(
306+
org.threeten.bp.format.DateTimeParseException.class,
307+
// DateTimeParseException.class,
308+
() -> Timestamp.parseTimestampDuration("2020-12-06T19:21:12.123Invalid/TimeZone"));
258309
}
259310

260311
@Test

0 commit comments

Comments
 (0)