44
55import java .text .SimpleDateFormat ;
66import java .time .Instant ;
7+ import java .time .format .DateTimeParseException ;
78import java .time .temporal .ChronoUnit ;
89import java .util .Date ;
910import java .util .TimeZone ;
@@ -20,25 +21,38 @@ public class GitHubStaticTest extends AbstractGitHubWireMockTest {
2021
2122 @ Test
2223 public void timeRoundTrip () throws Exception {
23- Instant instantNow = Instant .now ();
24+ final long stableInstantEpochMilli = 1533721222255L ;
25+ Instant instantNow = Instant .ofEpochMilli (stableInstantEpochMilli );
2426
2527 Date instantSeconds = Date .from (instantNow .truncatedTo (ChronoUnit .SECONDS ));
2628 Date instantMillis = Date .from (instantNow .truncatedTo (ChronoUnit .MILLIS ));
2729
28- // if we happen to land exactly on zero milliseconds, add 1 milli
29- if (instantSeconds .equals (instantMillis )) {
30- instantMillis = Date .from (instantNow .plusMillis (1 ).truncatedTo (ChronoUnit .MILLIS ));
31- }
30+ String instantFormatSlash = formatZonedDate (instantMillis , "yyyy/MM/dd HH:mm:ss ZZZZ" , "PST" );
31+ assertThat (instantFormatSlash , equalTo ("2018/08/08 02:40:22 -0700" ));
3232
33- // TODO: other formats
34- String instantFormatSlash = formatDate (instantMillis , "yyyy/MM/dd HH:mm:ss ZZZZ" );
3533 String instantFormatDash = formatDate (instantMillis , "yyyy-MM-dd'T'HH:mm:ss'Z'" );
34+ assertThat (instantFormatDash , equalTo ("2018-08-08T09:40:22Z" ));
35+
3636 String instantFormatMillis = formatDate (instantMillis , "yyyy-MM-dd'T'HH:mm:ss.S'Z'" );
37+ assertThat (instantFormatMillis , equalTo ("2018-08-08T09:40:22.255Z" ));
38+
39+ String instantFormatMillisZoned = formatZonedDate (instantMillis , "yyyy-MM-dd'T'HH:mm:ss.SXXX" , "PST" );
40+ assertThat (instantFormatMillisZoned , equalTo ("2018-08-08T02:40:22.255-07:00" ));
41+
3742 String instantSecondsFormatMillis = formatDate (instantSeconds , "yyyy-MM-dd'T'HH:mm:ss.S'Z'" );
43+ assertThat (instantSecondsFormatMillis , equalTo ("2018-08-08T09:40:22.0Z" ));
44+
45+ String instantSecondsFormatMillisZoned = formatZonedDate (instantSeconds , "yyyy-MM-dd'T'HH:mm:ss.SSSXXX" , "PST" );
46+ assertThat (instantSecondsFormatMillisZoned , equalTo ("2018-08-08T02:40:22.000-07:00" ));
47+
3848 String instantBadFormat = formatDate (instantMillis , "yy-MM-dd'T'HH:mm'Z'" );
49+ assertThat (instantBadFormat , equalTo ("18-08-08T09:40Z" ));
3950
4051 assertThat (GitHubClient .parseDate (GitHubClient .printDate (instantSeconds )),
4152 equalTo (GitHubClient .parseDate (GitHubClient .printDate (instantMillis ))));
53+ assertThat (GitHubClient .printDate (instantSeconds ), equalTo ("2018-08-08T09:40:22Z" ));
54+ assertThat (GitHubClient .printDate (GitHubClient .parseDate (instantFormatMillisZoned )),
55+ equalTo ("2018-08-08T09:40:22Z" ));
4256
4357 assertThat (instantSeconds , equalTo (GitHubClient .parseDate (GitHubClient .printDate (instantSeconds ))));
4458
@@ -51,14 +65,16 @@ public void timeRoundTrip() throws Exception {
5165
5266 // This parser does not truncate to the nearest second, so it will be equal
5367 assertThat (instantMillis , equalTo (GitHubClient .parseDate (instantFormatMillis )));
68+ assertThat (instantMillis , equalTo (GitHubClient .parseDate (instantFormatMillisZoned )));
5469
5570 assertThat (instantSeconds , equalTo (GitHubClient .parseDate (instantSecondsFormatMillis )));
71+ assertThat (instantSeconds , equalTo (GitHubClient .parseDate (instantSecondsFormatMillisZoned )));
5672
5773 try {
5874 GitHubClient .parseDate (instantBadFormat );
5975 fail ("Bad time format should throw." );
60- } catch (IllegalStateException e ) {
61- assertThat (e .getMessage (), equalTo ("Unable to parse the timestamp: " + instantBadFormat ));
76+ } catch (DateTimeParseException e ) {
77+ assertThat (e .getMessage (), equalTo ("Text '" + instantBadFormat + "' could not be parsed at index 0" ));
6278 }
6379 }
6480
@@ -226,8 +242,12 @@ public void testMappingReaderWriter() throws Exception {
226242 }
227243
228244 static String formatDate (Date dt , String format ) {
245+ return formatZonedDate (dt , format , "GMT" );
246+ }
247+
248+ static String formatZonedDate (Date dt , String format , String timeZone ) {
229249 SimpleDateFormat df = new SimpleDateFormat (format );
230- df .setTimeZone (TimeZone .getTimeZone ("GMT" ));
250+ df .setTimeZone (TimeZone .getTimeZone (timeZone ));
231251 return df .format (dt );
232252 }
233253
0 commit comments