Fix GitHub.printDate()

GitHub.printDate() was not setting GMT timezone resulting in completely bogus output.
This commit is contained in:
Liam Newman
2019-10-30 14:57:56 -07:00
parent 9fc24d1981
commit 4802c97e89
3 changed files with 79 additions and 5 deletions

View File

@@ -956,7 +956,9 @@ public class GitHub {
}
/*package*/ static String printDate(Date dt) {
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(dt);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
return df.format(dt);
}
/*package*/ static final ObjectMapper MAPPER = new ObjectMapper();

View File

@@ -21,7 +21,7 @@ public class GHMilestoneTest extends AbstractGitHubWireMockTest {
return;
}
for (GHMilestone milestone : getRepository().listMilestones(GHIssueState.ALL)) {
for (GHMilestone milestone : getRepository(gitHubBeforeAfter).listMilestones(GHIssueState.ALL)) {
if ("Original Title".equals(milestone.getTitle()) ||
"Updated Title".equals(milestone.getTitle())) {
milestone.delete();
@@ -37,7 +37,8 @@ public class GHMilestoneTest extends AbstractGitHubWireMockTest {
String NEW_TITLE = "Updated Title";
String NEW_DESCRIPTION = "Updated Description";
Date NEW_DUE_DATE = GitHub.parseDate("2020-10-01T17:00:00Z");
Date NEW_DUE_DATE = GitHub.parseDate("2020-10-01T13:00:00Z");
Date OUTPUT_DUE_DATE = GitHub.parseDate("2020-10-01T13:00:00Z");
milestone.setTitle(NEW_TITLE);
milestone.setDescription(NEW_DESCRIPTION);
@@ -48,8 +49,7 @@ public class GHMilestoneTest extends AbstractGitHubWireMockTest {
assertEquals(NEW_TITLE, milestone.getTitle());
assertEquals(NEW_DESCRIPTION, milestone.getDescription());
// The dates never seem to match exactly...
//assertEquals(NEW_DUE_DATE, milestone.getDueOn());
assertEquals(NEW_DUE_DATE, milestone.getDueOn());
assertNotNull(milestone.getDueOn());
}

View File

@@ -0,0 +1,72 @@
package org.kohsuke.github;
import org.junit.Assert;
import org.junit.Test;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.TimeZone;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not;
/**
* Unit test for {@link GitHub} static helpers.
*
* @author Liam Newman
*/
public class GitHubStaticTest extends Assert {
@Test
public void timeRoundTrip() throws Exception {
Instant instantNow = Instant.now();
Date instantSeconds = Date.from(instantNow.truncatedTo(ChronoUnit.SECONDS));
Date instantMillis = Date.from(instantNow.truncatedTo(ChronoUnit.MILLIS));
// TODO: other formats
String instantFormatSlash = formatDate(instantMillis, "yyyy/MM/dd HH:mm:ss ZZZZ");
String instantFormatDash = formatDate(instantMillis, "yyyy-MM-dd'T'HH:mm:ss'Z'");
String instantFormatMillis = formatDate(instantMillis, "yyyy-MM-dd'T'HH:mm:ss.S'Z'");
String instantSecondsFormatMillis = formatDate(instantSeconds, "yyyy-MM-dd'T'HH:mm:ss.S'Z'");
String instantBadFormat = formatDate(instantMillis, "yy-MM-dd'T'HH:mm'Z'");
assertThat(GitHub.parseDate(GitHub.printDate(instantSeconds)),
equalTo(GitHub.parseDate(GitHub.printDate(instantMillis))));
assertThat(instantSeconds,
equalTo(GitHub.parseDate(GitHub.printDate(instantSeconds))));
assertThat(instantMillis,
not(equalTo(GitHub.parseDate(GitHub.printDate(instantMillis)))));
assertThat(instantSeconds,
equalTo(GitHub.parseDate(instantFormatSlash)));
assertThat(instantSeconds,
equalTo(GitHub.parseDate(instantFormatDash)));
assertThat(instantMillis,
equalTo(GitHub.parseDate(instantFormatMillis)));
assertThat(instantSeconds,
equalTo(GitHub.parseDate(instantSecondsFormatMillis)));
try {
GitHub.parseDate(instantBadFormat);
fail("Bad time format should throw.");
} catch (IllegalStateException e) {
assertThat(e.getMessage(), equalTo("Unable to parse the timestamp: " + instantBadFormat));
}
}
static String formatDate(Date dt, String format) {
SimpleDateFormat df = new SimpleDateFormat(format);
df.setTimeZone(TimeZone.getTimeZone("GMT"));
return df.format(dt);
}
}