|
23 | 23 |
|
24 | 24 | import java.time.Instant;
|
25 | 25 | import java.time.ZoneId;
|
| 26 | +import java.time.ZoneOffset; |
26 | 27 | import java.time.format.DateTimeParseException;
|
27 | 28 | import java.time.temporal.TemporalAccessor;
|
28 | 29 | import java.util.Locale;
|
@@ -81,23 +82,37 @@ public void testLocales() {
|
81 | 82 | assertThat(DateFormatters.forPattern("strict_date_optional_time").getLocale(), is(Locale.ROOT));
|
82 | 83 | Locale locale = randomLocale(random());
|
83 | 84 | assertThat(DateFormatters.forPattern("strict_date_optional_time").withLocale(locale).getLocale(), is(locale));
|
84 |
| - IllegalArgumentException e = |
85 |
| - expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_millis").withLocale(locale)); |
86 |
| - assertThat(e.getMessage(), is("epoch_millis date formatter can only be in locale ROOT")); |
87 |
| - e = expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_second").withLocale(locale)); |
88 |
| - assertThat(e.getMessage(), is("epoch_second date formatter can only be in locale ROOT")); |
| 85 | + if (locale.equals(Locale.ROOT)) { |
| 86 | + DateFormatter millisFormatter = DateFormatters.forPattern("epoch_millis"); |
| 87 | + assertThat(millisFormatter.withLocale(locale), is(millisFormatter)); |
| 88 | + DateFormatter secondFormatter = DateFormatters.forPattern("epoch_second"); |
| 89 | + assertThat(secondFormatter.withLocale(locale), is(secondFormatter)); |
| 90 | + } else { |
| 91 | + IllegalArgumentException e = |
| 92 | + expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_millis").withLocale(locale)); |
| 93 | + assertThat(e.getMessage(), is("epoch_millis date formatter can only be in locale ROOT")); |
| 94 | + e = expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_second").withLocale(locale)); |
| 95 | + assertThat(e.getMessage(), is("epoch_second date formatter can only be in locale ROOT")); |
| 96 | + } |
89 | 97 | }
|
90 | 98 |
|
91 | 99 | public void testTimeZones() {
|
92 | 100 | // zone is null by default due to different behaviours between java8 and above
|
93 | 101 | assertThat(DateFormatters.forPattern("strict_date_optional_time").getZone(), is(nullValue()));
|
94 | 102 | ZoneId zoneId = randomZone();
|
95 | 103 | assertThat(DateFormatters.forPattern("strict_date_optional_time").withZone(zoneId).getZone(), is(zoneId));
|
96 |
| - IllegalArgumentException e = |
97 |
| - expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_millis").withZone(zoneId)); |
98 |
| - assertThat(e.getMessage(), is("epoch_millis date formatter can only be in zone offset UTC")); |
99 |
| - e = expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_second").withZone(zoneId)); |
100 |
| - assertThat(e.getMessage(), is("epoch_second date formatter can only be in zone offset UTC")); |
| 104 | + if (zoneId.equals(ZoneOffset.UTC)) { |
| 105 | + DateFormatter millisFormatter = DateFormatters.forPattern("epoch_millis"); |
| 106 | + assertThat(millisFormatter.withZone(zoneId), is(millisFormatter)); |
| 107 | + DateFormatter secondFormatter = DateFormatters.forPattern("epoch_second"); |
| 108 | + assertThat(secondFormatter.withZone(zoneId), is(secondFormatter)); |
| 109 | + } else { |
| 110 | + IllegalArgumentException e = |
| 111 | + expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_millis").withZone(zoneId)); |
| 112 | + assertThat(e.getMessage(), is("epoch_millis date formatter can only be in zone offset UTC")); |
| 113 | + e = expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_second").withZone(zoneId)); |
| 114 | + assertThat(e.getMessage(), is("epoch_second date formatter can only be in zone offset UTC")); |
| 115 | + } |
101 | 116 | }
|
102 | 117 |
|
103 | 118 | public void testEqualsAndHashcode() {
|
|
0 commit comments