Skip to content

Commit 4802c97

Browse files
committed
Fix GitHub.printDate()
GitHub.printDate() was not setting GMT timezone resulting in completely bogus output.
1 parent 9fc24d1 commit 4802c97

3 files changed

Lines changed: 79 additions & 5 deletions

File tree

src/main/java/org/kohsuke/github/GitHub.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,9 @@ public Reader renderMarkdown(String text) throws IOException {
956956
}
957957

958958
/*package*/ static String printDate(Date dt) {
959-
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(dt);
959+
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
960+
df.setTimeZone(TimeZone.getTimeZone("GMT"));
961+
return df.format(dt);
960962
}
961963

962964
/*package*/ static final ObjectMapper MAPPER = new ObjectMapper();

src/test/java/org/kohsuke/github/GHMilestoneTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void cleanUp() throws Exception {
2121
return;
2222
}
2323

24-
for (GHMilestone milestone : getRepository().listMilestones(GHIssueState.ALL)) {
24+
for (GHMilestone milestone : getRepository(gitHubBeforeAfter).listMilestones(GHIssueState.ALL)) {
2525
if ("Original Title".equals(milestone.getTitle()) ||
2626
"Updated Title".equals(milestone.getTitle())) {
2727
milestone.delete();
@@ -37,7 +37,8 @@ public void testUpdateMilestone() throws Exception {
3737

3838
String NEW_TITLE = "Updated Title";
3939
String NEW_DESCRIPTION = "Updated Description";
40-
Date NEW_DUE_DATE = GitHub.parseDate("2020-10-01T17:00:00Z");
40+
Date NEW_DUE_DATE = GitHub.parseDate("2020-10-01T13:00:00Z");
41+
Date OUTPUT_DUE_DATE = GitHub.parseDate("2020-10-01T13:00:00Z");
4142

4243
milestone.setTitle(NEW_TITLE);
4344
milestone.setDescription(NEW_DESCRIPTION);
@@ -48,8 +49,7 @@ public void testUpdateMilestone() throws Exception {
4849

4950
assertEquals(NEW_TITLE, milestone.getTitle());
5051
assertEquals(NEW_DESCRIPTION, milestone.getDescription());
51-
// The dates never seem to match exactly...
52-
//assertEquals(NEW_DUE_DATE, milestone.getDueOn());
52+
assertEquals(NEW_DUE_DATE, milestone.getDueOn());
5353
assertNotNull(milestone.getDueOn());
5454
}
5555

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package org.kohsuke.github;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
import java.text.SimpleDateFormat;
7+
import java.time.Instant;
8+
import java.time.temporal.ChronoUnit;
9+
import java.util.Date;
10+
import java.util.TimeZone;
11+
12+
import static org.hamcrest.CoreMatchers.equalTo;
13+
import static org.hamcrest.CoreMatchers.not;
14+
15+
/**
16+
* Unit test for {@link GitHub} static helpers.
17+
*
18+
* @author Liam Newman
19+
*/
20+
public class GitHubStaticTest extends Assert {
21+
22+
@Test
23+
public void timeRoundTrip() throws Exception {
24+
Instant instantNow = Instant.now();
25+
26+
Date instantSeconds = Date.from(instantNow.truncatedTo(ChronoUnit.SECONDS));
27+
Date instantMillis = Date.from(instantNow.truncatedTo(ChronoUnit.MILLIS));
28+
29+
// TODO: other formats
30+
String instantFormatSlash = formatDate(instantMillis, "yyyy/MM/dd HH:mm:ss ZZZZ");
31+
String instantFormatDash = formatDate(instantMillis, "yyyy-MM-dd'T'HH:mm:ss'Z'");
32+
String instantFormatMillis = formatDate(instantMillis, "yyyy-MM-dd'T'HH:mm:ss.S'Z'");
33+
String instantSecondsFormatMillis = formatDate(instantSeconds, "yyyy-MM-dd'T'HH:mm:ss.S'Z'");
34+
String instantBadFormat = formatDate(instantMillis, "yy-MM-dd'T'HH:mm'Z'");
35+
36+
37+
assertThat(GitHub.parseDate(GitHub.printDate(instantSeconds)),
38+
equalTo(GitHub.parseDate(GitHub.printDate(instantMillis))));
39+
40+
assertThat(instantSeconds,
41+
equalTo(GitHub.parseDate(GitHub.printDate(instantSeconds))));
42+
43+
assertThat(instantMillis,
44+
not(equalTo(GitHub.parseDate(GitHub.printDate(instantMillis)))));
45+
46+
assertThat(instantSeconds,
47+
equalTo(GitHub.parseDate(instantFormatSlash)));
48+
49+
assertThat(instantSeconds,
50+
equalTo(GitHub.parseDate(instantFormatDash)));
51+
52+
assertThat(instantMillis,
53+
equalTo(GitHub.parseDate(instantFormatMillis)));
54+
55+
assertThat(instantSeconds,
56+
equalTo(GitHub.parseDate(instantSecondsFormatMillis)));
57+
58+
try {
59+
GitHub.parseDate(instantBadFormat);
60+
fail("Bad time format should throw.");
61+
} catch (IllegalStateException e) {
62+
assertThat(e.getMessage(), equalTo("Unable to parse the timestamp: " + instantBadFormat));
63+
}
64+
}
65+
66+
static String formatDate(Date dt, String format) {
67+
SimpleDateFormat df = new SimpleDateFormat(format);
68+
df.setTimeZone(TimeZone.getTimeZone("GMT"));
69+
return df.format(dt);
70+
}
71+
72+
}

0 commit comments

Comments
 (0)