Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow more timezone formats #2320

Merged
merged 7 commits into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.nio.file.Paths;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.HashSet;
Expand Down Expand Up @@ -730,9 +731,21 @@ static Instant getCreationTime(String configuredCreationTime, ProjectProperties
return Instant.now();

default:
return DateTimeFormatter.ISO_DATE_TIME.parse(configuredCreationTime, Instant::from);
DateTimeFormatter formatter =
new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.optionalStart()
.appendOffset("+HH:MM", "+00:00")
.optionalEnd()
.optionalStart()
.appendOffset("+HHMM", "+0000")
.optionalEnd()
.optionalStart()
.appendOffset("+HH", "Z")
.optionalEnd()
.toFormatter();
return formatter.parse(configuredCreationTime, Instant::from);
chanseokoh marked this conversation as resolved.
Show resolved Hide resolved
}

} catch (DateTimeParseException ex) {
throw new InvalidCreationTimeException(configuredCreationTime, configuredCreationTime, ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.google.cloud.tools.jib.api.RegistryImage;
import com.google.cloud.tools.jib.configuration.BuildContext;
import com.google.cloud.tools.jib.configuration.ImageConfiguration;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.Resources;
Expand Down Expand Up @@ -908,11 +909,27 @@ public void testGetCreationTime_useCurrentTimestamp() throws InvalidCreationTime

@Test
public void testGetCreationTime_isoDateTimeValue() throws InvalidCreationTimeException {
Instant time =
PluginConfigurationProcessor.getCreationTime(
"2011-12-03T10:15:30+09:00", projectProperties);
Instant expected = DateTimeFormatter.ISO_DATE_TIME.parse("2011-12-03T01:15:30Z", Instant::from);
Assert.assertEquals(expected, time);
List<String> validTimeStamps =
ImmutableList.of(
"2011-12-03T10:15:30+09:00",
"2011-12-03T10:15:30+0900",
"2011-12-03T10:15:30+09",
"2011-12-03T01:15:30Z");
for (String timeString : validTimeStamps) {
Instant time = PluginConfigurationProcessor.getCreationTime(timeString, projectProperties);
Assert.assertEquals(expected, time);
}
}

@Test
public void testGetCreationTime_isoDateTimeValueRequiresTimeZone() {
try {
PluginConfigurationProcessor.getCreationTime("2011-12-03T01:15:30", projectProperties);
Assert.fail("creationTime should fail if timezone not specified");
loosebazooka marked this conversation as resolved.
Show resolved Hide resolved
} catch (InvalidCreationTimeException ex) {
// pass
}
}

@Test
Expand Down