Merge pull request #43612 from gavarava

* pr/43612:
  Polish "Preserve milliseconds in build info timestamp"
  Preserve milliseconds in build info timestamp

Closes gh-43612
This commit is contained in:
Stéphane Nicoll 2024-12-26 16:08:19 +01:00
commit 12e753c197
2 changed files with 9 additions and 8 deletions

View File

@ -21,7 +21,6 @@ import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import org.springframework.util.StringUtils;
@ -86,10 +85,7 @@ class MavenBuildOutputTimestamp {
return null;
}
try {
Instant instant = OffsetDateTime.parse(this.timestamp)
.withOffsetSameInstant(ZoneOffset.UTC)
.truncatedTo(ChronoUnit.SECONDS)
.toInstant();
Instant instant = OffsetDateTime.parse(this.timestamp).withOffsetSameInstant(ZoneOffset.UTC).toInstant();
if (instant.isBefore(DATE_MIN) || instant.isAfter(DATE_MAX)) {
throw new IllegalArgumentException(
String.format("'%s' is not within the valid range %s to %s", instant, DATE_MIN, DATE_MAX));

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -48,12 +48,17 @@ class MavenBuildOutputTimestampTests {
@Test
void shouldParseIso8601() {
assertThat(parse("2011-12-03T10:15:30Z")).isEqualTo(Instant.parse("2011-12-03T10:15:30Z"));
assertThat(parse("2011-12-03T10:15:30Z")).isEqualTo(Instant.parse("2011-12-03T10:15:30.000Z"));
}
@Test
void shouldParseIso8601WithMilliseconds() {
assertThat(parse("2011-12-03T10:15:30.12345Z")).isEqualTo(Instant.parse("2011-12-03T10:15:30Z"));
assertThat(parse("2011-12-03T10:15:30.123Z")).isEqualTo(Instant.parse("2011-12-03T10:15:30.123Z"));
}
@Test
void shouldParseIso8601WithSeconds() {
assertThat(parse("2011-12-03T10:15:30Z")).isEqualTo(Instant.parse("2011-12-03T10:15:30.000Z"));
}
@Test