Skip to content

Commit 9a24f78

Browse files
committed
Added timestamp range check
1 parent 2527d80 commit 9a24f78

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

table/src/main/java/tech/ydb/table/values/PrimitiveValue.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,18 +291,29 @@ public static PrimitiveValue newDatetime(LocalDateTime value) {
291291

292292
public static PrimitiveValue newTimestamp(long microsSinceEpoch) {
293293
if (microsSinceEpoch < 0) {
294-
throw new IllegalArgumentException("Negative microsSinceEpoch: " + microsSinceEpoch);
294+
throw new IllegalArgumentException("microsSinceEpoch value is before "
295+
+ "minimum timestamp(1970-01-01 00:00:00.000000): " + microsSinceEpoch);
296+
}
297+
if (microsSinceEpoch >= 4291747200000000L) {
298+
throw new IllegalArgumentException("microsSinceEpoch value is after "
299+
+ "maximum timestamp(2105-12-31 23:59:59.999999): " + microsSinceEpoch);
295300
}
296301
return new InstantValue(PrimitiveType.Timestamp, microsSinceEpoch);
297302
}
298303

299304
public static PrimitiveValue newTimestamp(Instant value) {
300305
long seconds = value.getEpochSecond();
301306
if (seconds < 0) {
302-
throw new IllegalArgumentException("Instant before epoch: " + value);
307+
throw new IllegalArgumentException("Instant value is before "
308+
+ "minimum timestamp(1970-01-01 00:00:00.000000): " + value);
303309
}
304310
int nanos = value.getNano();
305311
long micros = seconds * 1000000L + nanos / 1000;
312+
if (micros >= 4291747200000000L) {
313+
throw new IllegalArgumentException("Instant value is after "
314+
+ "maximum timestamp(2105-12-31 23:59:59.999999): " + value
315+
);
316+
}
306317
return new InstantValue(PrimitiveType.Timestamp, micros);
307318
}
308319

table/src/test/java/tech/ydb/table/values/PrimitiveValueTest.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -539,15 +539,30 @@ public void timestamp() {
539539
Assert.assertEquals(0, maxValue.getLow128());
540540
Assert.assertEquals(0, maxValue.getHigh128());
541541

542-
IllegalArgumentException err1 = Assert.assertThrows(
543-
IllegalArgumentException.class, () -> PrimitiveValue.newTimestamp(-1)
542+
Assert.assertEquals(
543+
"microsSinceEpoch value is before minimum timestamp(1970-01-01 00:00:00.000000): -1",
544+
Assert.assertThrows(IllegalArgumentException.class,
545+
() -> PrimitiveValue.newTimestamp(-1)
546+
).getMessage()
544547
);
545-
Assert.assertEquals("Negative microsSinceEpoch: -1", err1.getMessage());
546-
547-
IllegalArgumentException err2 = Assert.assertThrows(
548-
IllegalArgumentException.class, () -> PrimitiveValue.newTimestamp(Instant.EPOCH.minusNanos(1))
548+
Assert.assertEquals(
549+
"Instant value is before minimum timestamp(1970-01-01 00:00:00.000000): 1969-12-31T23:59:59.999999999Z",
550+
Assert.assertThrows(IllegalArgumentException.class,
551+
() -> PrimitiveValue.newTimestamp(Instant.EPOCH.minusNanos(1))
552+
).getMessage()
553+
);
554+
Assert.assertEquals(
555+
"microsSinceEpoch value is after maximum timestamp(2105-12-31 23:59:59.999999): 4291747200000000",
556+
Assert.assertThrows(IllegalArgumentException.class,
557+
() -> PrimitiveValue.newTimestamp(4291747200000000l)
558+
).getMessage()
559+
);
560+
Assert.assertEquals(
561+
"Instant value is after maximum timestamp(2105-12-31 23:59:59.999999): 2106-01-01T00:00:00Z",
562+
Assert.assertThrows(IllegalArgumentException.class,
563+
() -> PrimitiveValue.newTimestamp(Instant.parse("2106-01-01T00:00:00.000000Z"))
564+
).getMessage()
549565
);
550-
Assert.assertEquals("Instant before epoch: 1969-12-31T23:59:59.999999999Z", err2.getMessage());
551566
}
552567

553568
@Test

0 commit comments

Comments
 (0)