diff --git a/src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java b/src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java index 6562606fb..ead949e0f 100644 --- a/src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java +++ b/src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java @@ -37,8 +37,7 @@ import java.io.Reader; import java.io.StringReader; import java.nio.charset.Charset; -import java.text.ParseException; -import java.text.SimpleDateFormat; +import java.time.OffsetDateTime; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -75,9 +74,6 @@ public class DockerClient { @SuppressFBWarnings(value="MS_SHOULD_BE_FINAL", justification="mutable for scripts") @Restricted(NoExternalUse.class) public static int CLIENT_TIMEOUT = Integer.getInteger(DockerClient.class.getName() + ".CLIENT_TIMEOUT", 180); // TODO 2.4+ SystemProperties - - // e.g. 2015-04-09T13:40:21.981801679Z - public static final String DOCKER_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss"; private final Launcher launcher; private final @CheckForNull Node node; @@ -232,13 +228,8 @@ public void rm(@Nonnull EnvVars launchEnv, @Nonnull String containerId) throws I if (createdString == null) { return null; } - // TODO Currently truncating. Find out how to specify last part for parsing (TZ etc) - String s = createdString.substring(1, DOCKER_DATE_TIME_FORMAT.length() - 1); - try { - return new SimpleDateFormat(DOCKER_DATE_TIME_FORMAT).parse(s); - } catch (ParseException e) { - throw new IOException(String.format("Error parsing created date '%s' for object '%s'.", s, objectId), e); - } + String s = createdString.substring(1, createdString.length() - 1); // remove enclosing quotes + return Date.from(OffsetDateTime.parse(s).toInstant()); } /** diff --git a/src/test/java/org/jenkinsci/plugins/docker/workflow/client/DockerClientTest.java b/src/test/java/org/jenkinsci/plugins/docker/workflow/client/DockerClientTest.java index 2351e8eef..d57cea74e 100644 --- a/src/test/java/org/jenkinsci/plugins/docker/workflow/client/DockerClientTest.java +++ b/src/test/java/org/jenkinsci/plugins/docker/workflow/client/DockerClientTest.java @@ -36,6 +36,7 @@ import java.io.IOException; import java.util.Collections; +import java.util.Date; /** * @author tom.fennelly@gmail.com @@ -58,15 +59,18 @@ public void setup() throws Exception { @Test public void test_run() throws IOException, InterruptedException { EnvVars launchEnv = DockerTestUtil.newDockerLaunchEnv(); + Date createdEarliest = new Date(); String containerId = dockerClient.run(launchEnv, "learn/tutorial", null, null, Collections.emptyMap(), Collections.emptyList(), new EnvVars(), dockerClient.whoAmI(), "cat"); + Date createdLatest = new Date(); Assert.assertEquals(64, containerId.length()); ContainerRecord containerRecord = dockerClient.getContainerRecord(launchEnv, containerId); Assert.assertEquals(dockerClient.inspect(launchEnv, "learn/tutorial", ".Id"), containerRecord.getImageId()); Assert.assertTrue(containerRecord.getContainerName().length() > 0); Assert.assertTrue(containerRecord.getHost().length() > 0); - Assert.assertTrue(containerRecord.getCreated() > 1000000000000L); + Assert.assertTrue(containerRecord.getCreated() >= createdEarliest.getTime()); + Assert.assertTrue(containerRecord.getCreated() <= createdLatest.getTime()); Assert.assertEquals(Collections.emptyList(), dockerClient.getVolumes(launchEnv, containerId)); // Also test that the stop works and cleans up after itself