Skip to content

Commit

Permalink
Allow more timezone formats (#2320)
Browse files Browse the repository at this point in the history
* Allow more timezone formats

* Support full iso6801 and increase test coverage
  • Loading branch information
loosebazooka authored Mar 12, 2020
1 parent f6a9a3a commit ac099d2
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
3 changes: 3 additions & 0 deletions jib-gradle-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ All notable changes to this project will be documented in this file.

### Changed

- `jib.container.creationTime` now accepts more timezone formats:`+HHmm`. This allows for easier configuration of creationTime by external systems. ([#2320](https://github.com/GoogleContainerTools/jib/issues/2320))

### Fixed

## 2.1.0

### Added

- Additionally reads credentials from `~/.docker/.dockerconfigjson` and legacy Docker config (`~/.docker/.dockercfg`). Also searches for `$HOME/.docker/*` (in addition to current `System.get("user.home")/.docker/*`). This may help retrieve credentials, for example, on Kubernetes. ([#2260](https://github.com/GoogleContainerTools/jib/issues/2260))
- New skaffold configuration options that modify how jib's build config is presented to skaffold ([#2292](https://github.com/GoogleContainerTools/jib/pull/2292)):
- `jib.skaffold.watch.buildIncludes`: a list of build files to watch
Expand Down
3 changes: 3 additions & 0 deletions jib-maven-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ All notable changes to this project will be documented in this file.

### Changed

- `<container><creationTime>` now accepts more timezone formats:`+HHmm`. This allows for easier configuration of creationTime by external systems. ([#2320](https://github.com/GoogleContainerTools/jib/issues/2320))

### Fixed

## 2.1.0

### Added

- Additionally reads credentials from `~/.docker/.dockerconfigjson` and legacy Docker config (`~/.docker/.dockercfg`). Also searches for `$HOME/.docker/*` (in addition to current `System.get("user.home")/.docker/*`). This may help retrieve credentials, for example, on Kubernetes. ([#2260](https://github.com/GoogleContainerTools/jib/issues/2260))
- New skaffold configuration options that modify how jib's build config is presented to skaffold ([#2292](https://github.com/GoogleContainerTools/jib/pull/2292)):
- `<watch><buildIncludes>`: a list of build files to watch
Expand Down
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,16 @@ 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_DATE_TIME) // parses isoStrict
// add ability to parse with no ":" in tz
.optionalStart()
.appendOffset("+HHmm", "+0000")
.optionalEnd()
.toFormatter();
return formatter.parse(configuredCreationTime, Instant::from);
}

} 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,52 @@ 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+09:00[Asia/Tokyo]",
"2011-12-02T16:15:30-09:00",
"2011-12-03T10:15:30+0900",
"2011-12-02T16:15:30-0900",
"2011-12-03T10:15:30+09",
"2011-12-02T16:15:30-09",
"2011-12-03T01:15:30Z");
for (String timeString : validTimeStamps) {
Instant time = PluginConfigurationProcessor.getCreationTime(timeString, projectProperties);
Assert.assertEquals("for " + timeString, expected, time);
}
}

@Test
public void testGetCreationTime_isoDateTimeValueTimeZoneRegionOnlyAllowedForMostStrict8601Mode() {
List<String> invalidTimeStamps =
ImmutableList.of(
"2011-12-03T01:15:30+0900[Asia/Tokyo]", "2011-12-03T01:15:30+09[Asia/Tokyo]");
for (String timeString : invalidTimeStamps) {
try {
PluginConfigurationProcessor.getCreationTime(timeString, projectProperties);
// this is the expected behavior, not specifically designed like this for any reason, feel
// free to change this behavior and update the test
Assert.fail(
"creationTime should fail if region specified when zone not in HH:MM mode - "
+ timeString);
} catch (InvalidCreationTimeException ex) {
// pass
}
}
}

@Test
public void testGetCreationTime_isoDateTimeValueRequiresTimeZone() {
try {
PluginConfigurationProcessor.getCreationTime("2011-12-03T01:15:30", projectProperties);
// this is the expected behavior, not specifically designed like this for any reason, feel
// free to change this behavior and update the test
Assert.fail("getCreationTime should fail if timezone not specified");
} catch (InvalidCreationTimeException ex) {
// pass
}
}

@Test
Expand Down

0 comments on commit ac099d2

Please sign in to comment.